From dca80dec4dd2c564ddce34f8570f6a559a27c4e3 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 13:43:37 +0530 Subject: [PATCH 1/9] chore: create package run --- packages/run/.gitignore | 3 ++ packages/run/README.md | 11 +++++ packages/run/__tests__/run.test.js | 7 ++++ packages/run/package.json | 39 +++++++++++++++++ packages/run/src/run.ts | 60 +++++++++++++++++++++++++++ packages/run/tsconfig.json | 9 ++++ packages/run/yarn.lock | 60 +++++++++++++++++++++++++++ packages/webpack-cli/lib/bootstrap.js | 2 +- packages/webpack-cli/package.json | 1 + 9 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 packages/run/.gitignore create mode 100644 packages/run/README.md create mode 100644 packages/run/__tests__/run.test.js create mode 100644 packages/run/package.json create mode 100644 packages/run/src/run.ts create mode 100644 packages/run/tsconfig.json create mode 100644 packages/run/yarn.lock diff --git a/packages/run/.gitignore b/packages/run/.gitignore new file mode 100644 index 00000000000..ba4337343ce --- /dev/null +++ b/packages/run/.gitignore @@ -0,0 +1,3 @@ +lib/* +node_modules/* +tsconfig.tsbuildinfo diff --git a/packages/run/README.md b/packages/run/README.md new file mode 100644 index 00000000000..2f4a07dcd9f --- /dev/null +++ b/packages/run/README.md @@ -0,0 +1,11 @@ +# `@webpack-cli/run` + +> TODO: description + +## Usage + +``` +const run = require('@webpack-cli/run'); + +// TODO: DEMONSTRATE API +``` diff --git a/packages/run/__tests__/run.test.js b/packages/run/__tests__/run.test.js new file mode 100644 index 00000000000..54c70430210 --- /dev/null +++ b/packages/run/__tests__/run.test.js @@ -0,0 +1,7 @@ +'use strict'; + +const run = require('..'); + +describe('@webpack-cli/run', () => { + it('needs tests'); +}); diff --git a/packages/run/package.json b/packages/run/package.json new file mode 100644 index 00000000000..fc1fa41f62e --- /dev/null +++ b/packages/run/package.json @@ -0,0 +1,39 @@ +{ + "name": "@webpack-cli/run", + "version": "1.0.0", + "description": "Execute CLI interactivly using prompts", + "keywords": [ + "webpack-cli" + ], + "author": "rishabh3112 ", + "homepage": "https://github.com/webpack/webpack-cli.git", + "license": "MIT", + "main": "lib/run.js", + "directories": { + "lib": "lib", + "test": "__tests__" + }, + "files": [ + "lib" + ], + "publishConfig": { + "registry": "https://registry.yarnpkg.com" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/webpack/webpack-cli.git" + }, + "scripts": { + "test": "echo \"Error: run tests from root\" && exit 1" + }, + "bugs": { + "url": "https://github.com/webpack/webpack-cli/issues" + }, + "dependencies": { + "chalk": "^3.0.0", + "enquirer": "^2.3.4" + }, + "peerDependencies": { + "webpack-cli": "^3.3.11" + } +} diff --git a/packages/run/src/run.ts b/packages/run/src/run.ts new file mode 100644 index 00000000000..d399717244a --- /dev/null +++ b/packages/run/src/run.ts @@ -0,0 +1,60 @@ +const { MultiSelect, Input } = require('enquirer'); +import runner from 'webpack-cli/lib/runner'; +import logger from 'webpack-cli/lib/utils/logger'; +import {core as cliArgs} from 'webpack-cli/lib/utils/cli-flags'; +import chalk from 'chalk' + +async function prompter() { + const args = []; + const typePrompt = new MultiSelect({ + name: 'type', + message: 'Which flags do you want to use?', + choices: cliArgs.reduce((prev, curr) => { + return [...prev, `--${curr.name}: ${curr.description}`]; + }, []), + result: (value) => { + return value.map((flag) => flag.split(":")[0]); + } + }); + const selections = await typePrompt.run(); + const boolArgs = []; + const questions = []; + + selections.forEach(selection => { + const options = cliArgs.find(flag => { + return flag.name === selection.slice(2); + }); + + if (options.type === Boolean) { + boolArgs.push(selection); + return; + } + + const valuePrompt = new Input({ + name: 'value', + message: `Enter value of the ${selection} flag`, + initial: options.defaultValue, + result: (value) => [selection, value] + }); + questions.push(valuePrompt); + }); + + // Create promise chain to force synchronous prompt of question + for await (let question of questions) { + const flagArgs = await question.run(); + args.push(...flagArgs); + } + + return [...args, ...boolArgs]; +} + +export default async function run() { + try { + const args = await prompter(); + process.stdout.write('\n'); + logger.info(`Executing CLI\n`); + runner([], args); + } catch (err) { + logger.error(`Action Interrupted, use ${chalk.cyan(`webpack-cli help`)} to see possible options.`) + } +} diff --git a/packages/run/tsconfig.json b/packages/run/tsconfig.json new file mode 100644 index 00000000000..aa32ce86439 --- /dev/null +++ b/packages/run/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "../../tsconfig.packages.json", + "compilerOptions": { + "outDir": "./lib", + "rootDir": "./src", + "composite": true + }, + "include": ["./src"], +} diff --git a/packages/run/yarn.lock b/packages/run/yarn.lock new file mode 100644 index 00000000000..584534eee6f --- /dev/null +++ b/packages/run/yarn.lock @@ -0,0 +1,60 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@types/color-name@^1.1.1": + version "1.1.1" + resolved "https://registry.yarnpkg.com/@types/color-name/-/color-name-1.1.1.tgz#1c1261bbeaa10a8055bbc5d8ab84b7b2afc846a0" + integrity sha512-rr+OQyAjxze7GgWrSaJwydHStIhHq2lvY3BOC2Mj7KnzI7XK0Uw1TOOdI9lDoajEbSWLiYgoo4f1R51erQfhPQ== + +ansi-colors@^3.2.1: + version "3.2.4" + resolved "https://registry.yarnpkg.com/ansi-colors/-/ansi-colors-3.2.4.tgz#e3a3da4bfbae6c86a9c285625de124a234026fbf" + integrity sha512-hHUXGagefjN2iRrID63xckIvotOXOojhQKWIPUZ4mNUZ9nLZW+7FMNoE1lOkEhNWYsx/7ysGIuJYCiMAA9FnrA== + +ansi-styles@^4.1.0: + version "4.2.1" + resolved "https://registry.yarnpkg.com/ansi-styles/-/ansi-styles-4.2.1.tgz#90ae75c424d008d2624c5bf29ead3177ebfcf359" + integrity sha512-9VGjrMsG1vePxcSweQsN20KY/c4zN0h9fLjqAbwbPfahM3t+NL+M9HC8xeXG2I8pX5NoamTGNuomEUFI7fcUjA== + dependencies: + "@types/color-name" "^1.1.1" + color-convert "^2.0.1" + +chalk@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/chalk/-/chalk-3.0.0.tgz#3f73c2bf526591f574cc492c51e2456349f844e4" + integrity sha512-4D3B6Wf41KOYRFdszmDqMCGq5VV/uMAB273JILmO+3jAlh8X4qDtdtgCR3fxtbLEMzSx22QdhnDcJvu2u1fVwg== + dependencies: + ansi-styles "^4.1.0" + supports-color "^7.1.0" + +color-convert@^2.0.1: + version "2.0.1" + resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3" + integrity sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ== + dependencies: + color-name "~1.1.4" + +color-name@~1.1.4: + version "1.1.4" + resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2" + integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA== + +enquirer@^2.3.4: + version "2.3.4" + resolved "https://registry.yarnpkg.com/enquirer/-/enquirer-2.3.4.tgz#c608f2e1134c7f68c1c9ee056de13f9b31076de9" + integrity sha512-pkYrrDZumL2VS6VBGDhqbajCM2xpkUNLuKfGPjfKaSIBKYopQbqEFyrOkRMIb2HDR/rO1kGhEt/5twBwtzKBXw== + dependencies: + ansi-colors "^3.2.1" + +has-flag@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" + integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== + +supports-color@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.1.0.tgz#68e32591df73e25ad1c4b49108a2ec507962bfd1" + integrity sha512-oRSIpR8pxT1Wr2FquTNnGet79b3BWljqOuoW/h4oBhxJ/HUbX5nX6JSruTkvXDCFMwDPvsaTTbvMLKZWSy0R5g== + dependencies: + has-flag "^4.0.0" diff --git a/packages/webpack-cli/lib/bootstrap.js b/packages/webpack-cli/lib/bootstrap.js index 99b59f6cb02..fbeb9344d52 100644 --- a/packages/webpack-cli/lib/bootstrap.js +++ b/packages/webpack-cli/lib/bootstrap.js @@ -1,7 +1,7 @@ const WebpackCLI = require('./webpack-cli'); const { core, commands } = require('./utils/cli-flags'); const logger = require('./utils/logger'); -const cliExecuter = require('./utils/cli-executer'); +const cliExecuter = require('@webpack-cli/run').default; require('./utils/process-log'); diff --git a/packages/webpack-cli/package.json b/packages/webpack-cli/package.json index d31f69249c5..e76f60a11ce 100644 --- a/packages/webpack-cli/package.json +++ b/packages/webpack-cli/package.json @@ -40,6 +40,7 @@ "webpack-merge": "^4.2.2" }, "peerDependencies": { + "@webpack-cli/run": "../run", "webpack": "4.x.x || 5.x.x" }, "devDependencies": { From ebc1ef0df6ee72b7d15ccc02bafb15e3a6730bd5 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 14:03:15 +0530 Subject: [PATCH 2/9] chore: rebase and add test --- packages/run/__tests__/run.test.js | 73 ++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 4 deletions(-) diff --git a/packages/run/__tests__/run.test.js b/packages/run/__tests__/run.test.js index 54c70430210..f40d988bac9 100644 --- a/packages/run/__tests__/run.test.js +++ b/packages/run/__tests__/run.test.js @@ -1,7 +1,72 @@ -'use strict'; +jest.mock('webpack-cli/lib/runner'); +jest.mock('enquirer'); -const run = require('..'); +const runner = require('webpack-cli/lib/runner'); +runner.mockImplementation(() => {}); -describe('@webpack-cli/run', () => { - it('needs tests'); +describe('CLI Executer', () => { + let cliExecuter = null; + let multiCalls = 0; + let multiChoices = null; + let multiMapper = null; + + let inputCalls = 0; + const inputConstructorObjs = []; + + beforeAll(() => { + let inputRunCount = 0; + + const enquirer = require('enquirer'); + enquirer.MultiSelect = class MultiSelect { + constructor(obj) { + multiCalls++; + multiChoices = obj.choices; + multiMapper = obj.result; + } + + run() { + return ['--config', '--entry', '--progress']; + } + }; + enquirer.Input = class Input { + constructor(obj) { + this.mapper = obj.result; + inputCalls++; + inputConstructorObjs.push(obj); + } + + run(obj) { + inputRunCount++; + return this.mapper(`test${inputRunCount}`); + } + }; + + cliExecuter = require('..'); + }); + + it('runs enquirer options then runs webpack', async () => { + await cliExecuter(); + + // ensure that the webpack runner is called + expect(runner.mock.calls.length).toEqual(1); + expect(runner.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); + + // check that webpack options are actually being displayed that + // the user can select from + expect(multiCalls).toEqual(1); + expect(multiChoices instanceof Array).toBeTruthy(); + expect(multiChoices.length > 0).toBeTruthy(); + expect(multiChoices[0]).toMatch(/\-\-entry/); + + // ensure flag names are parsed out correctly + expect(typeof multiMapper).toEqual('function'); + expect(multiMapper(['--test1: test flag', '--test2: test flag 2'])).toEqual(['--test1', '--test2']); + + // check that the user is then prompted to set values to + // some flags + expect(inputCalls).toEqual(2); + expect(inputConstructorObjs.length).toEqual(2); + expect(inputConstructorObjs[0].message).toEqual('Enter value of the --config flag'); + expect(inputConstructorObjs[1].message).toEqual('Enter value of the --entry flag'); + }); }); From 73e9b04ae3bb2a3e0011611f72bf4ed6d08274f9 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 14:04:09 +0530 Subject: [PATCH 3/9] chore: remove stale files --- .../__tests__/cli-executer.test.js | 72 ------------------- .../webpack-cli/lib/utils/cli-executer.js | 64 ----------------- 2 files changed, 136 deletions(-) delete mode 100644 packages/webpack-cli/__tests__/cli-executer.test.js delete mode 100644 packages/webpack-cli/lib/utils/cli-executer.js diff --git a/packages/webpack-cli/__tests__/cli-executer.test.js b/packages/webpack-cli/__tests__/cli-executer.test.js deleted file mode 100644 index b0f7ff78198..00000000000 --- a/packages/webpack-cli/__tests__/cli-executer.test.js +++ /dev/null @@ -1,72 +0,0 @@ -jest.mock('../lib/runner'); -jest.mock('enquirer'); - -const runner = require('../lib/runner'); -runner.mockImplementation(() => {}); - -describe('CLI Executer', () => { - let cliExecuter = null; - let multiCalls = 0; - let multiChoices = null; - let multiMapper = null; - - let inputCalls = 0; - const inputConstructorObjs = []; - - beforeAll(() => { - let inputRunCount = 0; - - const enquirer = require('enquirer'); - enquirer.MultiSelect = class MultiSelect { - constructor(obj) { - multiCalls++; - multiChoices = obj.choices; - multiMapper = obj.result; - } - - run() { - return ['--config', '--entry', '--progress']; - } - }; - enquirer.Input = class Input { - constructor(obj) { - this.mapper = obj.result; - inputCalls++; - inputConstructorObjs.push(obj); - } - - run(obj) { - inputRunCount++; - return this.mapper(`test${inputRunCount}`); - } - }; - - cliExecuter = require('../lib/utils/cli-executer'); - }); - - it('runs enquirer options then runs webpack', async () => { - await cliExecuter(); - - // ensure that the webpack runner is called - expect(runner.mock.calls.length).toEqual(1); - expect(runner.mock.calls[0]).toEqual([[], ['--config', 'test1', '--entry', 'test2', '--progress']]); - - // check that webpack options are actually being displayed that - // the user can select from - expect(multiCalls).toEqual(1); - expect(multiChoices instanceof Array).toBeTruthy(); - expect(multiChoices.length > 0).toBeTruthy(); - expect(multiChoices[0]).toMatch(/\-\-entry/); - - // ensure flag names are parsed out correctly - expect(typeof multiMapper).toEqual('function'); - expect(multiMapper(['--test1: test flag', '--test2: test flag 2'])).toEqual(['--test1', '--test2']); - - // check that the user is then prompted to set values to - // some flags - expect(inputCalls).toEqual(2); - expect(inputConstructorObjs.length).toEqual(2); - expect(inputConstructorObjs[0].message).toEqual('Enter value of the --config flag'); - expect(inputConstructorObjs[1].message).toEqual('Enter value of the --entry flag'); - }); -}); diff --git a/packages/webpack-cli/lib/utils/cli-executer.js b/packages/webpack-cli/lib/utils/cli-executer.js deleted file mode 100644 index 045b92d19f0..00000000000 --- a/packages/webpack-cli/lib/utils/cli-executer.js +++ /dev/null @@ -1,64 +0,0 @@ -const { MultiSelect, Input } = require('enquirer'); -const chalk = require('chalk'); -const runner = require('../runner'); -const logger = require('./logger'); -const cliArgs = require('./cli-flags').core; - -async function prompter() { - const args = []; - - const typePrompt = new MultiSelect({ - name: 'type', - message: 'Which flags do you want to use?', - choices: cliArgs.reduce((prev, curr) => { - return [...prev, `--${curr.name}: ${curr.description}`]; - }, []), - result: (value) => { - return value.map((flag) => flag.split(":")[0]); - } - }); - - const selections = await typePrompt.run(); - - const boolArgs = []; - const questions = []; - selections.forEach(selection => { - const options = cliArgs.find(flag => { - return flag.name === selection.slice(2); - }); - - if (options.type === Boolean) { - boolArgs.push(selection); - return; - } - - const valuePrompt = new Input({ - name: 'value', - message: `Enter value of the ${selection} flag`, - initial: options.defaultValue, - result: (value) => [selection, value] - }); - questions.push(valuePrompt); - }); - - // Create promise chain to force synchronous prompt of question - for await (question of questions) { - const flagArgs = await question.run(); - args.push(...flagArgs); - } - - return [...args, ...boolArgs]; -} - -async function run() { - try { - const args = await prompter(); - process.stdout.write('\n'); - logger.info(`Executing CLI\n`); - runner([], args); - } catch (err) { - logger.error(`Action Interrupted, use ${chalk.cyan(`webpack-cli help`)} to see possible options.`) - } -} - -module.exports = run; From be6a780a5876ea37ca748220af2627fa2e55e525 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 14:07:05 +0530 Subject: [PATCH 4/9] chore: lint --- packages/run/src/run.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/run/src/run.ts b/packages/run/src/run.ts index d399717244a..346c4a34a82 100644 --- a/packages/run/src/run.ts +++ b/packages/run/src/run.ts @@ -1,8 +1,8 @@ const { MultiSelect, Input } = require('enquirer'); import runner from 'webpack-cli/lib/runner'; import logger from 'webpack-cli/lib/utils/logger'; -import {core as cliArgs} from 'webpack-cli/lib/utils/cli-flags'; -import chalk from 'chalk' +import { core as cliArgs } from 'webpack-cli/lib/utils/cli-flags'; +import chalk from 'chalk'; async function prompter() { const args = []; @@ -12,9 +12,9 @@ async function prompter() { choices: cliArgs.reduce((prev, curr) => { return [...prev, `--${curr.name}: ${curr.description}`]; }, []), - result: (value) => { - return value.map((flag) => flag.split(":")[0]); - } + result: value => { + return value.map(flag => flag.split(':')[0]); + }, }); const selections = await typePrompt.run(); const boolArgs = []; @@ -34,7 +34,7 @@ async function prompter() { name: 'value', message: `Enter value of the ${selection} flag`, initial: options.defaultValue, - result: (value) => [selection, value] + result: value => [selection, value], }); questions.push(valuePrompt); }); @@ -50,11 +50,11 @@ async function prompter() { export default async function run() { try { - const args = await prompter(); - process.stdout.write('\n'); - logger.info(`Executing CLI\n`); - runner([], args); + const args = await prompter(); + process.stdout.write('\n'); + logger.info('Executing CLI\n'); + runner([], args); } catch (err) { - logger.error(`Action Interrupted, use ${chalk.cyan(`webpack-cli help`)} to see possible options.`) + logger.error(`Action Interrupted, use ${chalk.cyan('webpack-cli help')} to see possible options.`); } } From d4c0197c5b7b228102f1ec00d953c3f19bf04e52 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 14:20:32 +0530 Subject: [PATCH 5/9] chore: update test --- packages/run/__tests__/run.test.js | 4 ++-- packages/run/package.json | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/run/__tests__/run.test.js b/packages/run/__tests__/run.test.js index f40d988bac9..dc6aaff6109 100644 --- a/packages/run/__tests__/run.test.js +++ b/packages/run/__tests__/run.test.js @@ -4,7 +4,7 @@ jest.mock('enquirer'); const runner = require('webpack-cli/lib/runner'); runner.mockImplementation(() => {}); -describe('CLI Executer', () => { +describe('@webpack-cli/run', () => { let cliExecuter = null; let multiCalls = 0; let multiChoices = null; @@ -41,7 +41,7 @@ describe('CLI Executer', () => { } }; - cliExecuter = require('..'); + cliExecuter = require('..').default; }); it('runs enquirer options then runs webpack', async () => { diff --git a/packages/run/package.json b/packages/run/package.json index fc1fa41f62e..3e844c71480 100644 --- a/packages/run/package.json +++ b/packages/run/package.json @@ -9,6 +9,9 @@ "homepage": "https://github.com/webpack/webpack-cli.git", "license": "MIT", "main": "lib/run.js", + "engines": { + "node": ">=10.13.0" + }, "directories": { "lib": "lib", "test": "__tests__" From 1f348e80a6241169b00ecccf950a1a5fb0e4af39 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 14:40:22 +0530 Subject: [PATCH 6/9] chore: add eslintrc --- packages/run/.eslintrc | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 packages/run/.eslintrc diff --git a/packages/run/.eslintrc b/packages/run/.eslintrc new file mode 100644 index 00000000000..e8b8b926974 --- /dev/null +++ b/packages/run/.eslintrc @@ -0,0 +1,10 @@ +{ + "root": true, + "extends": [ + "plugin:@typescript-eslint/recommended", + "prettier", + "prettier/@typescript-eslint" + ], + "parser": "@typescript-eslint/parser", + "plugins": ["@typescript-eslint"], +} From 5051ef2f227d50dd87883d7ae724fbe1a7738216 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 15:14:19 +0530 Subject: [PATCH 7/9] chore: fix eslint errors --- packages/run/.eslintrc | 1 + .../run/__tests__/{run.test.js => run.test.ts} | 6 ++++-- packages/run/src/run.ts | 16 +++++++++------- packages/run/tsconfig.json | 2 +- 4 files changed, 15 insertions(+), 10 deletions(-) rename packages/run/__tests__/{run.test.js => run.test.ts} (93%) diff --git a/packages/run/.eslintrc b/packages/run/.eslintrc index e8b8b926974..320ad83b2f0 100644 --- a/packages/run/.eslintrc +++ b/packages/run/.eslintrc @@ -1,5 +1,6 @@ { "root": true, + "ignorePatterns": ["*.tests.js"], "extends": [ "plugin:@typescript-eslint/recommended", "prettier", diff --git a/packages/run/__tests__/run.test.js b/packages/run/__tests__/run.test.ts similarity index 93% rename from packages/run/__tests__/run.test.js rename to packages/run/__tests__/run.test.ts index dc6aaff6109..af0de597d95 100644 --- a/packages/run/__tests__/run.test.js +++ b/packages/run/__tests__/run.test.ts @@ -1,3 +1,5 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +/* eslint-disable @typescript-eslint/no-var-requires */ jest.mock('webpack-cli/lib/runner'); jest.mock('enquirer'); @@ -24,7 +26,7 @@ describe('@webpack-cli/run', () => { multiMapper = obj.result; } - run() { + run(): string[] { return ['--config', '--entry', '--progress']; } }; @@ -35,7 +37,7 @@ describe('@webpack-cli/run', () => { inputConstructorObjs.push(obj); } - run(obj) { + run(): string { inputRunCount++; return this.mapper(`test${inputRunCount}`); } diff --git a/packages/run/src/run.ts b/packages/run/src/run.ts index 346c4a34a82..d160d3e9bdf 100644 --- a/packages/run/src/run.ts +++ b/packages/run/src/run.ts @@ -1,10 +1,12 @@ -const { MultiSelect, Input } = require('enquirer'); import runner from 'webpack-cli/lib/runner'; import logger from 'webpack-cli/lib/utils/logger'; import { core as cliArgs } from 'webpack-cli/lib/utils/cli-flags'; import chalk from 'chalk'; -async function prompter() { +// eslint-disable-next-line @typescript-eslint/no-var-requires +const { MultiSelect, Input } = require('enquirer'); + +async function prompter(): Promise { const args = []; const typePrompt = new MultiSelect({ name: 'type', @@ -12,11 +14,11 @@ async function prompter() { choices: cliArgs.reduce((prev, curr) => { return [...prev, `--${curr.name}: ${curr.description}`]; }, []), - result: value => { + result: (value: string[] ): string[] => { return value.map(flag => flag.split(':')[0]); }, }); - const selections = await typePrompt.run(); + const selections: string[] = await typePrompt.run(); const boolArgs = []; const questions = []; @@ -34,13 +36,13 @@ async function prompter() { name: 'value', message: `Enter value of the ${selection} flag`, initial: options.defaultValue, - result: value => [selection, value], + result: (value: string): string[] => [selection, value], }); questions.push(valuePrompt); }); // Create promise chain to force synchronous prompt of question - for await (let question of questions) { + for await (const question of questions) { const flagArgs = await question.run(); args.push(...flagArgs); } @@ -48,7 +50,7 @@ async function prompter() { return [...args, ...boolArgs]; } -export default async function run() { +export default async function run(): Promise { try { const args = await prompter(); process.stdout.write('\n'); diff --git a/packages/run/tsconfig.json b/packages/run/tsconfig.json index aa32ce86439..fd0ff87aea4 100644 --- a/packages/run/tsconfig.json +++ b/packages/run/tsconfig.json @@ -5,5 +5,5 @@ "rootDir": "./src", "composite": true }, - "include": ["./src"], + "include": ["./src"] } From eabb2bbb0e898a1b1d812ee0526c3284b0358638 Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 15:25:40 +0530 Subject: [PATCH 8/9] chore: remove typescript test --- packages/run/.eslintrc | 2 +- packages/run/__tests__/{run.test.ts => run.test.js} | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) rename packages/run/__tests__/{run.test.ts => run.test.js} (93%) diff --git a/packages/run/.eslintrc b/packages/run/.eslintrc index 320ad83b2f0..b474e27e4dd 100644 --- a/packages/run/.eslintrc +++ b/packages/run/.eslintrc @@ -7,5 +7,5 @@ "prettier/@typescript-eslint" ], "parser": "@typescript-eslint/parser", - "plugins": ["@typescript-eslint"], + "plugins": ["@typescript-eslint"] } diff --git a/packages/run/__tests__/run.test.ts b/packages/run/__tests__/run.test.js similarity index 93% rename from packages/run/__tests__/run.test.ts rename to packages/run/__tests__/run.test.js index af0de597d95..d041a56ca01 100644 --- a/packages/run/__tests__/run.test.ts +++ b/packages/run/__tests__/run.test.js @@ -1,5 +1,4 @@ -/* eslint-disable @typescript-eslint/no-empty-function */ -/* eslint-disable @typescript-eslint/no-var-requires */ +/* eslint-disable */ // Disable typescript check jest.mock('webpack-cli/lib/runner'); jest.mock('enquirer'); @@ -26,7 +25,7 @@ describe('@webpack-cli/run', () => { multiMapper = obj.result; } - run(): string[] { + run() { return ['--config', '--entry', '--progress']; } }; @@ -37,7 +36,7 @@ describe('@webpack-cli/run', () => { inputConstructorObjs.push(obj); } - run(): string { + run(obj) { inputRunCount++; return this.mapper(`test${inputRunCount}`); } From 69f6efbcc6c76bedf25ef2ef5d71737db6e0d51c Mon Sep 17 00:00:00 2001 From: Rishabh Chawla Date: Tue, 10 Mar 2020 21:42:12 +0530 Subject: [PATCH 9/9] docs: add documentation --- packages/run/README.md | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/run/README.md b/packages/run/README.md index 2f4a07dcd9f..a3da89e5605 100644 --- a/packages/run/README.md +++ b/packages/run/README.md @@ -1,11 +1,16 @@ # `@webpack-cli/run` +Programmatically execute CLI interactively using prompts -> TODO: description - -## Usage +## Installation +```bash +yarn install webpack-cli @webpack-cli/run ``` -const run = require('@webpack-cli/run'); +`webpack-cli` is a peer dependency + +## Usage -// TODO: DEMONSTRATE API +```js +const cliExecuter = require('@webpack-cli/run'); +cliExecuter(); ```