Skip to content

Commit d4115a6

Browse files
matheus1lvasokra
authored andcommitted
Add installation if webpack-cli is not present
1 parent 41bb63a commit d4115a6

2 files changed

Lines changed: 41 additions & 4 deletions

File tree

bin/webpack.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
#!/usr/bin/env node
1+
const {exec} = require('child_process');
2+
3+
4+
function runCommand(command) {
5+
exec(command, (err, data) => {
6+
if(data) {
7+
console.log(data);
8+
} else {
9+
console.log(err);
10+
}
11+
});
12+
}
213

314
let webpackCliInstalled = false;
415
try {
@@ -11,10 +22,35 @@ try {
1122
if (webpackCliInstalled) {
1223
require("webpack-cli"); // eslint-disable-line node/no-missing-require, node/no-extraneous-require, node/no-unpublished-require
1324
} else {
25+
const path = require('path');
26+
const fs = require('fs');
27+
const isYarn = fs.existsSync(path.resolve(process.cwd(), 'yarn.lock'));
28+
const {prompt} = require('inquirer');
29+
let command;
30+
31+
32+
const question = {
33+
type: 'confirm',
34+
name: 'shouldInstall',
35+
message: 'Would you like to install webpack-cli?',
36+
default: true
37+
}
38+
39+
if(isYarn){
40+
command = 'yarn add webpack-cli -D';
41+
}else{
42+
command = 'npm install --save-dev webpack-cli';
43+
}
44+
1445
console.error("The CLI moved into a separate package: webpack-cli.");
15-
console.error(
16-
"Please install 'webpack-cli' in addition to webpack itself to use the CLI."
17-
);
46+
prompt(question).then((anwswer) => {
47+
if(answer){
48+
console.error('Installing webpack-cli')
49+
runCommand(command);
50+
}
51+
})
52+
53+
console.error("Please install 'webpack-cli' in addition to webpack itself to use the CLI.");
1854
console.error("-> When using npm: npm install webpack-cli -D");
1955
console.error("-> When using yarn: yarn add webpack-cli -D");
2056
process.exitCode = 1;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"file-loader": "^1.1.6",
4242
"glob": "^7.1.2",
4343
"i18n-webpack-plugin": "^1.0.0",
44+
"inquire": "^0.4.8",
4445
"istanbul": "^0.4.5",
4546
"jade": "^1.11.0",
4647
"jade-loader": "~0.8.0",

0 commit comments

Comments
 (0)