Skip to content

Commit

Permalink
feat(webpack-scaffold): adds Input defaults, doc & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
misterdev committed May 28, 2019
1 parent 99304c4 commit 0a648f7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 25 deletions.
51 changes: 28 additions & 23 deletions packages/webpack-scaffold/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -163,38 +168,38 @@ CheckList("entry", "what kind of entry do you want?", ["Array", "Function"]);

### Input

Param: `name<String>, message<String>`
Param: `name<String>, message<String>, [default<String>]`

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<String>, message<String>, validate<Function>`
Param: `name<String>, message<String>, [validate<Function>, default<String>]`

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<String>, message<String>, default<?Boolean>`
Param: `name<String>, message<String>, [default<Boolean>]`

Creates an Input from Inquirer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 14 additions & 0 deletions packages/webpack-scaffold/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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();
});
});
});
12 changes: 10 additions & 2 deletions packages/webpack-scaffold/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0a648f7

Please sign in to comment.