@@ -12,7 +12,7 @@ type PropertyDescription = {
1212 description ?: string ;
1313 default ?: string | number | boolean | string [ ] ;
1414 $ref ?: string ;
15- $default ?: { $source : 'argv' ; index : number } ;
15+ $default ?: { $source : 'argv' ; index : number } | { $source : 'projectName' } ;
1616 'x-prompt' ?: string | { message : string ; type : string ; items : any [ ] } ;
1717} ;
1818
@@ -261,6 +261,7 @@ function setPropertyDefault(
261261 if ( schema . $ref ) {
262262 schema = resolveDefinition ( schema . $ref , definitions ) ;
263263 }
264+
264265 if ( schema . type !== 'object' && schema . type !== 'array' ) {
265266 if ( opts [ propName ] === undefined && schema . default !== undefined ) {
266267 opts [ propName ] = schema . default ;
@@ -297,28 +298,12 @@ function resolveDefinition(ref: string, definitions: Properties) {
297298 return definitions [ definition ] ;
298299}
299300
300- export function convertPositionParamsIntoNamedParams (
301- opts : { [ k : string ] : any } ,
302- schema : Schema ,
303- argv : string [ ]
304- ) {
305- Object . entries ( schema . properties ) . forEach ( ( [ k , v ] ) => {
306- if (
307- opts [ k ] === undefined &&
308- v . $default !== undefined &&
309- argv [ v . $default . index ]
310- ) {
311- opts [ k ] = coerceType ( v , argv [ v . $default . index ] ) ;
312- }
313- } ) ;
314- delete opts [ '_' ] ;
315- }
316-
317301export function combineOptionsForExecutor (
318302 commandLineOpts : Options ,
319303 config : string ,
320304 target : TargetConfiguration ,
321- schema : Schema
305+ schema : Schema ,
306+ defaultProjectName : string | null
322307) {
323308 const r = convertAliases (
324309 coerceTypesInOptions ( commandLineOpts , schema ) ,
@@ -328,10 +313,11 @@ export function combineOptionsForExecutor(
328313 const configOpts =
329314 config && target . configurations ? target . configurations [ config ] || { } : { } ;
330315 const combined = { ...target . options , ...configOpts , ...r } ;
331- convertPositionParamsIntoNamedParams (
316+ convertSmartDefaultsIntoNamedParams (
332317 combined ,
333318 schema ,
334- ( commandLineOpts [ '_' ] as string [ ] ) || [ ]
319+ ( commandLineOpts [ '_' ] as string [ ] ) || [ ] ,
320+ defaultProjectName
335321 ) ;
336322 setDefaults ( combined , schema ) ;
337323 validateOptsAgainstSchema ( combined , schema ) ;
@@ -342,50 +328,105 @@ export async function combineOptionsForGenerator(
342328 commandLineOpts : Options ,
343329 collectionName : string ,
344330 generatorName : string ,
345- ws : WorkspaceConfiguration | null ,
331+ wc : WorkspaceConfiguration | null ,
346332 schema : Schema ,
347- isInteractive : boolean
333+ isInteractive : boolean ,
334+ defaultProjectName : string | null
348335) {
349- const generatorDefaults = ws
350- ? getGeneratorDefaults ( ws , collectionName , generatorName )
336+ const generatorDefaults = wc
337+ ? getGeneratorDefaults (
338+ defaultProjectName ,
339+ wc ,
340+ collectionName ,
341+ generatorName
342+ )
351343 : { } ;
352344 let combined = convertAliases (
353345 coerceTypesInOptions ( { ...generatorDefaults , ...commandLineOpts } , schema ) ,
354346 schema ,
355347 false
356348 ) ;
357- convertPositionParamsIntoNamedParams (
349+ convertSmartDefaultsIntoNamedParams (
358350 combined ,
359351 schema ,
360- ( commandLineOpts [ '_' ] as string [ ] ) || [ ]
352+ ( commandLineOpts [ '_' ] as string [ ] ) || [ ] ,
353+ defaultProjectName
361354 ) ;
355+
362356 if ( isInteractive && isTTY ( ) ) {
363357 combined = await promptForValues ( combined , schema ) ;
364358 }
359+
365360 setDefaults ( combined , schema ) ;
361+
366362 validateOptsAgainstSchema ( combined , schema ) ;
367363 return combined ;
368364}
369365
366+ export function convertSmartDefaultsIntoNamedParams (
367+ opts : { [ k : string ] : any } ,
368+ schema : Schema ,
369+ argv : string [ ] ,
370+ defaultProjectName : string | null
371+ ) {
372+ Object . entries ( schema . properties ) . forEach ( ( [ k , v ] ) => {
373+ if (
374+ opts [ k ] === undefined &&
375+ v . $default !== undefined &&
376+ v . $default . $source === 'argv' &&
377+ argv [ v . $default . index ]
378+ ) {
379+ opts [ k ] = coerceType ( v , argv [ v . $default . index ] ) ;
380+ } else if (
381+ opts [ k ] === undefined &&
382+ v . $default !== undefined &&
383+ v . $default . $source === 'projectName' &&
384+ defaultProjectName
385+ ) {
386+ opts [ k ] = defaultProjectName ;
387+ }
388+ } ) ;
389+ }
390+
370391function getGeneratorDefaults (
371- ws : WorkspaceConfiguration ,
392+ projectName : string | null ,
393+ wc : WorkspaceConfiguration ,
372394 collectionName : string ,
373395 generatorName : string
374396) {
375- if ( ! ws . generators ) return { } ;
376-
377397 let defaults = { } ;
398+ if ( wc . generators ) {
399+ if (
400+ wc . generators [ collectionName ] &&
401+ wc . generators [ collectionName ] [ generatorName ]
402+ ) {
403+ defaults = {
404+ ...defaults ,
405+ ...wc . generators [ collectionName ] [ generatorName ] ,
406+ } ;
407+ }
408+ if ( wc . generators [ `${ collectionName } :${ generatorName } ` ] ) {
409+ defaults = {
410+ ...defaults ,
411+ ...wc . generators [ `${ collectionName } :${ generatorName } ` ] ,
412+ } ;
413+ }
414+ }
378415 if (
379- ws . generators [ collectionName ] &&
380- ws . generators [ collectionName ] [ generatorName ]
416+ projectName &&
417+ wc . projects [ projectName ] &&
418+ wc . projects [ projectName ] . generators
381419 ) {
382- defaults = { ...defaults , ...ws . generators [ collectionName ] [ generatorName ] } ;
383- }
384- if ( ws . generators [ `${ collectionName } :${ generatorName } ` ] ) {
385- defaults = {
386- ...defaults ,
387- ...ws . generators [ `${ collectionName } :${ generatorName } ` ] ,
388- } ;
420+ const g = wc . projects [ projectName ] . generators ;
421+ if ( g [ collectionName ] && g [ collectionName ] [ generatorName ] ) {
422+ defaults = { ...defaults , ...g [ collectionName ] [ generatorName ] } ;
423+ }
424+ if ( g [ `${ collectionName } :${ generatorName } ` ] ) {
425+ defaults = {
426+ ...defaults ,
427+ ...g [ `${ collectionName } :${ generatorName } ` ] ,
428+ } ;
429+ }
389430 }
390431 return defaults ;
391432}
0 commit comments