@@ -11,11 +11,17 @@ import {
11
11
toAbsoluteRootPath ,
12
12
} from './helpers' ;
13
13
import { platform } from 'os' ;
14
+ import { PluginResourceSettings } from './Settings' ;
14
15
15
16
export const DEBUG_CONFIG_PLATFORMS = [ 'windows' , 'linux' , 'osx' ] ;
16
17
const testNamePatternRegex = / \$ \{ j e s t .t e s t N a m e P a t t e r n \} / g;
17
18
const testFileRegex = / \$ \{ j e s t .t e s t F i l e \} / g;
18
19
const testFilePatternRegex = / \$ \{ j e s t .t e s t F i l e P a t t e r n \} / g;
20
+
21
+ export type DebugConfigOptions = Partial <
22
+ Pick < PluginResourceSettings , 'jestCommandLine' | 'rootPath' | 'nodeEnv' >
23
+ > ;
24
+ type PartialDebugConfig = Partial < vscode . DebugConfiguration > ;
19
25
export class DebugConfigurationProvider implements vscode . DebugConfigurationProvider {
20
26
private fileNameToRun = '' ;
21
27
private testToRun = '' ;
@@ -176,7 +182,7 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
176
182
}
177
183
178
184
/** return a config if cmd is a package-manager */
179
- private usePM ( cmd : string , args : string [ ] ) : Partial < vscode . DebugConfiguration | undefined > {
185
+ private usePM ( cmd : string , args : string [ ] ) : PartialDebugConfig | undefined {
180
186
const commonConfig = {
181
187
program : undefined ,
182
188
} ;
@@ -191,49 +197,58 @@ export class DebugConfigurationProvider implements vscode.DebugConfigurationProv
191
197
}
192
198
193
199
/**
194
- * generate a debug config incorporating commandLine and rootPath. Throw exception if error.
195
- * @param cmdLine
196
- * @param rootPath
197
- * @returns a debug config.
200
+ * Creates a debug configuration for a given workspace.
201
+ *
202
+ * @param {vscode.WorkspaceFolder } workspace - The workspace folder for which the debug configuration is created.
203
+ * @param {DebugConfigOptions } [options] - Optional parameters to override the default debug configuration.
204
+ * @returns {vscode.DebugConfiguration } The final debug configuration.
205
+ *
206
+ * @throws {Error } If the provided jestCommandLine is invalid.
207
+ *
208
+ * This function customizes the default debug configuration with the settings from the options parameter,
209
+ * such as `rootPath`, `jestCommandLine`, and `nodeEnv`.
210
+ * Please note, the platform-specific settings that were not converted are removed.
198
211
*/
199
- withCommandLine (
212
+ createDebugConfig (
200
213
workspace : vscode . WorkspaceFolder ,
201
- cmdLine : string ,
202
- rootPath ?: string
214
+ options ?: DebugConfigOptions
203
215
) : vscode . DebugConfiguration {
204
216
const config = this . provideDebugConfigurations ( workspace ) [ 0 ] ;
205
- const [ cmd , ...cmdArgs ] = parseCmdLine ( cmdLine ) ;
206
- if ( ! cmd ) {
207
- throw new Error ( `invalid cmdLine: ${ cmdLine } ` ) ;
208
- }
209
-
210
- const absoluteRootPath = rootPath && toAbsoluteRootPath ( workspace , rootPath ) ;
211
-
212
- let finalConfig : vscode . DebugConfiguration = { ...config } ;
217
+ let args : string [ ] = [ ] ;
218
+ let override : PartialDebugConfig = { } ;
213
219
220
+ const absoluteRootPath = options ?. rootPath && toAbsoluteRootPath ( workspace , options . rootPath ) ;
214
221
const cwd = absoluteRootPath ?? config . cwd ;
215
222
216
- const pmConfig = this . usePM ( cmd , cmdArgs ) ;
217
- if ( pmConfig ) {
218
- const args = [ ...cmdArgs , ...pmConfig . args , ...config . args ] ;
219
- finalConfig = {
220
- ...finalConfig ,
221
- ...pmConfig ,
222
- cwd,
223
- args,
224
- } ;
225
- } else {
226
- // convert the cmd to absolute path
227
- let program = path . isAbsolute ( cmd )
228
- ? cmd
229
- : absoluteRootPath
230
- ? path . resolve ( absoluteRootPath , cmd )
231
- : [ '${workspaceFolder}' , cmd ] . join ( path . sep ) ;
232
- program = this . adjustProgram ( program ) ;
233
- const args = [ ...cmdArgs , ...config . args ] ;
234
- finalConfig = { ...finalConfig , cwd, program, args } ;
223
+ // handle jestCommandLine related overrides
224
+ if ( options ?. jestCommandLine ) {
225
+ const [ cmd , ...cmdArgs ] = parseCmdLine ( options . jestCommandLine ) ;
226
+ if ( ! cmd ) {
227
+ throw new Error ( `invalid cmdLine: ${ options . jestCommandLine } ` ) ;
228
+ }
229
+ const pmConfig = this . usePM ( cmd , cmdArgs ) ;
230
+ if ( pmConfig ) {
231
+ args = [ ...cmdArgs , ...pmConfig . args , ...config . args ] ;
232
+ override = { ...pmConfig , args } ;
233
+ } else {
234
+ let program = path . isAbsolute ( cmd )
235
+ ? cmd
236
+ : absoluteRootPath
237
+ ? path . resolve ( absoluteRootPath , cmd )
238
+ : [ '${workspaceFolder}' , cmd ] . join ( path . sep ) ;
239
+ program = this . adjustProgram ( program ) ;
240
+ args = [ ...cmdArgs , ...config . args ] ;
241
+ override = { program, args } ;
242
+ }
235
243
}
236
244
245
+ //handle nodeEnv
246
+ if ( options ?. nodeEnv ) {
247
+ override = { env : options . nodeEnv , ...override } ;
248
+ }
249
+
250
+ const finalConfig : vscode . DebugConfiguration = { ...config , cwd, ...override } ;
251
+
237
252
// delete platform specific settings since we did not convert them
238
253
DEBUG_CONFIG_PLATFORMS . forEach ( ( p ) => delete finalConfig [ p ] ) ;
239
254
0 commit comments