Skip to content

Commit 915c4ab

Browse files
committedMay 28, 2019
chore(refactor): move questions to utils
1 parent e839614 commit 915c4ab

File tree

4 files changed

+25
-2067
lines changed

4 files changed

+25
-2067
lines changed
 

‎packages/generators/add-generator.ts

+23-76
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,24 @@ import {
66
manualOrListInput,
77
mergeFileQuestion,
88
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+
1018

1119
import npmExists from "@webpack-cli/utils/npm-exists";
1220
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";
1522

1623
import { SchemaProperties, WebpackOptions } from "./types";
1724
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";
7527

7628
/**
7729
*
@@ -103,15 +55,13 @@ export default class AddGenerator extends Generator {
10355
}
10456
};
10557
const { registerPrompt } = this.env.adapter.promptModule;
106-
registerPrompt("autocomplete", autoComplete);
58+
registerPrompt("autocomplete", AutoComplete);
10759
}
10860

10961
public prompting(): Promise<void | {}> {
11062
const done: () => {} = this.async();
11163
let action: string;
11264
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}?`);
11565
let inputPrompt: Generator.Question;
11666

11767
// first index indicates if it has a deep prop, 2nd indicates what kind of
@@ -120,13 +70,7 @@ export default class AddGenerator extends Generator {
12070
// eslint-disable-next-line
12171
const isDeepProp: any[] = [false, false];
12272

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)
13074
.then(
13175
(actionTypeAnswer: { actionType: string }): void => {
13276
// Set initial prop, like devtool
@@ -138,9 +82,7 @@ export default class AddGenerator extends Generator {
13882
.then(
13983
(): Promise<void | {}> => {
14084
if (action === "entry") {
141-
return this.prompt([
142-
Confirm("entryType", "Will your application have multiple bundles?", false)
143-
])
85+
return this.prompt(entryTypeQuestion)
14486
.then(
14587
(entryTypeAnswer: { entryType: boolean }): Promise<void | {}> => {
14688
// Ask different questions for entry points
@@ -155,7 +97,7 @@ export default class AddGenerator extends Generator {
15597
);
15698
} else {
15799
if (action === "topScope") {
158-
return this.prompt([Input("topScope", "What do you want to add to topScope?")]).then(
100+
return this.prompt(topScopeQuestion).then(
159101
(topScopeAnswer: { topScope: string }): void => {
160102
this.configuration.config.topScope.push(topScopeAnswer.topScope);
161103
done();
@@ -169,7 +111,12 @@ export default class AddGenerator extends Generator {
169111
}) => {
170112
const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile);
171113
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;
173120
});
174121
}
175122
}
@@ -446,7 +393,7 @@ export default class AddGenerator extends Generator {
446393
(action === "devtool" || action === "watch" || action === "mode")
447394
) {
448395
this.configuration.config.item = action;
449-
this.configuration.config.webpackOptions[action] = answerToAction.actionAnswer;
396+
(this.configuration.config.webpackOptions[action] as string) = answerToAction.actionAnswer;
450397
done();
451398
return;
452399
}

‎packages/utils/generators/add/index.ts ‎packages/generators/utils/add/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import PROP_TYPES from "../../prop-types";
1+
import PROP_TYPES from "@webpack-cli/utils/prop-types";
22

3-
const PROPS: string[] = Array.from(PROP_TYPES.keys());
3+
export const PROPS: string[] = Array.from(PROP_TYPES.keys());
44

55
// tslint:disable:no-var-requires
66
export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json");

‎packages/utils/generators/optionsSchema.json

-1,989
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.