Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bin, serve): force default package export, add serve default #815

Merged
merged 2 commits into from Apr 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 11 additions & 5 deletions bin/utils/prompt-command.js
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();
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
}
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);
}
};
6 changes: 3 additions & 3 deletions packages/serve/index.ts
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 @@ -170,7 +170,7 @@ function serve() {
}
}

export = {
export {
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
evenstensberg marked this conversation as resolved.
Show resolved Hide resolved
getRootPathModule,
serve,
spawnNPMWithArg,
Expand Down