Skip to content

Commit 6c766f8

Browse files
Merge pull request #974 from jamesgeorge007/feat/minor-refactor
chore(serve): refactor code to be more concise
2 parents 7978d77 + a30a027 commit 6c766f8

File tree

1 file changed

+35
-32
lines changed

1 file changed

+35
-32
lines changed

packages/serve/index.ts

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,49 @@ import * as path from "path";
66

77
import { processPromise } from "@webpack-cli/utils/resolve-packages";
88

9+
interface Commands {
10+
dependency: string[];
11+
devDependency: string[];
12+
optionalDependency: string[];
13+
}
14+
15+
interface PackageManagerConfig {
16+
[key: string]: Commands;
17+
}
18+
19+
const pmConfig: PackageManagerConfig = {
20+
npm: {
21+
dependency: ["install", "--save"],
22+
devDependency: ["install", "--save-dev"],
23+
optionalDependency: ["install", "--save-optional"]
24+
},
25+
yarn: {
26+
dependency: ["add"],
27+
devDependency: ["add", "-D"],
28+
optionalDependency: ["add", "--optional"]
29+
}
30+
};
31+
932
/**
1033
*
11-
* Installs WDS using NPM with --save --dev etc
34+
* Installs WDS using the respective package manager with corresponding commands
1235
*
13-
* @param {Object} cmd - arg to spawn with
14-
* @returns {Void}
15-
*/
16-
17-
/**
36+
* @param {String} pm - package manager to be used
37+
* @param {String} cmd - arg to spawn with
38+
* @returns {Function} spawn - installs WDS
1839
*
19-
* Installs WDS using Yarn with add etc
40+
* The dependency installation commands for the
41+
* respective package manager is available as
42+
* nested objects within pmConfig
2043
*
21-
* @param {Object} cmd - arg to spawn with
22-
* @returns {Void}
44+
* We gonna extract the root installation command
45+
* and rest of the flags from pmConfig object
46+
* by means of array destructuring
2347
*/
2448

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

0 commit comments

Comments
 (0)