diff --git a/packages/generators/add-generator.ts b/packages/generators/add-generator.ts index cf366ed27db..78a26567e9b 100644 --- a/packages/generators/add-generator.ts +++ b/packages/generators/add-generator.ts @@ -1,4 +1,12 @@ import * as Generator from "yeoman-generator"; +import npmExists from "@webpack-cli/utils/npm-exists"; +import { getPackageManager } from "@webpack-cli/utils/package-manager"; +import { Input, List } from "@webpack-cli/webpack-scaffold"; +// eslint-disable-next-line +export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json"); +import * as AutoComplete from "inquirer-autocomplete-prompt"; +import path, { resolve } from "path"; +import glob from "glob-all"; import { actionTypeQuestion, @@ -7,19 +15,12 @@ import { mergeFileQuestion, topScopeQuestion } from "./utils/add/questions"; - -import { traverseAndGetProperties, webpackDevServerSchema, webpackSchema } from "./utils/add"; - -import npmExists from "@webpack-cli/utils/npm-exists"; -import { getPackageManager } from "@webpack-cli/utils/package-manager"; -import { Input, List } from "@webpack-cli/webpack-scaffold"; - +import { traverseAndGetProperties } from "./utils/add"; import { SchemaProperties, WebpackOptions } from "./types"; import entryQuestions from "./utils/entry"; -import * as AutoComplete from "inquirer-autocomplete-prompt"; -import path, { resolve } from "path"; -import glob from "glob-all"; import { generatePluginName } from "./utils/plugins"; +// eslint-disable-next-line +export const webpackSchema = require("../optionsSchema.json"); /** * @@ -105,7 +106,6 @@ export default class AddGenerator extends Generator { return this.prompt(mergeFileQuestion).then( (mergeFileAnswer: { mergeFile: string; mergeConfigName: string }): void => { const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile); - // eslint-disable-next-line this.configuration.config[action] = [mergeFileAnswer.mergeConfigName, resolvedPath]; done(); } diff --git a/packages/generators/utils/add/index.ts b/packages/generators/utils/add/index.ts index f9743d9ecd6..494271a20e7 100644 --- a/packages/generators/utils/add/index.ts +++ b/packages/generators/utils/add/index.ts @@ -2,12 +2,6 @@ import PROP_TYPES from "@webpack-cli/utils/prop-types"; export const PROPS: string[] = Array.from(PROP_TYPES.keys()); -// tslint:disable:no-var-requires -// eslint-disable-next-line -export const webpackDevServerSchema = require("webpack-dev-server/lib/options.json"); -// eslint-disable-next-line -export const webpackSchema = require("../optionsSchema.json"); - /** * * Replaces the string with a substring at the given index @@ -36,11 +30,13 @@ export function replaceAt(str: string, index: number, replace: string): string { */ export const traverseAndGetProperties = (arr: object[], prop: string): boolean => { let hasProp = false; - arr.forEach((p: object): void => { - if (p[prop]) { - hasProp = true; + arr.forEach( + (p: object): void => { + if (p[prop]) { + hasProp = true; + } } - }); + ); return hasProp; }; @@ -56,9 +52,5 @@ export const traverseAndGetProperties = (arr: object[], prop: string): boolean = */ export const searchProps = (answers: object, input: string): Promise => { input = input || ""; - return Promise.resolve( - PROPS.filter((prop: string): boolean => - prop.toLowerCase().includes(input.toLowerCase()), - ), - ); + return Promise.resolve(PROPS.filter((prop: string): boolean => prop.toLowerCase().includes(input.toLowerCase()))); }; diff --git a/packages/generators/utils/add/questions.ts b/packages/generators/utils/add/questions.ts new file mode 100644 index 00000000000..4fbb5be2c8f --- /dev/null +++ b/packages/generators/utils/add/questions.ts @@ -0,0 +1,46 @@ +import { AutoComplete, Confirm, Input, InputValidate } from "@webpack-cli/webpack-scaffold"; +import { existsSync } from "fs"; +import { resolve } from "path"; +import { searchProps } from "./index"; +import { Question } from "inquirer"; + +/** + * Returns Inquirer question for given action + * @param {string} action action for which question has to be prompted + * @returns {Question} Question for given action + */ +export const manualOrListInput = (action: string): Question => { + const actionQuestion = `What do you want to add to ${action}?`; + return Input("actionAnswer", actionQuestion); +}; + +export const actionTypeQuestion = AutoComplete("actionType", "What property do you want to add to?", { + pageSize: 7, + source: searchProps, + suggestOnly: false +}); + +export const entryTypeQuestion: Question = Confirm("entryType", "Will your application have multiple bundles?", false); + +export const topScopeQuestion: Question = Input("topScope", "What do you want to add to topScope?"); + +const mergeFileQuestionsFunction = (): Question[] => { + const mergePathQuestion = + "What is the location of webpack configuration with which you want to merge current configuration?"; + const mergePathValidator = (path: string): boolean | string => { + const resolvedPath = resolve(process.cwd(), path); + if (existsSync(resolvedPath)) { + if (/\.js$/.test(path)) { + return true; + } + return "Path doesn't corresponds to a javascript file"; + } + return "Invalid path provided"; + }; + const mergeConfigNameQuestion = "What is the name by which you want to denote above configuration?"; + return [ + InputValidate("mergeFile", mergePathQuestion, mergePathValidator), + Input("mergeConfigName", mergeConfigNameQuestion) + ]; +}; +export const mergeFileQuestion: Question[] = mergeFileQuestionsFunction(); diff --git a/packages/generators/utils/add/questions/index.ts b/packages/generators/utils/add/questions/index.ts deleted file mode 100644 index fbbc0dfe138..00000000000 --- a/packages/generators/utils/add/questions/index.ts +++ /dev/null @@ -1,63 +0,0 @@ -import { - AutoComplete, - Confirm, - Input, - InputValidate, -} from "@webpack-cli/webpack-scaffold"; -import { existsSync } from "fs"; -import { resolve } from "path"; -import { - searchProps, -} from "../index"; -import { Question } from "inquirer"; - -/** - * Returns Inquirer question for given action - * @param {string} action action for which question has to be prompted - * @returns {Question} Question for given action - */ -export const manualOrListInput = (action: string): Question => { - const actionQuestion = `What do you want to add to ${action}?`; - return Input("actionAnswer", actionQuestion); -}; - -export const actionTypeQuestion = AutoComplete( - "actionType", - "What property do you want to add to?", - { - pageSize: 7, - source: searchProps, - suggestOnly: false, - }, -); - -export const entryTypeQuestion: Question = Confirm( - "entryType", - "Will your application have multiple bundles?", - false, -); - -export const topScopeQuestion: Question = Input( - "topScope", - "What do you want to add to topScope?", -); - -const mergeFileQuestionsFunction = (): Question[] => { - const mergePathQuestion = "What is the location of webpack configuration with which you want to merge current configuration?"; - const mergePathValidator = (path: string): boolean|string => { - const resolvedPath = resolve(process.cwd(), path); - if (existsSync(resolvedPath)) { - if (/\.js$/.test(path)) { - return true; - } - return "Path doesn't corresponds to a javascript file"; - } - return "Invalid path provided"; - }; - const mergeConfigNameQuestion = "What is the name by which you want to denote above configuration?"; - return [ - InputValidate("mergeFile", mergePathQuestion, mergePathValidator), - Input("mergeConfigName", mergeConfigNameQuestion) -] -}; -export const mergeFileQuestion: Question[] = mergeFileQuestionsFunction();