From 065faa5730264e2e4465d03451da45e302152cec Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Mon, 1 Apr 2019 11:17:04 -0500 Subject: [PATCH 1/2] fix(prompt-command, serve): force default package export, add serve default ISSUES CLOSED: #572 --- bin/utils/prompt-command.js | 16 +++++++++++----- packages/serve/index.ts | 6 +++--- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/bin/utils/prompt-command.js b/bin/utils/prompt-command.js index 021fa90527b..907434c6c71 100644 --- a/bin/utils/prompt-command.js +++ b/bin/utils/prompt-command.js @@ -37,6 +37,15 @@ const npmGlobalRoot = () => { }); }; +const runWhenInstalled = (packages, pathForCmd, ...args) => { + const package = require(pathForCmd); + const func = package.default; + if (typeof func !== 'function') { + throw new Error(`@webpack-cli/${packages} failed to export a default function`); + } + return func(...args); +} + module.exports = function promptForInstallation(packages, ...args) { const nameOfPackage = "@webpack-cli/" + packages; let packageIsInstalled = false; @@ -113,10 +122,7 @@ module.exports = function promptForInstallation(packages, ...args) { } pathForCmd = path.resolve(process.cwd(), "node_modules", "@webpack-cli", packages); - if (packages === "serve") { - return require(pathForCmd).default.serve(); - } - return require(pathForCmd).default(...args); //eslint-disable-line + return runWhenInstalled(packages, pathForCmd, ...args); }) .catch(error => { console.error(error); @@ -132,6 +138,6 @@ module.exports = function promptForInstallation(packages, ...args) { } }); } else { - require(pathForCmd).default(...args); // eslint-disable-line + return runWhenInstalled(packages, pathForCmd, ...args); } }; diff --git a/packages/serve/index.ts b/packages/serve/index.ts index d4fa9cad4a0..2759706083a 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -47,11 +47,11 @@ const getRootPathModule = (dep: string): string => path.resolve(process.cwd(), d * * Prompts for installing the devServer and running it * - * @param {Object} args - args processed from the CLI + * @param {String[]} args - args processed from the CLI * @returns {Function} invokes the devServer API */ -function serve() { +export default function serve(...args: string[]) { const packageJSONPath = getRootPathModule("package.json"); if (!packageJSONPath) { console.error( @@ -170,7 +170,7 @@ function serve() { } } -export = { +export { getRootPathModule, serve, spawnNPMWithArg, From a37a1c44bbded05c97d7c2d745932995f1f69dce Mon Sep 17 00:00:00 2001 From: Kirill Nagaitsev Date: Mon, 1 Apr 2019 18:06:48 -0500 Subject: [PATCH 2/2] misc(serve): remove unnecessary exports, update docs --- packages/serve/README.md | 2 +- packages/serve/index.ts | 7 ------- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/serve/README.md b/packages/serve/README.md index eb647361781..a6612b6f9be 100644 --- a/packages/serve/README.md +++ b/packages/serve/README.md @@ -16,7 +16,7 @@ To run the scaffolding instance programmatically, install it as a dependency. Wh ### Node ```js -const serve = require("@webpack-cli/serve").serve; +const serve = require("@webpack-cli/serve").default; serve(); ``` diff --git a/packages/serve/index.ts b/packages/serve/index.ts index 2759706083a..950ae78d3b7 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -169,10 +169,3 @@ export default function serve(...args: string[]) { }); } } - -export { - getRootPathModule, - serve, - spawnNPMWithArg, - spawnYarnWithArg, -};