Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions bin/utils/prompt-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -132,6 +138,6 @@ module.exports = function promptForInstallation(packages, ...args) {
}
});
} else {
require(pathForCmd).default(...args); // eslint-disable-line
return runWhenInstalled(packages, pathForCmd, ...args);
}
};
2 changes: 1 addition & 1 deletion packages/serve/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

Expand Down
11 changes: 2 additions & 9 deletions packages/serve/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -169,10 +169,3 @@ function serve() {
});
}
}

export = {
getRootPathModule,
serve,
spawnNPMWithArg,
spawnYarnWithArg,
};