Skip to content

Commit

Permalink
fix(bin, serve): force default package export, add serve default (#815)
Browse files Browse the repository at this point in the history
* fix(prompt-command, serve): force default package export, add serve default

ISSUES CLOSED: #572

* misc(serve): remove unnecessary exports, update docs
  • Loading branch information
knagaitsev authored and sendilkumarn committed Apr 2, 2019
1 parent 6cfed0f commit b96857f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 15 deletions.
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();
}
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
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
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,
};

0 comments on commit b96857f

Please sign in to comment.