Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.21.0] - Unreleased

No changes yet.
### Added:

- Command: `dev-proxy-toolkit.config-new` - Create new configuration file

## [0.20.0] - 2025-04-01

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The following sections describe the features that the extension contributes to V
- `Dev Proxy Toolkit: Start recording` - Only available when Dev Proxy is running
- `Dev Proxy Toolkit: Stop recording`- Only available when Dev Proxy is recording
- `Dev Proxy Toolkit: Open configuration file`- Only available when Dev Proxy is installed
- `Dev Proxy Toolkit: Create configuration file`- Only available when Dev Proxy is installed

### Diagnostics

Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
"category": "Dev Proxy Toolkit",
"icon": "$(file-code)",
"enablement": "isDevProxyInstalled"
},
{
"command": "dev-proxy-toolkit.config-new",
"title": "Create configuration file",
"category": "Dev Proxy Toolkit",
"icon": "$(file-code)",
"enablement": "isDevProxyInstalled"
}
],
"menus": {
Expand Down
58 changes: 58 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,62 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
? await executeCommand(`${VersionExeName.Stable} config open`)
: await executeCommand(`${VersionExeName.Beta} config open`);
}));

context.subscriptions.push(
vscode.commands.registerCommand('dev-proxy-toolkit.config-new', async () => {
const versionPreference = configuration.get('version') as VersionPreference;
const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta;

// ask the user for the filename that they want to use
const fileName = await vscode.window.showInputBox({
prompt: 'Enter the name of the new config file',
value: 'devproxyrc.json',
validateInput: (value: string) => {
console.log(value);
const errors: string[] = [];

if (!value) {
errors.push('The file name cannot be empty');
}

if (value.includes('/') || value.includes('\\') || value.includes(' ') || value.includes(':') || value.includes('*') || value.includes('?') || value.includes('"') || value.includes('<') || value.includes('>') || value.includes('|')) {
errors.push('The file name cannot contain special characters');
}

if (!value.endsWith('.json') && !value.endsWith('.jsonc')) {
errors.push('The file name must use .json or .jsonc extension');
}

return errors.length === 0 ? undefined : errors[0];
}
});

// check if file exists, if it does show an error message
// we do this after the user has entered the filename
try {
const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
console.log(workspaceFolder);
const { type } = await vscode.workspace.fs.stat(vscode.Uri.file(`${workspaceFolder}/${fileName}`));
if (type === vscode.FileType.File) {
vscode.window.showErrorMessage('A file with that name already exists');
return;
}
} catch { } // file does not exist, continue

try {
// show progress
await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
title: 'Creating new config file...'
}, async () => {
await executeCommand(`${exeName} config new ${fileName}`, { cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath });
});

const configUri = vscode.Uri.file(`${vscode.workspace.workspaceFolders?.[0].uri.fsPath}/${fileName}`);
const document = await vscode.workspace.openTextDocument(configUri);
await vscode.window.showTextDocument(document);
} catch (error) {
vscode.window.showErrorMessage('Failed to create new config file');
}
}));
};
6 changes: 3 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import parse from 'json-to-ast';
import { exec } from 'child_process';
import { exec, ExecOptions } from 'child_process';

export const getASTNode = (
children: parse.PropertyNode[],
Expand Down Expand Up @@ -98,9 +98,9 @@ export const sleep = (ms: number): Promise<void> => {
});
};

export const executeCommand = async (cmd: string): Promise<string> => {
export const executeCommand = async (cmd: string, options: ExecOptions = {}): Promise<string> => {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
exec(cmd, options, (error, stdout, stderr) => {
if (error) {
reject(`exec error: ${error}`);
} else if (stderr) {
Expand Down