Skip to content

Commit d2c7ae4

Browse files
committedJun 4, 2019
chore: refactor serve command action handler
1 parent 71a58b6 commit d2c7ae4

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed
 

‎packages/serve/index.ts

+30-32
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ import { List } from "@webpack-cli/webpack-scaffold";
1515
* @returns {Void}
1616
*/
1717

18-
const spawnNPMWithArg = (cmd: string): SpawnSyncReturns<Buffer> =>
19-
spawn.sync("npm", ["install", "webpack-dev-server", cmd], {
20-
stdio: "inherit"
21-
});
22-
2318
/**
2419
*
2520
* Installs WDS using Yarn with add etc
@@ -28,10 +23,32 @@ const spawnNPMWithArg = (cmd: string): SpawnSyncReturns<Buffer> =>
2823
* @returns {Void}
2924
*/
3025

31-
const spawnYarnWithArg = (cmd: string): SpawnSyncReturns<Buffer> =>
32-
spawn.sync("yarn", ["add", "webpack-dev-server", cmd], {
33-
stdio: "inherit"
34-
});
26+
interface ConfigType {
27+
installCmd: string,
28+
dependency: string,
29+
devDependency: string,
30+
optionalDependency: string
31+
};
32+
33+
const npmConfig: ConfigType = {
34+
installCmd: 'install',
35+
dependency: '--save',
36+
devDependency: '--save-dev',
37+
optionalDependency: '--save-optional'
38+
};
39+
40+
const yarnConfig: ConfigType = {
41+
installCmd: 'add',
42+
dependency: ' ',
43+
devDependency: '--save',
44+
optionalDependency: '--optinal'
45+
};
46+
47+
const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns<Buffer> => {
48+
const pmConfig: ConfigType = pm === 'npm' ? npmConfig : yarnConfig;
49+
const options: string[] = [pmConfig['installCmd'], "webpack-dev-server", pmConfig[cmd]];
50+
spawn.sync(pm, options);
51+
};
3552

3653
/**
3754
*
@@ -117,29 +134,10 @@ export default function serve(): Promise<void | Function> {
117134
(depTypeAns: { confirmDepType: string }): Promise<void | Function> => {
118135
const packager: string = getRootPathModule("package-lock.json") ? "npm" : "yarn";
119136
let spawnAction: () => SpawnSyncReturns<Buffer>;
120-
if (depTypeAns.confirmDepType === "devDependency") {
121-
if (packager === "yarn") {
122-
spawnAction = (): SpawnSyncReturns<Buffer> => spawnYarnWithArg("--dev");
123-
} else {
124-
spawnAction = (): SpawnSyncReturns<Buffer> => spawnNPMWithArg("--save-dev");
125-
}
126-
}
127-
if (depTypeAns.confirmDepType === "dependency") {
128-
if (packager === "yarn") {
129-
spawnAction = (): SpawnSyncReturns<Buffer> => spawnYarnWithArg(" ");
130-
} else {
131-
spawnAction = (): SpawnSyncReturns<Buffer> => spawnNPMWithArg("--save");
132-
}
133-
}
134-
if (depTypeAns.confirmDepType === "optionalDependency") {
135-
if (packager === "yarn") {
136-
spawnAction = (): SpawnSyncReturns<Buffer> =>
137-
spawnYarnWithArg("--optional");
138-
} else {
139-
spawnAction = (): SpawnSyncReturns<Buffer> =>
140-
spawnNPMWithArg("--save-optional");
141-
}
142-
}
137+
138+
spawnAction = (): SpawnSyncReturns<Buffer> =>
139+
spawnWithArg(packager, depTypeAns.confirmDepType);
140+
143141
return processPromise(spawnAction()).then(
144142
(): Promise<void | Function> => {
145143
// Recursion doesn't work well with require call being cached

0 commit comments

Comments
 (0)
Please sign in to comment.