From 0a648f7d461a659c81971f0af6428a63614e0aa6 Mon Sep 17 00:00:00 2001 From: "devid.farinelli@gmail.com" Date: Fri, 19 Apr 2019 15:11:11 +0200 Subject: [PATCH] feat(webpack-scaffold): adds Input defaults, doc & tests --- packages/webpack-scaffold/README.md | 51 ++++++++++--------- .../__snapshots__/index.test.ts.snap | 11 ++++ .../webpack-scaffold/__tests__/index.test.ts | 14 +++++ packages/webpack-scaffold/index.ts | 12 ++++- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/packages/webpack-scaffold/README.md b/packages/webpack-scaffold/README.md index a0a92441b40..33282ca8cf9 100755 --- a/packages/webpack-scaffold/README.md +++ b/packages/webpack-scaffold/README.md @@ -11,15 +11,20 @@ npm i -D webpack-cli @webpack-cli/webpack-scaffold ``` # API - -- [parseValue](#parsevalue) -- [createArrowFunction](#createarrowfunction) -- [createRegularFunction](#createregularfunction) -- [createDynamicPromise](#createdynamicpromise) -- [createAssetFilterFunction](#createassetfilterfunction) -- [createExternalFunction](#createexternalfunction) -- [createRequire](#createrequire) -- [Inquirer](#inquirer) - [List](#list) - [RawList](#rawlist) - [CheckList](#checklist) - [Input](#input) - [InputValidate](#inputvalidate) - [Confirm](#confirm) +- [parseValue](#parsevalue) +- [createArrowFunction](#createarrowfunction) +- [createRegularFunction](#createregularfunction) +- [createDynamicPromise](#createdynamicpromise) +- [createAssetFilterFunction](#createassetfilterfunction) +- [createExternalFunction](#createexternalfunction) +- [createRequire](#createrequire) +- [Inquirer](#inquirer) + - [List](#list) + - [RawList](#rawlist) + - [CheckList](#checklist) + - [Input](#input) + - [InputValidate](#inputvalidate) + - [Confirm](#confirm) ## parseValue @@ -163,38 +168,38 @@ CheckList("entry", "what kind of entry do you want?", ["Array", "Function"]); ### Input -Param: `name, message` +Param: `name, message, [default]` Creates an Input from Inquirer ```js const Input = require("@webpack-cli/webpack-scaffold").Input; -Input("entry", "what is your entry point?"); +Input('entry', 'what is your entry point?', 'src/index') ``` ### InputValidate -Param: `name, message, validate` +Param: `name, message, [validate, default]` Creates an Input from Inquirer ```js -const InputValidate = require("@webpack-cli/webpack-scaffold").InputValidate; - -const validation = value => { - if (value.length > 4) { - return true; - } else { - return "Wow, that was short!"; - } -}; -InputValidate("entry", "what is your entry point?", validation); +const InputValidate = require('@webpack-cli/webpack-scaffold').InputValidate; + +const validation = (value) => { + if(value.length > 4) { + return true; + } else { + return 'Wow, that was short!' + } +} +InputValidate('entry', 'what is your entry point?', validation, 'src/index') ``` ### Confirm -Param: `name, message, default` +Param: `name, message, [default]` Creates an Input from Inquirer diff --git a/packages/webpack-scaffold/__tests__/__snapshots__/index.test.ts.snap b/packages/webpack-scaffold/__tests__/__snapshots__/index.test.ts.snap index 0304203ef64..0e06b120337 100755 --- a/packages/webpack-scaffold/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/webpack-scaffold/__tests__/__snapshots__/index.test.ts.snap @@ -2,6 +2,17 @@ exports[`utils Inquirer should make an Input object with validation 1`] = ` Object { + "default": undefined, + "message": "what is your plugin?", + "name": "plugins", + "type": "input", + "validate": [Function], +} +`; + +exports[`utils Inquirer should make an Input object with validation and default value 1`] = ` +Object { + "default": "my-plugin", "message": "what is your plugin?", "name": "plugins", "type": "input", diff --git a/packages/webpack-scaffold/__tests__/index.test.ts b/packages/webpack-scaffold/__tests__/index.test.ts index 0db475e179a..586bbcd9a58 100755 --- a/packages/webpack-scaffold/__tests__/index.test.ts +++ b/packages/webpack-scaffold/__tests__/index.test.ts @@ -76,6 +76,15 @@ describe("utils", () => { }); it("should make an Input object", () => { expect(utils.Input("plugins", "what is your plugin?")).toEqual({ + default: undefined, + message: "what is your plugin?", + name: "plugins", + type: "input", + }); + }); + it("should make an Input object", () => { + expect(utils.Input("plugins", "what is your plugin?", "my-plugin")).toEqual({ + default: "my-plugin", message: "what is your plugin?", name: "plugins", type: "input", @@ -102,5 +111,10 @@ describe("utils", () => { utils.InputValidate("plugins", "what is your plugin?", () => true), ).toMatchSnapshot(); }); + it("should make an Input object with validation and default value", () => { + expect( + utils.InputValidate("plugins", "what is your plugin?", () => true, "my-plugin"), + ).toMatchSnapshot(); + }); }); }); diff --git a/packages/webpack-scaffold/index.ts b/packages/webpack-scaffold/index.ts index 0a4ceb38d5c..6477030c331 100755 --- a/packages/webpack-scaffold/index.ts +++ b/packages/webpack-scaffold/index.ts @@ -76,16 +76,24 @@ export function CheckList(name: string, message: string, choices: string[]): Gen }; } -export function Input(name: string, message: string): Generator.Question { +export function Input(name: string, message: string, defaultChoice?: string): Generator.Question { return { + default: defaultChoice, message, name, type: "input" }; } -export function InputValidate(name: string, message: string, cb?: (input: string) => string | boolean): Generator.Question { +export function InputValidate( + name: string, + message: string, + cb?: (input: string) => string | boolean, + defaultChoice?: string, + ): Generator.Question { + return { + default: defaultChoice, message, name, type: "input",