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/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 d4fa9cad4a0..950ae78d3b7 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( @@ -169,10 +169,3 @@ function serve() { }); } } - -export = { - getRootPathModule, - serve, - spawnNPMWithArg, - spawnYarnWithArg, -};