@@ -35,7 +35,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
} ;
36
36
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
37
37
exports . main = main ;
38
- exports . loadEsmModule = loadEsmModule ;
39
38
// symbol polyfill must go first
40
39
require ( "symbol-observable" ) ;
41
40
const node_1 = require ( "@angular-devkit/core/node" ) ;
@@ -84,66 +83,82 @@ function _listSchematics(workflow, collectionName, logger) {
84
83
}
85
84
function _createPromptProvider ( ) {
86
85
return async ( definitions ) => {
87
- const questions = definitions . map ( ( definition ) => {
88
- const question = {
89
- name : definition . id ,
90
- message : definition . message ,
91
- default : definition . default ,
92
- } ;
93
- const validator = definition . validator ;
94
- if ( validator ) {
95
- question . validate = ( input ) => validator ( input ) ;
96
- // Filter allows transformation of the value prior to validation
97
- question . filter = async ( input ) => {
98
- for ( const type of definition . propertyTypes ) {
99
- let value ;
100
- switch ( type ) {
101
- case 'string' :
102
- value = String ( input ) ;
103
- break ;
104
- case 'integer' :
105
- case 'number' :
106
- value = Number ( input ) ;
107
- break ;
108
- default :
109
- value = input ;
110
- break ;
111
- }
112
- // Can be a string if validation fails
113
- const isValid = ( await validator ( value ) ) === true ;
114
- if ( isValid ) {
115
- return value ;
116
- }
117
- }
118
- return input ;
119
- } ;
120
- }
86
+ let prompts ;
87
+ const answers = { } ;
88
+ for ( const definition of definitions ) {
89
+ // Only load prompt package if needed
90
+ prompts ??= await Promise . resolve ( ) . then ( ( ) => __importStar ( require ( '@inquirer/prompts' ) ) ) ;
121
91
switch ( definition . type ) {
122
92
case 'confirmation' :
123
- return { ...question , type : 'confirm' } ;
93
+ answers [ definition . id ] = await prompts . confirm ( {
94
+ message : definition . message ,
95
+ default : definition . default ,
96
+ } ) ;
97
+ break ;
124
98
case 'list' :
125
- return {
126
- ...question ,
127
- type : definition . multiselect ? 'checkbox' : 'list' ,
128
- choices : definition . items &&
129
- definition . items . map ( ( item ) => {
130
- if ( typeof item == 'string' ) {
131
- return item ;
99
+ if ( ! definition . items ?. length ) {
100
+ continue ;
101
+ }
102
+ const choices = definition . items ?. map ( ( item ) => {
103
+ return typeof item == 'string'
104
+ ? {
105
+ name : item ,
106
+ value : item ,
107
+ }
108
+ : {
109
+ name : item . label ,
110
+ value : item . value ,
111
+ } ;
112
+ } ) ;
113
+ answers [ definition . id ] = await ( definition . multiselect ? prompts . checkbox : prompts . select ) ( {
114
+ message : definition . message ,
115
+ default : definition . default ,
116
+ choices,
117
+ } ) ;
118
+ break ;
119
+ case 'input' :
120
+ let finalValue ;
121
+ answers [ definition . id ] = await prompts . input ( {
122
+ message : definition . message ,
123
+ default : definition . default ,
124
+ async validate ( value ) {
125
+ if ( definition . validator === undefined ) {
126
+ return true ;
127
+ }
128
+ let lastValidation = false ;
129
+ for ( const type of definition . propertyTypes ) {
130
+ let potential ;
131
+ switch ( type ) {
132
+ case 'string' :
133
+ potential = String ( value ) ;
134
+ break ;
135
+ case 'integer' :
136
+ case 'number' :
137
+ potential = Number ( value ) ;
138
+ break ;
139
+ default :
140
+ potential = value ;
141
+ break ;
132
142
}
133
- else {
134
- return {
135
- name : item . label ,
136
- value : item . value ,
137
- } ;
143
+ lastValidation = await definition . validator ( potential ) ;
144
+ // Can be a string if validation fails
145
+ if ( lastValidation === true ) {
146
+ finalValue = potential ;
147
+ return true ;
138
148
}
139
- } ) ,
140
- } ;
141
- default :
142
- return { ...question , type : definition . type } ;
149
+ }
150
+ return lastValidation ;
151
+ } ,
152
+ } ) ;
153
+ // Use validated value if present.
154
+ // This ensures the correct type is inserted into the final schema options.
155
+ if ( finalValue !== undefined ) {
156
+ answers [ definition . id ] = finalValue ;
157
+ }
158
+ break ;
143
159
}
144
- } ) ;
145
- const { default : inquirer } = await loadEsmModule ( 'inquirer' ) ;
146
- return inquirer . prompt ( questions ) ;
160
+ }
161
+ return answers ;
147
162
} ;
148
163
}
149
164
function findUp ( names , from ) {
@@ -431,23 +446,3 @@ if (require.main === module) {
431
446
throw e ;
432
447
} ) ;
433
448
}
434
- /**
435
- * Lazily compiled dynamic import loader function.
436
- */
437
- let load ;
438
- /**
439
- * This uses a dynamic import to load a module which may be ESM.
440
- * CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
441
- * will currently, unconditionally downlevel dynamic import into a require call.
442
- * require calls cannot load ESM code and will result in a runtime error. To workaround
443
- * this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
444
- * Once TypeScript provides support for keeping the dynamic import this workaround can
445
- * be dropped.
446
- *
447
- * @param modulePath The path of the module to load.
448
- * @returns A Promise that resolves to the dynamically imported module.
449
- */
450
- function loadEsmModule ( modulePath ) {
451
- load ??= new Function ( 'modulePath' , `return import(modulePath);` ) ;
452
- return load ( modulePath ) ;
453
- }
0 commit comments