Skip to content

Commit

Permalink
chore(refactor): move questions to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabh3112 committed May 28, 2019
1 parent e839614 commit 915c4ab
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2,067 deletions.
99 changes: 23 additions & 76 deletions packages/generators/add-generator.ts
Expand Up @@ -6,72 +6,24 @@ import {
manualOrListInput,
mergeFileQuestion,
topScopeQuestion,
} from "@webpack-cli/utils/generators/add/questions";
} from "./utils/add/questions";

import {
replaceAt,
traverseAndGetProperties,
webpackDevServerSchema,
webpackSchema
} from './utils/add'


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 { 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())));
};
import { AutoComplete } from "@webpack-cli/webpack-scaffold";
import { resolve } from "path";

/**
*
Expand Down Expand Up @@ -103,15 +55,13 @@ export default class AddGenerator extends Generator {
}
};
const { registerPrompt } = this.env.adapter.promptModule;
registerPrompt("autocomplete", autoComplete);
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
Expand All @@ -120,13 +70,7 @@ export default class AddGenerator extends Generator {
// 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
})
])
return this.prompt(actionTypeQuestion)
.then(
(actionTypeAnswer: { actionType: string }): void => {
// Set initial prop, like devtool
Expand All @@ -138,9 +82,7 @@ export default class AddGenerator extends Generator {
.then(
(): Promise<void | {}> => {
if (action === "entry") {
return this.prompt([
Confirm("entryType", "Will your application have multiple bundles?", false)
])
return this.prompt(entryTypeQuestion)
.then(
(entryTypeAnswer: { entryType: boolean }): Promise<void | {}> => {
// Ask different questions for entry points
Expand All @@ -155,7 +97,7 @@ export default class AddGenerator extends Generator {
);
} else {
if (action === "topScope") {
return this.prompt([Input("topScope", "What do you want to add to topScope?")]).then(
return this.prompt(topScopeQuestion).then(
(topScopeAnswer: { topScope: string }): void => {
this.configuration.config.topScope.push(topScopeAnswer.topScope);
done();
Expand All @@ -169,7 +111,12 @@ export default class AddGenerator extends Generator {
}) => {
const resolvedPath = resolve(process.cwd(), mergeFileAnswer.mergeFile);
const mergeConfig = require(resolvedPath);
this.configuration.config.merge = mergeConfig;
(this.configuration.config.merge as {
configName?: string;
topScope?: string[];
item?: string;
webpackOptions?: WebpackOptions;
}) = mergeConfig;
});
}
}
Expand Down Expand Up @@ -446,7 +393,7 @@ export default class AddGenerator extends Generator {
(action === "devtool" || action === "watch" || action === "mode")
) {
this.configuration.config.item = action;
this.configuration.config.webpackOptions[action] = answerToAction.actionAnswer;
(this.configuration.config.webpackOptions[action] as string) = answerToAction.actionAnswer;
done();
return;
}
Expand Down
@@ -1,6 +1,6 @@
import PROP_TYPES from "../../prop-types";
import PROP_TYPES from "@webpack-cli/utils/prop-types";

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

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

0 comments on commit 915c4ab

Please sign in to comment.