-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathconfig.ts
More file actions
127 lines (107 loc) · 3.81 KB
/
config.ts
File metadata and controls
127 lines (107 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as vscode from 'vscode';
import { Commands } from '../constants';
import { executeCommand } from '../utils/shell';
import { getDevProxyExe } from '../detect';
import { VersionPreference } from '../enums';
import * as logger from '../logger';
/**
* Configuration file commands: open, create new.
*/
export function registerConfigCommands(
context: vscode.ExtensionContext,
configuration: vscode.WorkspaceConfiguration
): void {
const versionPreference = configuration.get('version') as VersionPreference;
const devProxyExe = getDevProxyExe(versionPreference);
context.subscriptions.push(
vscode.commands.registerCommand(Commands.configOpen, () => openConfig(devProxyExe))
);
context.subscriptions.push(
vscode.commands.registerCommand(Commands.configNew, () => createNewConfig(devProxyExe))
);
}
async function openConfig(devProxyExe: string): Promise<void> {
logger.debug('Opening Dev Proxy config', { devProxyExe });
await executeCommand(`${devProxyExe} config open`);
}
async function createNewConfig(devProxyExe: string): Promise<void> {
const fileName = await promptForFileName();
if (!fileName) {
return;
}
const workspaceFolder = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath;
if (!workspaceFolder) {
logger.warn('Cannot create config: no workspace folder open');
vscode.window.showErrorMessage('No workspace folder open');
return;
}
logger.info('Creating new config file', { fileName, workspaceFolder });
const devProxyFolder = vscode.Uri.file(`${workspaceFolder}/.devproxy`);
const configUri = vscode.Uri.file(`${workspaceFolder}/.devproxy/${fileName}`);
// Check if file already exists
if (await fileExists(configUri)) {
logger.warn('Config file already exists', { path: configUri.fsPath });
vscode.window.showErrorMessage('A file with that name already exists');
return;
}
try {
// Ensure .devproxy folder exists
await ensureDirectoryExists(devProxyFolder);
// Create the config file
await vscode.window.withProgress(
{
location: vscode.ProgressLocation.Notification,
title: 'Creating new config file...',
},
async () => {
await executeCommand(`${devProxyExe} config new ${fileName}`, {
cwd: `${workspaceFolder}/.devproxy`,
});
}
);
// Open the newly created file
logger.info('Config file created', { path: configUri.fsPath });
const document = await vscode.workspace.openTextDocument(configUri);
await vscode.window.showTextDocument(document);
} catch (error) {
logger.error('Failed to create new config file', error);
vscode.window.showErrorMessage('Failed to create new config file');
}
}
async function promptForFileName(): Promise<string | undefined> {
return vscode.window.showInputBox({
prompt: 'Enter the name of the new config file',
placeHolder: 'devproxyrc.json',
value: 'devproxyrc.json',
validateInput: validateFileName,
});
}
function validateFileName(value: string): string | undefined {
const errors: string[] = [];
if (!value) {
errors.push('The file name cannot be empty');
}
const invalidChars = /[/\\:*?"<>|\s]/;
if (invalidChars.test(value)) {
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 ? errors[0] : undefined;
}
async function fileExists(uri: vscode.Uri): Promise<boolean> {
try {
const stat = await vscode.workspace.fs.stat(uri);
return stat.type === vscode.FileType.File;
} catch {
return false;
}
}
async function ensureDirectoryExists(uri: vscode.Uri): Promise<void> {
try {
await vscode.workspace.fs.stat(uri);
} catch {
await vscode.workspace.fs.createDirectory(uri);
}
}