@@ -43,13 +43,60 @@ module.exports = class extends Generator {
43
43
message :
44
44
"We need the build file (generally index.js, main.js or componentName.js) for this, import it using one of the options -" ,
45
45
choices : [
46
- "Tell us the path of the file on your local machine and we will import it in the project." ,
47
- "Tell us the npm package name, version, etc. and we will import it."
46
+ "Install component from npm package (Recommended - fastest way)" ,
47
+ "Tell us the path of the build file on your local machine and we will import it in the project." ,
48
+ "Tell us the npm package name, version, build file URL and we will download the build file."
48
49
] ,
49
50
default : 0
50
51
}
51
52
] ;
52
53
54
+ const installNpmPackagePrompts = [
55
+ {
56
+ type : "input" ,
57
+ name : "packageNameToInstallComponent" ,
58
+ message : "Enter the package name (case sensitive)." ,
59
+ validate : validators . packageName
60
+ } ,
61
+ {
62
+ type : "confirm" ,
63
+ name : "confirmPackageNameToInstallComponent" ,
64
+ message : "Press enter if the package description shown is correct."
65
+ } ,
66
+ {
67
+ type : "rawlist" ,
68
+ name : "changeImportSourceFromNpmPackage" ,
69
+ message : "What do you want to do?" ,
70
+ choices : [
71
+ "Enter package name again to install component from npm package." ,
72
+ "Import the file locally from your computer." ,
73
+ "Enter package name, version, build file URL to download the build file."
74
+ ] ,
75
+ when : function ( responses ) {
76
+ if ( responses . confirmPackageNameToInstallComponent ) {
77
+ return false ; // Don't show this prompt, if user says that package description is correct.
78
+ }
79
+
80
+ return true ; // Show this prompt if user says that package description is not correct.
81
+ }
82
+ } ,
83
+ {
84
+ type : "input" ,
85
+ name : "checkVersionAndInstallComponent" ,
86
+ message :
87
+ "Great! We will import the latest version of the npm package, if you don't want this, enter the version." ,
88
+ default : "latest" ,
89
+ when : function ( responses ) {
90
+ if ( responses . confirmPackageNameToInstallComponent ) {
91
+ return true ; // Show this prompt if user says that package description is correct
92
+ }
93
+
94
+ return false ; // Don't show this prompt if user says that package description is incorrect
95
+ } ,
96
+ validate : validators . checkVersionAndInstallComponent
97
+ }
98
+ ] ;
99
+
53
100
// Prompts if user chooses to import file(s) using npm
54
101
const npmPrompts = [
55
102
{
@@ -65,11 +112,12 @@ module.exports = class extends Generator {
65
112
} ,
66
113
{
67
114
type : "rawlist" ,
68
- name : "changeImportSource " ,
115
+ name : "changeImportSourceFromNpmBuildFile " ,
69
116
message : "What do you want to do?" ,
70
117
choices : [
71
- "Enter package name again." ,
72
- "Import the file locally from your computer."
118
+ "Enter package name again to install component from npm package." ,
119
+ "Import the file locally from your computer." ,
120
+ "Enter package name, version, build file URL to download the build file."
73
121
] ,
74
122
when : function ( responses ) {
75
123
if ( responses . confirmPackageName ) {
@@ -190,18 +238,55 @@ module.exports = class extends Generator {
190
238
191
239
/** Interacts with the user using prompts
192
240
* Recursive so that user can go to a previous step and/or change method of importing file
193
- * @param {string } repeatLocalOrNpmPrompts tells the generator which prompts should be shown again (localPrompts or npmPrompts)
241
+ * @param {string } repeatPrompts tells the generator which prompts should be shown again (installNpmPackagePrompts or localPrompts or npmPrompts)
194
242
* @returns {Promise } to execute prompts and wait for there execution to finish
195
243
*/
196
- const recursivePromptExecution = repeatLocalOrNpmPrompts => {
244
+ const recursivePromptExecution = repeatPrompts => {
197
245
// If user changes the method of importing later, recursive execution
198
- if ( repeatLocalOrNpmPrompts ) {
246
+ if ( repeatPrompts ) {
199
247
// If user chooses to enter package name again when package description is incorrect
200
- if ( repeatLocalOrNpmPrompts === npmPrompts [ 2 ] . choices [ 0 ] ) {
248
+ if (
249
+ repeatPrompts === npmPrompts [ 2 ] . choices [ 0 ] ||
250
+ repeatPrompts === installNpmPackagePrompts [ 2 ] . choices [ 0 ]
251
+ ) {
252
+ return this . prompt ( installNpmPackagePrompts ) . then ( props => {
253
+ // If user chooses to go back and choose source of importing file again
254
+ if ( props . changeImportSourceFromNpmPackage ) {
255
+ return recursivePromptExecution (
256
+ props . changeImportSourceFromNpmPackage
257
+ ) ; // Call the function recursively
258
+ }
259
+
260
+ // If user chooses to install component from npm after starting over
261
+ return this . prompt ( commonPrompts ) . then ( props => {
262
+ this . props = props ;
263
+ this . props . toolNameCamel = toCamelCase ( props . toolNameHuman ) ;
264
+ } ) ;
265
+ } ) ;
266
+ }
267
+
268
+ if (
269
+ repeatPrompts === npmPrompts [ 2 ] . choices [ 1 ] ||
270
+ repeatPrompts === installNpmPackagePrompts [ 2 ] . choices [ 1 ]
271
+ ) {
272
+ return this . prompt ( localPrompts ) . then ( ( ) => {
273
+ return this . prompt ( commonPrompts ) . then ( props => {
274
+ this . props = props ;
275
+ this . props . toolNameCamel = toCamelCase ( props . toolNameHuman ) ;
276
+ } ) ;
277
+ } ) ;
278
+ }
279
+
280
+ if (
281
+ repeatPrompts === npmPrompts [ 2 ] . choices [ 2 ] ||
282
+ repeatPrompts === installNpmPackagePrompts [ 2 ] . choices [ 2 ]
283
+ ) {
201
284
return this . prompt ( npmPrompts ) . then ( props => {
202
285
// If user chooses to go back and choose source of importing file again
203
- if ( props . changeImportSource ) {
204
- return recursivePromptExecution ( props . changeImportSource ) ; // Call the function recursively
286
+ if ( props . changeImportSourceFromNpmBuildFile ) {
287
+ return recursivePromptExecution (
288
+ props . changeImportSourceFromNpmBuildFile
289
+ ) ; // Call the function recursively
205
290
}
206
291
207
292
// If user chooses to import from npm after starting over
@@ -229,6 +314,23 @@ module.exports = class extends Generator {
229
314
return this . prompt ( upgradeComponentPrompts ) . then ( props => {
230
315
// If user chooses to import file locally from computer
231
316
if ( props . importFrom === upgradeComponentPrompts [ 0 ] . choices [ 0 ] ) {
317
+ return this . prompt ( installNpmPackagePrompts ) . then ( props => {
318
+ // If user chooses to go back and choose source of importing file again
319
+ if ( props . changeImportSourceFromNpmPackage ) {
320
+ return recursivePromptExecution (
321
+ props . changeImportSourceFromNpmPackage
322
+ ) ; // Call the function recursively
323
+ }
324
+
325
+ // If user chooses to install component from npm initially
326
+ return this . prompt ( commonPrompts ) . then ( props => {
327
+ this . props = props ;
328
+ this . props . toolNameCamel = toCamelCase ( props . toolNameHuman ) ;
329
+ } ) ;
330
+ } ) ;
331
+ }
332
+
333
+ if ( props . importFrom === upgradeComponentPrompts [ 0 ] . choices [ 1 ] ) {
232
334
return this . prompt ( localPrompts ) . then ( ( ) => {
233
335
return this . prompt ( commonPrompts ) . then ( props => {
234
336
this . props = props ;
@@ -237,19 +339,23 @@ module.exports = class extends Generator {
237
339
} ) ;
238
340
}
239
341
240
- // If user chooses to import file from npm
241
- return this . prompt ( npmPrompts ) . then ( props => {
242
- // If user chooses to go back and choose source of importing file again
243
- if ( props . changeImportSource ) {
244
- return recursivePromptExecution ( props . changeImportSource ) ; // Call the function recursively
245
- }
246
-
247
- // If user chooses to import from npm initially
248
- return this . prompt ( commonPrompts ) . then ( props => {
249
- this . props = props ;
250
- this . props . toolNameCamel = toCamelCase ( props . toolNameHuman ) ;
342
+ if ( props . importFrom === upgradeComponentPrompts [ 0 ] . choices [ 2 ] ) {
343
+ // If user chooses to import file from npm
344
+ return this . prompt ( npmPrompts ) . then ( props => {
345
+ // If user chooses to go back and choose source of importing file again
346
+ if ( props . changeImportSourceFromNpmBuildFile ) {
347
+ return recursivePromptExecution (
348
+ props . changeImportSourceFromNpmBuildFile
349
+ ) ; // Call the function recursively
350
+ }
351
+
352
+ // If user chooses to import from npm initially
353
+ return this . prompt ( commonPrompts ) . then ( props => {
354
+ this . props = props ;
355
+ this . props . toolNameCamel = toCamelCase ( props . toolNameHuman ) ;
356
+ } ) ;
251
357
} ) ;
252
- } ) ;
358
+ }
253
359
} ) ;
254
360
}
255
361
0 commit comments