Skip to content

Commit

Permalink
Merge pull request #974 from jamesgeorge007/feat/minor-refactor
Browse files Browse the repository at this point in the history
chore(serve): refactor code to be more concise
  • Loading branch information
evenstensberg committed Jul 6, 2019
2 parents 7978d77 + a30a027 commit 6c766f8
Showing 1 changed file with 35 additions and 32 deletions.
67 changes: 35 additions & 32 deletions packages/serve/index.ts
Expand Up @@ -6,46 +6,49 @@ import * as path from "path";

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

interface Commands {
dependency: string[];
devDependency: string[];
optionalDependency: string[];
}

interface PackageManagerConfig {
[key: string]: Commands;
}

const pmConfig: PackageManagerConfig = {
npm: {
dependency: ["install", "--save"],
devDependency: ["install", "--save-dev"],
optionalDependency: ["install", "--save-optional"]
},
yarn: {
dependency: ["add"],
devDependency: ["add", "-D"],
optionalDependency: ["add", "--optional"]
}
};

/**
*
* Installs WDS using NPM with --save --dev etc
* Installs WDS using the respective package manager with corresponding commands
*
* @param {Object} cmd - arg to spawn with
* @returns {Void}
*/

/**
* @param {String} pm - package manager to be used
* @param {String} cmd - arg to spawn with
* @returns {Function} spawn - installs WDS
*
* Installs WDS using Yarn with add etc
* The dependency installation commands for the
* respective package manager is available as
* nested objects within pmConfig
*
* @param {Object} cmd - arg to spawn with
* @returns {Void}
* We gonna extract the root installation command
* and rest of the flags from pmConfig object
* by means of array destructuring
*/

interface ConfigType {
installCmd: string;
dependency: string;
devDependency: string;
optionalDependency: string;
}

const npmConfig: ConfigType = {
installCmd: "install",
dependency: "--save",
devDependency: "--save-dev",
optionalDependency: "--save-optional"
};

const yarnConfig: ConfigType = {
installCmd: "add",
dependency: " ",
devDependency: "--save",
optionalDependency: "--optional"
};

const spawnWithArg = (pm: string, cmd: string): SpawnSyncReturns<Buffer> => {
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" });
};

Expand Down

0 comments on commit 6c766f8

Please sign in to comment.