1+ import {
2+ replaceAt ,
3+ traverseAndGetProperties ,
4+ webpackDevServerSchema ,
5+ webpackSchema ,
6+ } from "@webpack-cli/utils/generators/add" ;
7+
8+ import {
9+ actionTypeQuestion ,
10+ entryTypeQuestion ,
11+ manualOrListInput ,
12+ mergeFileQuestion ,
13+ topScopeQuestion ,
14+ } from "@webpack-cli/utils/generators/add/questions" ;
15+
116import npmExists from "@webpack-cli/utils/npm-exists" ;
217import { getPackageManager } from "@webpack-cli/utils/package-manager" ;
3- import PROP_TYPES from "@webpack-cli/utils/prop-types" ;
418import {
519 AutoComplete ,
620 Confirm ,
@@ -18,66 +32,6 @@ import { ISchemaProperties, IWebpackOptions } from "./types";
1832import entryQuestions from "./utils/entry" ;
1933import validate from "./utils/validate" ;
2034
21- // tslint:disable:no-var-requires
22- const webpackDevServerSchema = require ( "webpack-dev-server/lib/options.json" ) ;
23- const webpackSchema = require ( "./utils/optionsSchema.json" ) ;
24- const PROPS : string [ ] = Array . from ( PROP_TYPES . keys ( ) ) ;
25-
26- /**
27- *
28- * Replaces the string with a substring at the given index
29- * https://gist.github.com/efenacigiray/9367920
30- *
31- * @param {String } string - string to be modified
32- * @param {Number } index - index to replace from
33- * @param {String } replace - string to replace starting from index
34- *
35- * @returns {String } string - The newly mutated string
36- *
37- */
38- function replaceAt ( str : string , index : number , replace : string ) : string {
39- return str . substring ( 0 , index ) + replace + str . substring ( index + 1 ) ;
40- }
41-
42- /**
43- *
44- * Checks if the given array has a given property
45- *
46- * @param {Array } arr - array to check
47- * @param {String } prop - property to check existence of
48- *
49- * @returns {Boolean } hasProp - Boolean indicating if the property
50- * is present
51- */
52- const traverseAndGetProperties = ( arr : object [ ] , prop : string ) : boolean => {
53- let hasProp : boolean = false ;
54- arr . forEach ( ( p : object ) : void => {
55- if ( p [ prop ] ) {
56- hasProp = true ;
57- }
58- } ) ;
59- return hasProp ;
60- } ;
61-
62- /**
63- *
64- * Search config properties
65- *
66- * @param {Object } answers Prompt answers object
67- * @param {String } input Input search string
68- *
69- * @returns {Promise } Returns promise which resolves to filtered props
70- *
71- */
72- const searchProps = ( answers : object , input : string ) : Promise < string [ ] > => {
73- input = input || "" ;
74- return Promise . resolve (
75- PROPS . filter ( ( prop : string ) : boolean =>
76- prop . toLowerCase ( ) . includes ( input . toLowerCase ( ) ) ,
77- ) ,
78- ) ;
79- } ;
80-
8135/**
8236 *
8337 * Generator for adding properties
@@ -94,7 +48,7 @@ export default class AddGenerator extends Generator {
9448 configName ?: string ,
9549 topScope ?: string [ ] ,
9650 item ?: string ,
97- merge ?: object ,
51+ merge ?: IWebpackOptions ,
9852 webpackOptions ?: IWebpackOptions ,
9953 } ,
10054 } ;
@@ -116,24 +70,12 @@ export default class AddGenerator extends Generator {
11670 const done : ( _ ?: void ) => void | boolean = this . async ( ) ;
11771 let action : string ;
11872 const self : this = this ;
119- const manualOrListInput : ( promptAction : string ) => IInquirerInput = ( promptAction : string ) =>
120- Input ( "actionAnswer" , `What do you want to add to ${ promptAction } ?` ) ;
12173 let inputPrompt : IInquirerInput ;
12274
12375 // first index indicates if it has a deep prop, 2nd indicates what kind of
12476 const isDeepProp : any [ ] = [ false , false ] ;
12577
126- return this . prompt ( [
127- AutoComplete (
128- "actionType" ,
129- "What property do you want to add to?" ,
130- {
131- pageSize : 7 ,
132- source : searchProps ,
133- suggestOnly : false ,
134- } ,
135- ) ,
136- ] )
78+ return this . prompt ( [ actionTypeQuestion ] )
13779 . then ( ( actionTypeAnswer : {
13880 actionType : string ,
13981 } ) => {
@@ -146,9 +88,7 @@ export default class AddGenerator extends Generator {
14688 } )
14789 . then ( ( _ : void ) => {
14890 if ( action === "entry" ) {
149- return this . prompt ( [
150- Confirm ( "entryType" , "Will your application have multiple bundles?" , false ) ,
151- ] )
91+ return this . prompt ( [ entryTypeQuestion ] )
15292 . then ( ( entryTypeAnswer : {
15393 entryType : boolean ,
15494 } ) => {
@@ -163,9 +103,7 @@ export default class AddGenerator extends Generator {
163103 } ) ;
164104 } else {
165105 if ( action === "topScope" ) {
166- return this . prompt ( [
167- Input ( "topScope" , "What do you want to add to topScope?" ) ,
168- ] )
106+ return this . prompt ( [ topScopeQuestion ] )
169107 . then ( ( topScopeAnswer : {
170108 topScope : string ;
171109 } ) => {
@@ -174,27 +112,7 @@ export default class AddGenerator extends Generator {
174112 } ) ;
175113 }
176114 if ( action === "merge" ) {
177- const validatePath = ( path : string ) => {
178- const resolvedPath = resolve ( process . env . PWD , path ) ;
179- if ( existsSync ( resolvedPath ) ) {
180- if ( / \. j s $ / . test ( path ) ) {
181- if ( typeof require ( resolvedPath ) !== "object" ) {
182- return "Given file doesn't export an Object" ;
183- }
184- return true ;
185- } else {
186- return "Path doesn't corresponds to a javascript file" ;
187- }
188- }
189- return "Invalid path provided" ;
190- } ;
191- return this . prompt ( [
192- InputValidate (
193- "mergeFile" ,
194- "What is the location of webpack configuration file with which you want to merge current configuration?" ,
195- validatePath ,
196- ) ,
197- ] )
115+ return this . prompt ( [ mergeFileQuestion ] )
198116 . then ( ( mergeFileAnswer : {
199117 mergeFile : string ;
200118 } ) => {
0 commit comments