@@ -6,72 +6,24 @@ import {
6
6
manualOrListInput ,
7
7
mergeFileQuestion ,
8
8
topScopeQuestion ,
9
- } from "@webpack-cli/utils/generators/add/questions" ;
9
+ } from "./utils/add/questions" ;
10
+
11
+ import {
12
+ replaceAt ,
13
+ traverseAndGetProperties ,
14
+ webpackDevServerSchema ,
15
+ webpackSchema
16
+ } from './utils/add'
17
+
10
18
11
19
import npmExists from "@webpack-cli/utils/npm-exists" ;
12
20
import { getPackageManager } from "@webpack-cli/utils/package-manager" ;
13
- import PROP_TYPES from "@webpack-cli/utils/prop-types" ;
14
- import { AutoComplete , Confirm , Input , List } from "@webpack-cli/webpack-scaffold" ;
21
+ import { Input , List } from "@webpack-cli/webpack-scaffold" ;
15
22
16
23
import { SchemaProperties , WebpackOptions } from "./types" ;
17
24
import entryQuestions from "./utils/entry" ;
18
-
19
- import webpackDevServerSchema from "webpack-dev-server/lib/options.json" ;
20
- import webpackSchema from "./utils/optionsSchema.json" ;
21
- const PROPS : string [ ] = Array . from ( PROP_TYPES . keys ( ) ) ;
22
-
23
- /**
24
- *
25
- * Replaces the string with a substring at the given index
26
- * https://gist.github.com/efenacigiray/9367920
27
- *
28
- * @param {String } str - string to be modified
29
- * @param {Number } index - index to replace from
30
- * @param {String } replace - string to replace starting from index
31
- *
32
- * @returns {String } string - The newly mutated string
33
- *
34
- */
35
- function replaceAt ( str : string , index : number , replace : string ) : string {
36
- return str . substring ( 0 , index ) + replace + str . substring ( index + 1 ) ;
37
- }
38
-
39
- /**
40
- *
41
- * Checks if the given array has a given property
42
- *
43
- * @param {Array } arr - array to check
44
- * @param {String } prop - property to check existence of
45
- *
46
- * @returns {Boolean } hasProp - Boolean indicating if the property
47
- * is present
48
- */
49
- const traverseAndGetProperties = ( arr : object [ ] , prop : string ) : boolean => {
50
- let hasProp = false ;
51
- arr . forEach (
52
- ( p : object ) : void => {
53
- if ( p [ prop ] ) {
54
- hasProp = true ;
55
- }
56
- }
57
- ) ;
58
- return hasProp ;
59
- } ;
60
-
61
- /**
62
- *
63
- * Search config properties
64
- *
65
- * @param {Object } answers Prompt answers object
66
- * @param {String } input Input search string
67
- *
68
- * @returns {Promise } Returns promise which resolves to filtered props
69
- *
70
- */
71
- const searchProps = ( answers : object , input : string ) : Promise < string [ ] > => {
72
- input = input || "" ;
73
- return Promise . resolve ( PROPS . filter ( ( prop : string ) : boolean => prop . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) ) ) ;
74
- } ;
25
+ import { AutoComplete } from "@webpack-cli/webpack-scaffold" ;
26
+ import { resolve } from "path" ;
75
27
76
28
/**
77
29
*
@@ -103,15 +55,13 @@ export default class AddGenerator extends Generator {
103
55
}
104
56
} ;
105
57
const { registerPrompt } = this . env . adapter . promptModule ;
106
- registerPrompt ( "autocomplete" , autoComplete ) ;
58
+ registerPrompt ( "autocomplete" , AutoComplete ) ;
107
59
}
108
60
109
61
public prompting ( ) : Promise < void | { } > {
110
62
const done : ( ) => { } = this . async ( ) ;
111
63
let action : string ;
112
64
const self : this = this ;
113
- const manualOrListInput : ( promptAction : string ) => Generator . Question = ( promptAction : string ) : Generator . Question =>
114
- Input ( "actionAnswer" , `What do you want to add to ${ promptAction } ?` ) ;
115
65
let inputPrompt : Generator . Question ;
116
66
117
67
// first index indicates if it has a deep prop, 2nd indicates what kind of
@@ -120,13 +70,7 @@ export default class AddGenerator extends Generator {
120
70
// eslint-disable-next-line
121
71
const isDeepProp : any [ ] = [ false , false ] ;
122
72
123
- return this . prompt ( [
124
- AutoComplete ( "actionType" , "What property do you want to add to?" , {
125
- pageSize : 7 ,
126
- source : searchProps ,
127
- suggestOnly : false
128
- } )
129
- ] )
73
+ return this . prompt ( actionTypeQuestion )
130
74
. then (
131
75
( actionTypeAnswer : { actionType : string } ) : void => {
132
76
// Set initial prop, like devtool
@@ -138,9 +82,7 @@ export default class AddGenerator extends Generator {
138
82
. then (
139
83
( ) : Promise < void | { } > => {
140
84
if ( action === "entry" ) {
141
- return this . prompt ( [
142
- Confirm ( "entryType" , "Will your application have multiple bundles?" , false )
143
- ] )
85
+ return this . prompt ( entryTypeQuestion )
144
86
. then (
145
87
( entryTypeAnswer : { entryType : boolean } ) : Promise < void | { } > => {
146
88
// Ask different questions for entry points
@@ -155,7 +97,7 @@ export default class AddGenerator extends Generator {
155
97
) ;
156
98
} else {
157
99
if ( action === "topScope" ) {
158
- return this . prompt ( [ Input ( "topScope" , "What do you want to add to topScope?" ) ] ) . then (
100
+ return this . prompt ( topScopeQuestion ) . then (
159
101
( topScopeAnswer : { topScope : string } ) : void => {
160
102
this . configuration . config . topScope . push ( topScopeAnswer . topScope ) ;
161
103
done ( ) ;
@@ -169,7 +111,12 @@ export default class AddGenerator extends Generator {
169
111
} ) => {
170
112
const resolvedPath = resolve ( process . cwd ( ) , mergeFileAnswer . mergeFile ) ;
171
113
const mergeConfig = require ( resolvedPath ) ;
172
- this . configuration . config . merge = mergeConfig ;
114
+ ( this . configuration . config . merge as {
115
+ configName ?: string ;
116
+ topScope ?: string [ ] ;
117
+ item ?: string ;
118
+ webpackOptions ?: WebpackOptions ;
119
+ } ) = mergeConfig ;
173
120
} ) ;
174
121
}
175
122
}
@@ -446,7 +393,7 @@ export default class AddGenerator extends Generator {
446
393
( action === "devtool" || action === "watch" || action === "mode" )
447
394
) {
448
395
this . configuration . config . item = action ;
449
- this . configuration . config . webpackOptions [ action ] = answerToAction . actionAnswer ;
396
+ ( this . configuration . config . webpackOptions [ action ] as string ) = answerToAction . actionAnswer ;
450
397
done ( ) ;
451
398
return ;
452
399
}
0 commit comments