Skip to content

Commit 9f27491

Browse files
committed
Add config new command. Closes #209
Closes #209
1 parent 059e599 commit 9f27491

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
1010
## [0.21.0] - Unreleased
1111

12-
No changes yet.
12+
### Added:
13+
14+
- Command: `dev-proxy-toolkit.config-new` - Create new configuration file
1315

1416
## [0.20.0] - 2025-04-01
1517

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The following sections describe the features that the extension contributes to V
2929
- `Dev Proxy Toolkit: Start recording` - Only available when Dev Proxy is running
3030
- `Dev Proxy Toolkit: Stop recording`- Only available when Dev Proxy is recording
3131
- `Dev Proxy Toolkit: Open configuration file`- Only available when Dev Proxy is installed
32+
- `Dev Proxy Toolkit: Create configuration file`- Only available when Dev Proxy is installed
3233

3334
### Diagnostics
3435

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@
6262
"category": "Dev Proxy Toolkit",
6363
"icon": "$(file-code)",
6464
"enablement": "isDevProxyInstalled"
65+
},
66+
{
67+
"command": "dev-proxy-toolkit.config-new",
68+
"title": "Create configuration file",
69+
"category": "Dev Proxy Toolkit",
70+
"icon": "$(file-code)",
71+
"enablement": "isDevProxyInstalled"
6572
}
6673
],
6774
"menus": {

src/commands.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,62 @@ export const registerCommands = (context: vscode.ExtensionContext, configuration
195195
? await executeCommand(`${VersionExeName.Stable} config open`)
196196
: await executeCommand(`${VersionExeName.Beta} config open`);
197197
}));
198+
199+
context.subscriptions.push(
200+
vscode.commands.registerCommand('dev-proxy-toolkit.config-new', async () => {
201+
const versionPreference = configuration.get('version') as VersionPreference;
202+
const exeName = versionPreference === VersionPreference.Stable ? VersionExeName.Stable : VersionExeName.Beta;
203+
204+
// ask the user for the filename that they want to use
205+
const fileName = await vscode.window.showInputBox({
206+
prompt: 'Enter the name of the new config file',
207+
value: 'devproxyrc.json',
208+
validateInput: (value: string) => {
209+
console.log(value);
210+
const errors: string[] = [];
211+
212+
if (!value) {
213+
errors.push('The file name cannot be empty');
214+
}
215+
216+
if (value.includes('/') || value.includes('\\') || value.includes(' ') || value.includes(':') || value.includes('*') || value.includes('?') || value.includes('"') || value.includes('<') || value.includes('>') || value.includes('|')) {
217+
errors.push('The file name cannot contain special characters');
218+
}
219+
220+
if (!value.endsWith('.json') && !value.endsWith('.jsonc')) {
221+
errors.push('The file name must use .json or .jsonc extension');
222+
}
223+
224+
return errors.length === 0 ? undefined : errors[0];
225+
}
226+
});
227+
228+
// check if file exists, if it does show an error message
229+
// we do this after the user has entered the filename
230+
try {
231+
const workspaceFolder = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
232+
console.log(workspaceFolder);
233+
const { type } = await vscode.workspace.fs.stat(vscode.Uri.file(`${workspaceFolder}/${fileName}`));
234+
if (type === vscode.FileType.File) {
235+
vscode.window.showErrorMessage('A file with that name already exists');
236+
return;
237+
}
238+
} catch { } // file does not exist, continue
239+
240+
try {
241+
// show progress
242+
await vscode.window.withProgress({
243+
location: vscode.ProgressLocation.Notification,
244+
title: 'Creating new config file...'
245+
}, async () => {
246+
await executeCommand(`${exeName} config new ${fileName}`, { cwd: vscode.workspace.workspaceFolders?.[0].uri.fsPath });
247+
});
248+
249+
const configUri = vscode.Uri.file(`${vscode.workspace.workspaceFolders?.[0].uri.fsPath}/${fileName}`);
250+
const document = await vscode.workspace.openTextDocument(configUri);
251+
await vscode.window.showTextDocument(document);
252+
} catch (error) {
253+
vscode.window.showErrorMessage('Failed to create new config file');
254+
}
255+
}));
198256
};

src/helpers.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import parse from 'json-to-ast';
3-
import { exec } from 'child_process';
3+
import { exec, ExecOptions } from 'child_process';
44

55
export const getASTNode = (
66
children: parse.PropertyNode[],
@@ -98,9 +98,9 @@ export const sleep = (ms: number): Promise<void> => {
9898
});
9999
};
100100

101-
export const executeCommand = async (cmd: string): Promise<string> => {
101+
export const executeCommand = async (cmd: string, options: ExecOptions = {}): Promise<string> => {
102102
return new Promise((resolve, reject) => {
103-
exec(cmd, (error, stdout, stderr) => {
103+
exec(cmd, options, (error, stdout, stderr) => {
104104
if (error) {
105105
reject(`exec error: ${error}`);
106106
} else if (stderr) {

0 commit comments

Comments
 (0)