@@ -34,11 +34,25 @@ async function isCmdInstalled(): Promise<boolean> {
3434 }
3535}
3636
37- async function getCmdFilePaths ( ) : Promise < { startupFile : string ; mainBatchFile : string } > {
38- const homeDir = os . homedir ( ) ;
37+ interface CmdFilePaths {
38+ startupFile : string ;
39+ regStartupFile : string ;
40+ mainBatchFile : string ;
41+ regMainBatchFile : string ;
42+ }
43+
44+ async function getCmdFilePaths ( ) : Promise < CmdFilePaths > {
45+ const homeDir = process . env . USERPROFILE ?? os . homedir ( ) ;
46+ const cmdrcDir = path . join ( homeDir , '.cmdrc' ) ;
47+
48+ // Ensure the directory exists
49+ await fs . ensureDir ( cmdrcDir ) ;
50+
3951 return {
40- mainBatchFile : path . join ( homeDir , 'cmd_startup.bat' ) ,
41- startupFile : path . join ( homeDir , 'vscode-python-cmd-init.cmd' ) ,
52+ mainBatchFile : path . join ( cmdrcDir , 'cmd_startup.bat' ) ,
53+ regMainBatchFile : path . join ( '%USERPROFILE%' , '.cmdrc' , 'cmd_startup.bat' ) ,
54+ startupFile : path . join ( cmdrcDir , 'vscode-python-cmd-init.cmd' ) ,
55+ regStartupFile : path . join ( '%USERPROFILE%' , '.cmdrc' , 'vscode-python-cmd-init.cmd' ) ,
4256 } ;
4357}
4458
@@ -56,24 +70,23 @@ function getMainBatchFileContent(startupFile: string, existingContent?: string):
5670
5771 // Add header
5872 content . push ( '@echo off' ) ;
59- content . push ( 'rem This file is managed by VS Code Python extension ' ) ;
73+ content . push ( 'rem startup used in HKCU\\Software\\Microsoft\\Command Processor key AutoRun ' ) ;
6074 content . push ( '' ) ;
6175
76+ // Add our startup file call
77+ content . push ( 'rem VS Code Python environment activation' ) ;
78+ content . push ( `if exist "${ startupFile } " call "${ startupFile } "` ) ;
79+
6280 // Add existing AutoRun content if any
6381 if ( existingContent && existingContent . trim ( ) ) {
64- content . push ( 'rem Original AutoRun content' ) ;
6582 content . push ( existingContent ) ;
6683 content . push ( '' ) ;
6784 }
6885
69- // Add our startup file call
70- content . push ( 'rem VS Code Python environment activation' ) ;
71- content . push ( `if exist "${ startupFile } " call "${ startupFile } "` ) ;
72-
7386 return content . join ( lineSep ) ;
7487}
7588
76- async function checkRegistryAutoRun ( mainBatchFile : string ) : Promise < boolean > {
89+ async function checkRegistryAutoRun ( regMainBatchFile : string ) : Promise < boolean > {
7790 if ( ! isWindows ( ) ) {
7891 return false ;
7992 }
@@ -85,7 +98,7 @@ async function checkRegistryAutoRun(mainBatchFile: string): Promise<boolean> {
8598 } ) ;
8699
87100 // Check if the output contains our batch file path
88- return stdout . includes ( mainBatchFile ) ;
101+ return stdout . includes ( regMainBatchFile ) ;
89102 } catch {
90103 // If the command fails, the registry key might not exist
91104 return false ;
@@ -138,47 +151,55 @@ async function setupRegistryAutoRun(mainBatchFile: string): Promise<boolean> {
138151 }
139152}
140153
141- async function isCmdStartupSetup ( startupFile : string , mainBatchFile : string , key : string ) : Promise < boolean > {
154+ async function isCmdStartupSetup ( cmdFiles : CmdFilePaths , key : string ) : Promise < boolean > {
142155 // Check both the startup file and registry AutoRun setting
143- const fileExists = await fs . pathExists ( startupFile ) ;
144- const fileHasContent = fileExists ? ( await fs . readFile ( startupFile , 'utf8' ) ) . includes ( key ) : false ;
156+ const fileExists = await fs . pathExists ( cmdFiles . startupFile ) ;
157+ const fileHasContent = fileExists ? ( await fs . readFile ( cmdFiles . startupFile , 'utf8' ) ) . includes ( key ) : false ;
158+ if ( ! fileHasContent ) {
159+ return false ;
160+ }
145161
146- const mainFileExists = await fs . pathExists ( mainBatchFile ) ;
147- const registrySetup = await checkRegistryAutoRun ( mainBatchFile ) ;
162+ const mainFileExists = await fs . pathExists ( cmdFiles . mainBatchFile ) ;
163+ const mainFileHasContent = mainFileExists
164+ ? ( await fs . readFile ( cmdFiles . mainBatchFile , 'utf8' ) ) . includes ( cmdFiles . regStartupFile )
165+ : false ;
148166
149- return fileHasContent && mainFileExists && registrySetup ;
167+ if ( ! mainFileHasContent ) {
168+ return false ;
169+ }
170+
171+ const registrySetup = await checkRegistryAutoRun ( cmdFiles . regMainBatchFile ) ;
172+ return registrySetup ;
150173}
151174
152- async function setupCmdStartup ( startupFile : string , mainBatchFile : string , key : string ) : Promise < boolean > {
175+ async function setupCmdStartup ( cmdFiles : CmdFilePaths , key : string ) : Promise < boolean > {
153176 try {
154177 const activationContent = getActivationContent ( key ) ;
155178
156179 // Step 1: Create or update the activation file
157- if ( ! ( await fs . pathExists ( startupFile ) ) ) {
158- // Create new file with our content
159- await fs . writeFile ( startupFile , activationContent ) ;
160- traceInfo ( `Created new CMD activation file at: ${ startupFile } \r\n${ activationContent } ` ) ;
180+ if ( ! ( await fs . pathExists ( cmdFiles . startupFile ) ) ) {
181+ await fs . writeFile ( cmdFiles . startupFile , activationContent ) ;
182+ traceInfo ( `Created new CMD activation file at: ${ cmdFiles . startupFile } \r\n${ activationContent } ` ) ;
161183 } else {
162- // Update existing file if it doesn't have our content
163- const content = await fs . readFile ( startupFile , 'utf8' ) ;
184+ const content = await fs . readFile ( cmdFiles . startupFile , 'utf8' ) ;
164185 if ( ! content . includes ( key ) ) {
165- await fs . writeFile ( startupFile , `${ content } ${ activationContent } ` ) ;
166- traceInfo ( `Updated existing CMD activation file at: ${ startupFile } \r\n${ activationContent } ` ) ;
186+ await fs . writeFile ( cmdFiles . startupFile , `${ content } ${ activationContent } ` ) ;
187+ traceInfo ( `Updated existing CMD activation file at: ${ cmdFiles . startupFile } \r\n${ activationContent } ` ) ;
167188 } else {
168- traceInfo ( `CMD activation file at ${ startupFile } already contains activation code` ) ;
189+ traceInfo ( `CMD activation file at ${ cmdFiles . startupFile } already contains activation code` ) ;
169190 }
170191 }
171192
172193 // Step 2: Get existing AutoRun content
173194 const existingAutoRun = await getExistingAutoRun ( ) ;
174195
175196 // Step 3: Create or update the main batch file
176- const mainBatchContent = getMainBatchFileContent ( startupFile , existingAutoRun ) ;
177- await fs . writeFile ( mainBatchFile , mainBatchContent ) ;
178- traceInfo ( `Created/Updated main batch file at: ${ mainBatchFile } ` ) ;
197+ const mainBatchContent = getMainBatchFileContent ( cmdFiles . regStartupFile , existingAutoRun ) ;
198+ await fs . writeFile ( cmdFiles . mainBatchFile , mainBatchContent ) ;
199+ traceInfo ( `Created/Updated main batch file at: ${ cmdFiles . mainBatchFile } ` ) ;
179200
180201 // Step 4: Setup registry AutoRun to call our main batch file
181- const registrySetup = await setupRegistryAutoRun ( mainBatchFile ) ;
202+ const registrySetup = await setupRegistryAutoRun ( cmdFiles . mainBatchFile ) ;
182203
183204 return registrySetup ;
184205 } catch ( err ) {
@@ -232,8 +253,8 @@ export class CmdStartupProvider implements ShellStartupProvider {
232253 }
233254
234255 try {
235- const { startupFile , mainBatchFile } = await getCmdFilePaths ( ) ;
236- const isSetup = await isCmdStartupSetup ( startupFile , mainBatchFile , this . cmdActivationEnvVarKey ) ;
256+ const cmdFiles = await getCmdFilePaths ( ) ;
257+ const isSetup = await isCmdStartupSetup ( cmdFiles , this . cmdActivationEnvVarKey ) ;
237258 return isSetup ? ShellSetupState . Setup : ShellSetupState . NotSetup ;
238259 } catch ( err ) {
239260 traceError ( 'Failed to check if CMD startup is setup' , err ) ;
@@ -249,8 +270,8 @@ export class CmdStartupProvider implements ShellStartupProvider {
249270 }
250271
251272 try {
252- const { startupFile , mainBatchFile } = await getCmdFilePaths ( ) ;
253- const success = await setupCmdStartup ( startupFile , mainBatchFile , this . cmdActivationEnvVarKey ) ;
273+ const cmdFiles = await getCmdFilePaths ( ) ;
274+ const success = await setupCmdStartup ( cmdFiles , this . cmdActivationEnvVarKey ) ;
254275 return success ? ShellScriptEditState . Edited : ShellScriptEditState . NotEdited ;
255276 } catch ( err ) {
256277 traceError ( 'Failed to setup CMD startup' , err ) ;
0 commit comments