Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
chore: create questions.ts
  • Loading branch information
rishabh3112 committed Jun 5, 2019
1 parent cf8e3c9 commit 7481974
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 89 deletions.
22 changes: 11 additions & 11 deletions 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,
Expand All @@ -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");

/**
*
Expand Down Expand Up @@ -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();
}
Expand Down
22 changes: 7 additions & 15 deletions packages/generators/utils/add/index.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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;
};

Expand All @@ -56,9 +52,5 @@ export const traverseAndGetProperties = (arr: object[], prop: string): boolean =
*/
export const searchProps = (answers: object, input: string): Promise<string[]> => {
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())));
};
46 changes: 46 additions & 0 deletions 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();
63 changes: 0 additions & 63 deletions packages/generators/utils/add/questions/index.ts

This file was deleted.

0 comments on commit 7481974

Please sign in to comment.