Skip to content

Commit

Permalink
feat(chore): refactor code to eliminate redundancy
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesgeorge007 committed Jun 25, 2019
1 parent 30b1b8d commit 6170fd0
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions packages/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,6 @@ import * as spawn from "cross-spawn";
import * as fs from "fs";
import * as path from "path";

interface SpawnFunctions {
npm: (pkg: string, isNew: boolean) => SpawnSyncReturns<Buffer>;
yarn: (pkg: string, isNew: boolean) => SpawnSyncReturns<Buffer>;
}

/**
*
* Spawns a new process using npm
*
* @param {String} pkg - The dependency to be installed
* @param {Boolean} isNew - indicates if it needs to be updated or installed
* @returns {Function} spawn - Installs the package
*/

function spawnNPM(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
return spawn.sync("npm", [isNew ? "install" : "update", "-g", pkg], {
stdio: "inherit"
});
}

/**
*
* Spawns a new process using yarn
*
* @param {String} pkg - The dependency to be installed
* @param {Boolean} isNew - indicates if it needs to be updated or installed
* @returns {Function} spawn - Installs the package
*/

function spawnYarn(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
return spawn.sync("yarn", ["global", isNew ? "add" : "upgrade", pkg], {
stdio: "inherit"
});
}

const SPAWN_FUNCTIONS: SpawnFunctions = {
npm: spawnNPM,
yarn: spawnYarn
};

/**
*
* Returns the name of package manager to use,
Expand All @@ -66,6 +26,30 @@ export function getPackageManager(): string {
}
}

/**
*
* Spawns a new process using the respective package manager
*
* @param {String} pkg - The dependency to be installed
* @param {Boolean} isNew - indicates if it needs to be updated or installed
* @returns {Function} spawn - Installs the package
*/

function spawnWithArg(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
const packageManager: string = getPackageManager();
let options: string[] = [];

if (packageManager === "npm") {
options = [isNew ? "install" : "update", "-g", pkg];
} else {
options = ["global", isNew ? "add" : "upgrade", pkg];
}

return spawn.sync(packageManager, options, {
stdio: "inherit"
});
}

/**
*
* Returns the path to globally installed
Expand Down Expand Up @@ -101,8 +85,7 @@ export function getPathToGlobalPackages(): string {
export function spawnChild(pkg: string): SpawnSyncReturns<Buffer> {
const rootPath: string = getPathToGlobalPackages();
const pkgPath: string = path.resolve(rootPath, pkg);
const packageManager: string = getPackageManager();
const isNew = !fs.existsSync(pkgPath);

return SPAWN_FUNCTIONS[packageManager](pkg, isNew);
return spawnWithArg(pkg, isNew);
}

0 comments on commit 6170fd0

Please sign in to comment.