Skip to content

Commit 992bfe2

Browse files
committedDec 18, 2017
adds serve alias
1 parent 5d4430a commit 992bfe2

File tree

5 files changed

+190
-3
lines changed

5 files changed

+190
-3
lines changed
 

‎bin/webpack.js

+5
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ const NON_COMPILATION_ARGS = [
1616
"add",
1717
"remove",
1818
"update",
19+
"serve",
1920
"generate-loader",
2021
"generate-plugin"
2122
];
2223

2324
const NON_COMPILATION_CMD = process.argv.find(arg => {
25+
if (arg === "serve") {
26+
global.process.argv = global.process.argv.filter(a => a !== "serve");
27+
process.argv = global.process.argv;
28+
}
2429
return NON_COMPILATION_ARGS.find(a => a === arg);
2530
});
2631

‎lib/commands/serve.js

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
"use strict";
2+
3+
const inquirer = require("inquirer");
4+
const path = require("path");
5+
const glob = require("glob-all");
6+
const chalk = require("chalk");
7+
const spawn = require("cross-spawn");
8+
const List = require("webpack-addons").List;
9+
const processPromise = require("../utils/resolve-packages").processPromise;
10+
11+
/**
12+
*
13+
* Installs WDS using NPM with --save --dev etc
14+
*
15+
* @param {Object} cmd - arg to spawn with
16+
* @returns {Void}
17+
*/
18+
19+
const spawnNPMWithArg = cmd =>
20+
spawn.sync("npm", ["install", "webpack-dev-server", cmd], {
21+
stdio: "inherit"
22+
});
23+
24+
/**
25+
*
26+
* Installs WDS using Yarn with add etc
27+
*
28+
* @param {Object} cmd - arg to spawn with
29+
* @returns {Void}
30+
*/
31+
32+
const spawnYarnWithArg = cmd =>
33+
spawn.sync("yarn", ["add", "webpack-dev-server", cmd], {
34+
stdio: "inherit"
35+
});
36+
37+
/**
38+
*
39+
* Find the path of a given module
40+
*
41+
* @param {Object} dep - dependency to find
42+
* @returns {String} string with given path
43+
*/
44+
45+
const getRootPathModule = dep =>
46+
glob
47+
.sync([
48+
path.resolve(process.cwd(), dep),
49+
path.join(process.cwd(), "..", dep),
50+
path.join(process.cwd(), "..", "..", dep),
51+
path.join(process.cwd(), "..", "..", "..", dep)
52+
])
53+
.pop();
54+
55+
/**
56+
*
57+
* Prompts for installing the devServer and running it
58+
*
59+
* @param {Object} args - args processed from the CLI
60+
* @returns {Function} invokes the devServer API
61+
*/
62+
module.exports = function serve() {
63+
let packageJSONPath = getRootPathModule("package.json");
64+
if (!packageJSONPath) {
65+
console.log(
66+
"\n",
67+
chalk.red("✖ Could not find your package.json file"),
68+
"\n"
69+
);
70+
process.exit(1);
71+
}
72+
let packageJSON = require(packageJSONPath);
73+
/*
74+
* We gotta do this, cause some configs might not have devdep,
75+
* dep or optional dep, so we'd need sanity checks for each
76+
*/
77+
let hasDevServerDep = packageJSON
78+
? Object.keys(packageJSON).filter(p => packageJSON[p]["webpack-dev-server"])
79+
: [];
80+
81+
if (hasDevServerDep.length) {
82+
let WDSPath = getRootPathModule(
83+
"node_modules/webpack-dev-server/bin/webpack-dev-server.js"
84+
);
85+
if (!WDSPath) {
86+
console.log(
87+
"\n",
88+
chalk.red(
89+
"✖ Could not find the webpack-dev-server dependency in node_modules root path"
90+
)
91+
);
92+
console.log(
93+
chalk.bold.green(" ✔︎"),
94+
"Try this command:",
95+
chalk.bold.green("rm -rf node_modules && npm install")
96+
);
97+
process.exit(1);
98+
}
99+
return require(WDSPath);
100+
} else {
101+
process.stdout.write(
102+
"\n" +
103+
chalk.bold(
104+
"✖ We didn't find any webpack-dev-server dependency in your project,"
105+
) +
106+
"\n" +
107+
chalk.bold.green(" 'webpack serve'") +
108+
" " +
109+
chalk.bold("requires you to have it installed ") +
110+
"\n\n"
111+
);
112+
return inquirer
113+
.prompt([
114+
{
115+
type: "confirm",
116+
name: "confirmDevserver",
117+
message: "Do you want to install it? (default: Y)",
118+
default: "Y"
119+
}
120+
])
121+
.then(answer => {
122+
if (answer["confirmDevserver"]) {
123+
return inquirer
124+
.prompt(
125+
List(
126+
"confirmDepType",
127+
"What kind of dependency do you want it to be under? (default: devDependency)",
128+
["devDependency", "optionalDependency", "dependency"]
129+
)
130+
)
131+
.then(depTypeAns => {
132+
const packager = getRootPathModule("package-lock.json")
133+
? "npm"
134+
: "yarn";
135+
let spawnAction;
136+
if (depTypeAns["confirmDepType"] === "devDependency") {
137+
if (packager === "yarn") {
138+
spawnAction = () => spawnYarnWithArg("--dev");
139+
} else {
140+
spawnAction = () => spawnNPMWithArg("--save-dev");
141+
}
142+
}
143+
if (depTypeAns["confirmDepType"] === "dependency") {
144+
if (packager === "yarn") {
145+
spawnAction = () => spawnYarnWithArg(" ");
146+
} else {
147+
spawnAction = () => spawnNPMWithArg("--save");
148+
}
149+
}
150+
if (depTypeAns["confirmDepType"] === "optionalDependency") {
151+
if (packager === "yarn") {
152+
spawnAction = () => spawnYarnWithArg("--optional");
153+
} else {
154+
spawnNPMWithArg("--save-optional");
155+
}
156+
}
157+
return processPromise(spawnAction()).then(() => {
158+
// Recursion doesn't work well with require call being cached
159+
delete require.cache[require.resolve(packageJSONPath)];
160+
return serve();
161+
});
162+
});
163+
} else {
164+
console.log(chalk.bold.red("✖ Serve aborted due cancelling"));
165+
process.exitCode = 1;
166+
}
167+
})
168+
.catch(err => {
169+
console.log(chalk.red("✖ Serve aborted due to some errors"));
170+
console.error(err);
171+
process.exitCode = 1;
172+
});
173+
}
174+
};

