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 all 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
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