Skip to content

Commit 7481974

Browse files
committedJun 5, 2019
chore: create questions.ts
1 parent cf8e3c9 commit 7481974

File tree

4 files changed

+64
-89
lines changed

4 files changed

+64
-89
lines changed
 

‎packages/generators/add-generator.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
import * as Generator from "yeoman-generator";
2+
import npmExists from "@webpack-cli/utils/npm-exists";
3+
import { getPackageManager } from "@webpack-cli/utils/package-manager";
4+
import { Input, List } from "@webpack-cli/webpack-scaffold";
5+
// eslint-disable-next-line
6+
export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json");
7+
import * as AutoComplete from "inquirer-autocomplete-prompt";
8+
import path, { resolve } from "path";
9+
import glob from "glob-all";
210

311
import {
412
actionTypeQuestion,
@@ -7,19 +15,12 @@ import {
715
mergeFileQuestion,
816
topScopeQuestion
917
} from "./utils/add/questions";
10-
11-
import { traverseAndGetProperties, webpackDevServerSchema, webpackSchema } from "./utils/add";
12-
13-
import npmExists from "@webpack-cli/utils/npm-exists";
14-
import { getPackageManager } from "@webpack-cli/utils/package-manager";
15-
import { Input, List } from "@webpack-cli/webpack-scaffold";
16-
18+
import { traverseAndGetProperties } from "./utils/add";
1719
import { SchemaProperties, WebpackOptions } from "./types";
1820
import entryQuestions from "./utils/entry";
19-
import * as AutoComplete from "inquirer-autocomplete-prompt";
20-
import path, { resolve } from "path";
21-
import glob from "glob-all";
2221
import { generatePluginName } from "./utils/plugins";
22+
// eslint-disable-next-line
23+
export const webpackSchema = require("../optionsSchema.json");
2324

2425
/**
2526
*
@@ -105,7 +106,6 @@ export default class AddGenerator extends Generator {
105106
return this.prompt(mergeFileQuestion).then(
106107
(mergeFileAnswer: { mergeFile: string; mergeConfigName: string }): void => {
107108
const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile);
108-
// eslint-disable-next-line
109109
this.configuration.config[action] = [mergeFileAnswer.mergeConfigName, resolvedPath];
110110
done();
111111
}

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

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

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

5-
// tslint:disable:no-var-requires
6-
// eslint-disable-next-line
7-
export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json");
8-
// eslint-disable-next-line
9-
export const webpackSchema = require("../optionsSchema.json");
10-
115
/**
126
*
137
* Replaces the string with a substring at the given index
@@ -36,11 +30,13 @@ export function replaceAt(str: string, index: number, replace: string): string {
3630
*/
3731
export const traverseAndGetProperties = (arr: object[], prop: string): boolean => {
3832
let hasProp = false;
39-
arr.forEach((p: object): void => {
40-
if (p[prop]) {
41-
hasProp = true;
33+
arr.forEach(
34+
(p: object): void => {
35+
if (p[prop]) {
36+
hasProp = true;
37+
}
4238
}
43-
});
39+
);
4440
return hasProp;
4541
};
4642

@@ -56,9 +52,5 @@ export const traverseAndGetProperties = (arr: object[], prop: string): boolean =
5652
*/
5753
export const searchProps = (answers: object, input: string): Promise<string[]> => {
5854
input = input || "";
59-
return Promise.resolve(
60-
PROPS.filter((prop: string): boolean =>
61-
prop.toLowerCase().includes(input.toLowerCase()),
62-
),
63-
);
55+
return Promise.resolve(PROPS.filter((prop: string): boolean => prop.toLowerCase().includes(input.toLowerCase())));
6456
};
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { AutoComplete, Confirm, Input, InputValidate } from "@webpack-cli/webpack-scaffold";
2+
import { existsSync } from "fs";
3+
import { resolve } from "path";
4+
import { searchProps } from "./index";
5+
import { Question } from "inquirer";
6+
7+
/**
8+
* Returns Inquirer question for given action
9+
* @param {string} action action for which question has to be prompted
10+
* @returns {Question} Question for given action
11+
*/
12+
export const manualOrListInput = (action: string): Question => {
13+
const actionQuestion = `What do you want to add to ${action}?`;
14+
return Input("actionAnswer", actionQuestion);
15+
};
16+
17+
export const actionTypeQuestion = AutoComplete("actionType", "What property do you want to add to?", {
18+
pageSize: 7,
19+
source: searchProps,
20+
suggestOnly: false
21+
});
22+
23+
export const entryTypeQuestion: Question = Confirm("entryType", "Will your application have multiple bundles?", false);
24+
25+
export const topScopeQuestion: Question = Input("topScope", "What do you want to add to topScope?");
26+
27+
const mergeFileQuestionsFunction = (): Question[] => {
28+
const mergePathQuestion =
29+
"What is the location of webpack configuration with which you want to merge current configuration?";
30+
const mergePathValidator = (path: string): boolean | string => {
31+
const resolvedPath = resolve(process.cwd(), path);
32+
if (existsSync(resolvedPath)) {
33+
if (/\.js$/.test(path)) {
34+
return true;
35+
}
36+
return "Path doesn't corresponds to a javascript file";
37+
}
38+
return "Invalid path provided";
39+
};
40+
const mergeConfigNameQuestion = "What is the name by which you want to denote above configuration?";
41+
return [
42+
InputValidate("mergeFile", mergePathQuestion, mergePathValidator),
43+
Input("mergeConfigName", mergeConfigNameQuestion)
44+
];
45+
};
46+
export const mergeFileQuestion: Question[] = mergeFileQuestionsFunction();

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

-63
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.