‎lib/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const path = require("path");
1414
*/
1515

1616
module.exports = function initialize(command, args) {
17-
const popArgs = args.slice(2).pop();
17+
const popArgs = args ? args.slice(2).pop() : null;
1818
switch (command) {
1919
case "init": {
2020
const initPkgs = args.slice(2).length === 1 ? [] : [popArgs];
@@ -41,6 +41,9 @@ module.exports = function initialize(command, args) {
4141
case "update": {
4242
return require("./commands/update")();
4343
}
44+
case "serve": {
45+
return require("./commands/serve")();
46+
}
4447
case "generate-loader": {
4548
return require("./generate-loader/index.js")();
4649
}

‎lib/utils/npm-packages-exists.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use strict";
22
const chalk = require("chalk");
33
const npmExists = require("./npm-exists");
4-
const resolvePackages = require("./resolve-packages");
4+
const resolvePackages = require("./resolve-packages").resolvePackages;
55

66
/**
77
*

‎lib/utils/resolve-packages.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function processPromise(child) {
3636
* a webpack configuration through yeoman or throws an error
3737
*/
3838

39-
module.exports = function resolvePackages(pkg) {
39+
function resolvePackages(pkg) {
4040
Error.stackTraceLimit = 30;
4141

4242
let packageLocations = [];
@@ -65,4 +65,9 @@ module.exports = function resolvePackages(pkg) {
6565
return creator(packageLocations);
6666
});
6767
});
68+
}
69+
70+
module.exports = {
71+
resolvePackages,
72+
processPromise
6873
};

0 commit comments

Comments
 (0)
Please sign in to comment.