@@ -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} ;
0 commit comments