Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(serve): refactor code to be more concise #974

Merged
merged 5 commits into from Jul 6, 2019
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 29 additions & 18 deletions packages/serve/index.ts
Expand Up @@ -22,30 +22,41 @@ 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<Buffer> => {
const pmConfig: ConfigType = pm === "npm" ? npmConfig : yarnConfig;
const options: string[] = [pmConfig.installCmd, "webpack-dev-server", pmConfig[cmd]];
/*
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
* 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" });
};

Expand Down