From d2e3e808ab63e2030acc0b76baafe68a4df66524 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 26 Jun 2019 11:29:21 +0530 Subject: [PATCH 1/5] chore(serve): refactor code to be more concise --- packages/serve/index.ts | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/packages/serve/index.ts b/packages/serve/index.ts index c8f471ba551..e3b51e72c8e 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -22,30 +22,33 @@ import { processPromise } from "@webpack-cli/utils/resolve-packages"; * @returns {Void} */ -interface ConfigType { - installCmd: string; - dependency: string; - devDependency: string; - optionalDependency: string; +interface Commands { + dependency: string[]; + devDependency: string[]; + optionalDependency: string[]; } -const npmConfig: ConfigType = { - installCmd: "install", - dependency: "--save", - devDependency: "--save-dev", - optionalDependency: "--save-optional" -}; +interface PackageManagerConfig { + [key: string]: Commands; +} -const yarnConfig: ConfigType = { - installCmd: "add", - dependency: " ", - devDependency: "--save", - optionalDependency: "--optional" +const pmConfig: PackageManagerConfig = { + npm: { + dependency: ["install", "--save"], + devDependency: ["install", "--save-dev"], + optionalDependency: ["install", "--save-optional"] + }, + yarn: { + dependency: ["add"], + devDependency: ["add", "-D"], + optionalDependency: ["add", "--optional"] + } }; const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns => { - const pmConfig: ConfigType = pm === "npm" ? npmConfig : yarnConfig; - const options: string[] = [pmConfig.installCmd, "webpack-dev-server", pmConfig[cmd]]; + const [installCmd, ...flags] = pmConfig[pm][cmd]; + const options: string[] = [installCmd, "webpack-dev-server", ...flags]; + return spawn.sync(pm, options, { stdio: "inherit" }); }; From 0d9aa9ac7868f0154209eb119b6244df55859af7 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Wed, 26 Jun 2019 11:39:22 +0530 Subject: [PATCH 2/5] fix: minor fix --- packages/serve/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/serve/index.ts b/packages/serve/index.ts index e3b51e72c8e..d7771647fe1 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -48,7 +48,6 @@ const pmConfig: PackageManagerConfig = { const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns => { const [installCmd, ...flags] = pmConfig[pm][cmd]; const options: string[] = [installCmd, "webpack-dev-server", ...flags]; - return spawn.sync(pm, options, { stdio: "inherit" }); }; From 941da90ebfcb6aa5ba07430465bf2d53a2c54c4f Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sat, 29 Jun 2019 17:38:09 +0530 Subject: [PATCH 3/5] chore: include comments --- packages/serve/index.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/serve/index.ts b/packages/serve/index.ts index d7771647fe1..3f8bde6a369 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -46,6 +46,15 @@ const pmConfig: PackageManagerConfig = { }; const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns => { + /* + * The dependency installation commands for the + * respective package manager is available as + * nested objects within pmConfig + * + * We gonna extract the root installation command + * and rest of the flags from pmConfig object + * by means of array destructuring + */ const [installCmd, ...flags] = pmConfig[pm][cmd]; const options: string[] = [installCmd, "webpack-dev-server", ...flags]; return spawn.sync(pm, options, { stdio: "inherit" }); From 7553ae76b6a2f84cb5cb69f73f1eb3613020775f Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sun, 30 Jun 2019 14:33:02 +0530 Subject: [PATCH 4/5] fix: update comments --- packages/serve/index.ts | 42 +++++++++++++++++------------------------ 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/packages/serve/index.ts b/packages/serve/index.ts index 3f8bde6a369..0a37e8eb915 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -6,22 +6,6 @@ import * as path from "path"; import { processPromise } from "@webpack-cli/utils/resolve-packages"; -/** - * - * Installs WDS using NPM with --save --dev etc - * - * @param {Object} cmd - arg to spawn with - * @returns {Void} - */ - -/** - * - * Installs WDS using Yarn with add etc - * - * @param {Object} cmd - arg to spawn with - * @returns {Void} - */ - interface Commands { dependency: string[]; devDependency: string[]; @@ -45,16 +29,24 @@ const pmConfig: PackageManagerConfig = { } }; +/** + * + * Installs WDS using the respective package manager with add etc + * + * @param {String} pm - package manager to be used + * @param {String} cmd - arg to spawn with + * @returns {Function} spawn - installs WDS + * + * The dependency installation commands for the + * respective package manager is available as + * nested objects within pmConfig + * + * We gonna extract the root installation command + * and rest of the flags from pmConfig object + * by means of array destructuring + */ + const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns => { - /* - * The dependency installation commands for the - * respective package manager is available as - * nested objects within pmConfig - * - * We gonna extract the root installation command - * and rest of the flags from pmConfig object - * by means of array destructuring - */ const [installCmd, ...flags] = pmConfig[pm][cmd]; const options: string[] = [installCmd, "webpack-dev-server", ...flags]; return spawn.sync(pm, options, { stdio: "inherit" }); From a30a02716c50b1c52c223c42eabe5dd1cbe29577 Mon Sep 17 00:00:00 2001 From: jamesgeorge007 Date: Sun, 30 Jun 2019 14:35:21 +0530 Subject: [PATCH 5/5] fix: minor refactor --- packages/serve/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/serve/index.ts b/packages/serve/index.ts index 0a37e8eb915..2f0a930e37b 100644 --- a/packages/serve/index.ts +++ b/packages/serve/index.ts @@ -31,7 +31,7 @@ const pmConfig: PackageManagerConfig = { /** * - * Installs WDS using the respective package manager with add etc + * Installs WDS using the respective package manager with corresponding commands * * @param {String} pm - package manager to be used * @param {String} cmd - arg to spawn with