/
add-generator.ts
495 lines (478 loc) · 16.7 KB
/
add-generator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
import * as Generator from "yeoman-generator";
import * as glob from "glob-all";
import * as autoComplete from "inquirer-autocomplete-prompt";
import * as path from "path";
import npmExists from "@webpack-cli/utils/npm-exists";
import { getPackageManager } from "@webpack-cli/utils/package-manager";
import PROP_TYPES from "@webpack-cli/utils/prop-types";
import { AutoComplete, Confirm, Input, List } from "@webpack-cli/webpack-scaffold";
import { SchemaProperties, WebpackOptions } from "./types";
import entryQuestions from "./utils/entry";
import webpackDevServerSchema from "webpack-dev-server/lib/options.json";
import webpackSchema from "./utils/optionsSchema.json";
const PROPS: string[] = Array.from(PROP_TYPES.keys());
/**
*
* Replaces the string with a substring at the given index
* https://gist.github.com/efenacigiray/9367920
*
* @param {String} str - string to be modified
* @param {Number} index - index to replace from
* @param {String} replace - string to replace starting from index
*
* @returns {String} string - The newly mutated string
*
*/
function replaceAt(str: string, index: number, replace: string): string {
return str.substring(0, index) + replace + str.substring(index + 1);
}
/**
*
* Checks if the given array has a given property
*
* @param {Array} arr - array to check
* @param {String} prop - property to check existence of
*
* @returns {Boolean} hasProp - Boolean indicating if the property
* is present
*/
const traverseAndGetProperties = (arr: object[], prop: string): boolean => {
let hasProp = false;
arr.forEach(
(p: object): void => {
if (p[prop]) {
hasProp = true;
}
}
);
return hasProp;
};
/**
*
* Search config properties
*
* @param {Object} answers Prompt answers object
* @param {String} input Input search string
*
* @returns {Promise} Returns promise which resolves to filtered props
*
*/
const searchProps = (answers: object, input: string): Promise<string[]> => {
input = input || "";
return Promise.resolve(PROPS.filter((prop: string): boolean => prop.toLowerCase().includes(input.toLowerCase())));
};
/**
*
* Generator for adding properties
* @class AddGenerator
* @extends Generator
* @returns {Void} After execution, transforms are triggered
*
*/
export default class AddGenerator extends Generator {
private dependencies: string[];
private configuration: {
config: {
configName?: string;
topScope?: string[];
item?: string;
webpackOptions?: WebpackOptions;
};
};
public constructor(args, opts) {
super(args, opts);
this.dependencies = [];
this.configuration = {
config: {
topScope: ["const webpack = require('webpack')"],
webpackOptions: {}
}
};
const { registerPrompt } = this.env.adapter.promptModule;
registerPrompt("autocomplete", autoComplete);
}
public prompting(): Promise<void | {}> {
const done: () => {} = this.async();
let action: string;
const self: this = this;
const manualOrListInput: (promptAction: string) => Generator.Question = (promptAction: string): Generator.Question =>
Input("actionAnswer", `What do you want to add to ${promptAction}?`);
let inputPrompt: Generator.Question;
// first index indicates if it has a deep prop, 2nd indicates what kind of
// TODO: this must be reviewed. It starts as an array of booleans but after that it get overridden
// Bye bye functional programming.
// eslint-disable-next-line
const isDeepProp: any[] = [false, false];
return this.prompt([
AutoComplete("actionType", "What property do you want to add to?", {
pageSize: 7,
source: searchProps,
suggestOnly: false
})
])
.then(
(actionTypeAnswer: { actionType: string }): void => {
// Set initial prop, like devtool
this.configuration.config.webpackOptions[actionTypeAnswer.actionType] = null;
// update the action variable, we're using it later
action = actionTypeAnswer.actionType;
}
)
.then(
(): Promise<void | {}> => {
if (action === "entry") {
return this.prompt([
Confirm("entryType", "Will your application have multiple bundles?", false)
])
.then(
(entryTypeAnswer: { entryType: boolean }): Promise<void | {}> => {
// Ask different questions for entry points
return entryQuestions(self, entryTypeAnswer);
}
)
.then(
(entryOptions: { entryType: boolean }): void => {
this.configuration.config.webpackOptions.entry = entryOptions;
this.configuration.config.item = action;
}
);
} else {
if (action === "topScope") {
return this.prompt([Input("topScope", "What do you want to add to topScope?")]).then(
(topScopeAnswer: { topScope: string }): void => {
this.configuration.config.topScope.push(topScopeAnswer.topScope);
done();
}
);
}
}
const temp = action;
if (action === "resolveLoader") {
action = "resolve";
}
const webpackSchemaProp: SchemaProperties = webpackSchema.definitions[action];
/*
* https://github.com/webpack/webpack/blob/next/schemas/WebpackOptions.json
* Find the properties directly in the properties prop, or the anyOf prop
*/
let defOrPropDescription: object = webpackSchemaProp
? webpackSchemaProp.properties
: webpackSchema.properties[action].properties
? webpackSchema.properties[action].properties
: webpackSchema.properties[action].anyOf
? webpackSchema.properties[action].anyOf.filter(
(p: { properties?: object; enum?: string[] }): boolean => !!p.properties || !!p.enum
)
: null;
if (Array.isArray(defOrPropDescription)) {
// Todo: Generalize these to go through the array, then merge enum with props if needed
const hasPropertiesProp: boolean = traverseAndGetProperties(defOrPropDescription, "properties");
const hasEnumProp: boolean = traverseAndGetProperties(defOrPropDescription, "enum");
/* as we know he schema only has two arrays that might hold our values,
* check them for either having arr.enum or arr.properties
*/
if (hasPropertiesProp) {
defOrPropDescription =
defOrPropDescription[0].properties || defOrPropDescription[1].properties;
if (!defOrPropDescription) {
defOrPropDescription = defOrPropDescription[0].enum;
}
// TODO: manually implement stats and devtools like sourcemaps
} else if (hasEnumProp) {
const originalPropDesc: object = defOrPropDescription[0].enum;
// Array -> Object -> Merge objects into one for compat in manualOrListInput
defOrPropDescription = Object.keys(defOrPropDescription[0].enum)
.map(
(p: string): object => {
return Object.assign(
{},
{
[originalPropDesc[p]]: "noop"
}
);
}
)
.reduce((result: object, currentObject: object): object => {
for (const key in currentObject) {
if (currentObject.hasOwnProperty(key)) {
result[key] = currentObject[key];
}
}
return result;
}, {});
}
}
// WDS has its own schema, so we gonna need to check that too
const webpackDevserverSchemaProp: SchemaProperties =
action === "devServer" ? webpackDevServerSchema : null;
// Watch has a boolean arg, but we need to append to it manually
if (action === "watch") {
defOrPropDescription = {
false: {},
true: {}
};
}
if (action === "mode") {
defOrPropDescription = {
development: {},
production: {}
};
}
action = temp;
if (action === "resolveLoader") {
defOrPropDescription = Object.assign(defOrPropDescription, {
moduleExtensions: {}
});
}
// If we've got a schema prop or devServer Schema Prop
if (defOrPropDescription || webpackDevserverSchemaProp) {
// Check for properties in definitions[action] or properties[action]
if (defOrPropDescription) {
if (action !== "devtool") {
// Add the option of adding an own variable if the user wants
defOrPropDescription = Object.assign(defOrPropDescription, {
other: {}
});
} else {
// The schema doesn't have the source maps we can prompt, so add those
defOrPropDescription = Object.assign(defOrPropDescription, {
"cheap-eval-source-map": {},
"cheap-module-eval-source-map": {},
"cheap-module-source-map": {},
"cheap-source-map": {},
eval: {},
"eval-source-map": {},
"hidden-source-map": {},
"inline-cheap-module-source-map": {},
"inline-cheap-source-map": {},
"inline-source-map": {},
"nosources-source-map": {},
"source-map": {}
});
}
inputPrompt = List(
"actionAnswer",
`What do you want to add to ${action}?`,
Object.keys(defOrPropDescription)
);
// We know we're gonna append some deep prop like module.rule
isDeepProp[0] = true;
} else if (webpackDevserverSchemaProp) {
// Append the custom property option
webpackDevserverSchemaProp.properties = Object.assign(
webpackDevserverSchemaProp.properties,
{
other: {}
}
);
inputPrompt = List(
"actionAnswer",
`What do you want to add to ${action}?`,
Object.keys(webpackDevserverSchemaProp.properties)
);
// We know we are in a devServer.prop scenario
isDeepProp[0] = true;
} else {
// manual input if non-existent
inputPrompt = manualOrListInput(action);
}
} else {
inputPrompt = manualOrListInput(action);
}
return this.prompt([inputPrompt]);
}
)
.then(
(answerToAction: { actionAnswer: string }): Promise<void | {}> => {
if (!answerToAction) {
done();
return;
}
/*
* Plugins got their own logic,
* find the names of each natively plugin and check if it matches
*/
if (action === "plugins") {
const pluginExist: string = glob
.sync(["node_modules/webpack/lib/*Plugin.js", "node_modules/webpack/lib/**/*Plugin.js"])
.map(
(p: string): string =>
p
.split("/")
.pop()
.replace(".js", "")
)
.find((p: string): boolean => p.toLowerCase().indexOf(answerToAction.actionAnswer) >= 0);
if (pluginExist) {
this.configuration.config.item = pluginExist;
const pluginsSchemaPath: string = glob
.sync([
"node_modules/webpack/schemas/plugins/*Plugin.json",
"node_modules/webpack/schemas/plugins/**/*Plugin.json"
])
.find(
(p: string): boolean =>
p
.split("/")
.pop()
.replace(".json", "")
.toLowerCase()
.indexOf(answerToAction.actionAnswer) >= 0
);
if (pluginsSchemaPath) {
const constructorPrefix: string =
pluginsSchemaPath.indexOf("optimize") >= 0 ? "webpack.optimize" : "webpack";
const resolvePluginsPath: string = path.resolve(pluginsSchemaPath);
const pluginSchema: object = resolvePluginsPath ? require(resolvePluginsPath) : null;
let pluginsSchemaProps: string[] = ["other"];
if (pluginSchema) {
Object.keys(pluginSchema)
.filter((p: string): boolean => Array.isArray(pluginSchema[p]))
.forEach(
(p: string): void => {
Object.keys(pluginSchema[p]).forEach(
(n: string): void => {
if (pluginSchema[p][n].properties) {
pluginsSchemaProps = Object.keys(
pluginSchema[p][n].properties
);
}
}
);
}
);
}
return this.prompt([
List(
"pluginsPropType",
`What property do you want to add ${pluginExist}?`,
pluginsSchemaProps
)
]).then(
(pluginsPropAnswer: { pluginsPropType: string }): Promise<void | {}> => {
return this.prompt([
Input(
"pluginsPropTypeVal",
`What value should ${pluginExist}.${
pluginsPropAnswer.pluginsPropType
} have?`
)
]).then(
(valForProp: { pluginsPropTypeVal: string }): void => {
this.configuration.config.webpackOptions[action] = {
[`${constructorPrefix}.${pluginExist}`]: {
[pluginsPropAnswer.pluginsPropType]:
valForProp.pluginsPropTypeVal
}
};
done();
}
);
}
);
} else {
this.configuration.config.webpackOptions[action] = `new webpack.${pluginExist}`;
done();
}
} else {
// If its not in webpack, check npm
npmExists(answerToAction.actionAnswer).then(
(p: boolean): void => {
if (p) {
this.dependencies.push(answerToAction.actionAnswer);
const normalizePluginName = answerToAction.actionAnswer.replace(
"-webpack-plugin",
"Plugin"
);
const pluginName = replaceAt(
normalizePluginName,
0,
normalizePluginName.charAt(0).toUpperCase()
);
this.configuration.config.topScope.push(
`const ${pluginName} = require("${answerToAction.actionAnswer}")`
);
this.configuration.config.webpackOptions[action] = `new ${pluginName}`;
this.configuration.config.item = answerToAction.actionAnswer;
done();
this.scheduleInstallTask(getPackageManager(), this.dependencies, {
"save-dev": true
});
} else {
console.error(
answerToAction.actionAnswer,
"doesn't exist on NPM or is built in webpack, please check for any misspellings."
);
process.exit(0);
}
}
);
}
} else {
// If we're in the scenario with a deep-property
if (isDeepProp[0]) {
isDeepProp[1] = answerToAction.actionAnswer;
if (
isDeepProp[1] !== "other" &&
(action === "devtool" || action === "watch" || action === "mode")
) {
this.configuration.config.item = action;
this.configuration.config.webpackOptions[action] = answerToAction.actionAnswer;
done();
return;
}
// Either we are adding directly at the property, else we're in a prop.theOne scenario
const actionMessage =
isDeepProp[1] === "other"
? `What do you want the key on ${action} to be? (press enter if you want it directly as a value on the property)`
: `What do you want the value of ${isDeepProp[1]} to be?`;
this.prompt([Input("deepProp", actionMessage)]).then(
(deepPropAns: { deepProp: string }): void => {
// The other option needs to be validated of either being empty or not
if (isDeepProp[1] === "other") {
const othersDeepPropKey: string = deepPropAns.deepProp
? `What do you want the value of ${deepPropAns.deepProp} to be?` // eslint-disable-line
: `What do you want to be the value of ${action} to be?`;
// Push the answer to the array we have created, so we can use it later
isDeepProp.push(deepPropAns.deepProp);
this.prompt([Input("innerProp", othersDeepPropKey)]).then(
(innerPropAns: { innerProp }): void => {
// Check length, if it has none, add the prop directly on the given action
if (isDeepProp[2].length === 0) {
this.configuration.config.item = action;
this.configuration.config.webpackOptions[action] =
innerPropAns.innerProp;
} else {
// If not, we're adding to something like devServer.myProp
this.configuration.config.item = action + "." + isDeepProp[2];
this.configuration.config.webpackOptions[action] = {
[isDeepProp[2]]: innerPropAns.innerProp
};
}
done();
}
);
} else {
// We got the schema prop, we've correctly prompted it, and can add it directly
this.configuration.config.item = `${action}.${isDeepProp[1]}`;
this.configuration.config.webpackOptions[action] = {
[isDeepProp[1]]: deepPropAns.deepProp
};
done();
}
}
);
} else {
// We're asking for input-only
this.configuration.config.item = action;
this.configuration.config.webpackOptions[action] = answerToAction.actionAnswer;
done();
}
}
}
);
}
public writing(): void {
this.config.set("configuration", this.configuration);
}
}