Skip to content

Commit 07edfbd

Browse files
feat(chore): refactor code to eliminate redundancy
1 parent 6c766f8 commit 07edfbd

File tree

1 file changed

+25
-42
lines changed

1 file changed

+25
-42
lines changed

packages/utils/package-manager.ts

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,6 @@ import * as spawn from "cross-spawn";
44
import * as fs from "fs";
55
import * as path from "path";
66

7-
interface SpawnFunctions {
8-
npm: (pkg: string, isNew: boolean) => SpawnSyncReturns<Buffer>;
9-
yarn: (pkg: string, isNew: boolean) => SpawnSyncReturns<Buffer>;
10-
}
11-
12-
/**
13-
*
14-
* Spawns a new process using npm
15-
*
16-
* @param {String} pkg - The dependency to be installed
17-
* @param {Boolean} isNew - indicates if it needs to be updated or installed
18-
* @returns {Function} spawn - Installs the package
19-
*/
20-
21-
function spawnNPM(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
22-
return spawn.sync("npm", [isNew ? "install" : "update", "-g", pkg], {
23-
stdio: "inherit"
24-
});
25-
}
26-
27-
/**
28-
*
29-
* Spawns a new process using yarn
30-
*
31-
* @param {String} pkg - The dependency to be installed
32-
* @param {Boolean} isNew - indicates if it needs to be updated or installed
33-
* @returns {Function} spawn - Installs the package
34-
*/
35-
36-
function spawnYarn(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
37-
return spawn.sync("yarn", ["global", isNew ? "add" : "upgrade", pkg], {
38-
stdio: "inherit"
39-
});
40-
}
41-
42-
const SPAWN_FUNCTIONS: SpawnFunctions = {
43-
npm: spawnNPM,
44-
yarn: spawnYarn
45-
};
46-
477
/**
488
*
499
* Returns the name of package manager to use,
@@ -66,6 +26,30 @@ export function getPackageManager(): string {
6626
}
6727
}
6828

29+
/**
30+
*
31+
* Spawns a new process using the respective package manager
32+
*
33+
* @param {String} pkg - The dependency to be installed
34+
* @param {Boolean} isNew - indicates if it needs to be updated or installed
35+
* @returns {Function} spawn - Installs the package
36+
*/
37+
38+
function spawnWithArg(pkg: string, isNew: boolean): SpawnSyncReturns<Buffer> {
39+
const packageManager: string = getPackageManager();
40+
let options: string[] = [];
41+
42+
if (packageManager === "npm") {
43+
options = [isNew ? "install" : "update", "-g", pkg];
44+
} else {
45+
options = ["global", isNew ? "add" : "upgrade", pkg];
46+
}
47+
48+
return spawn.sync(packageManager, options, {
49+
stdio: "inherit"
50+
});
51+
}
52+
6953
/**
7054
*
7155
* Returns the path to globally installed
@@ -101,8 +85,7 @@ export function getPathToGlobalPackages(): string {
10185
export function spawnChild(pkg: string): SpawnSyncReturns<Buffer> {
10286
const rootPath: string = getPathToGlobalPackages();
10387
const pkgPath: string = path.resolve(rootPath, pkg);
104-
const packageManager: string = getPackageManager();
10588
const isNew = !fs.existsSync(pkgPath);
10689

107-
return SPAWN_FUNCTIONS[packageManager](pkg, isNew);
90+
return spawnWithArg(pkg, isNew);
10891
}

0 commit comments

Comments
 (0)