-
-
Notifications
You must be signed in to change notification settings - Fork 619
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
acb6285
commit 21ebcbe
Showing
5 changed files
with
170 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use strict"; | ||
|
||
const path = require("path"); | ||
const fs = require("fs"); | ||
const spawn = require("cross-spawn"); | ||
const globalPath = require("global-modules"); | ||
|
||
const SPAWN_FUNCTIONS = { | ||
npm: spawnNPM, | ||
yarn: spawnYarn | ||
}; | ||
|
||
function spawnNPM(pkg, isNew) { | ||
return spawn.sync("npm", [isNew ? "install" : "update", "-g", pkg], { | ||
stdio: "inherit" | ||
}); | ||
} | ||
|
||
function spawnYarn(pkg, isNew) { | ||
return spawn.sync("yarn", ["global", isNew ? "add" : "upgrade", pkg], { | ||
stdio: "inherit" | ||
}); | ||
} | ||
/* | ||
* @function spawnChild | ||
* | ||
* Spawns a new process that installs the addon/dependency | ||
* | ||
* @param { String } pkg - The dependency to be installed | ||
* @returns { <Function> } spawn - Installs the package | ||
*/ | ||
|
||
function spawnChild(pkg) { | ||
const pkgPath = path.resolve(globalPath, pkg); | ||
const packageManager = getPackageManager(); | ||
const isNew = !fs.existsSync(pkgPath); | ||
|
||
return SPAWN_FUNCTIONS[packageManager](pkg, isNew); | ||
} | ||
|
||
/* | ||
* @function getPackageManager | ||
* | ||
* Returns the name of package manager to use, | ||
* preferring yarn over npm if available | ||
* | ||
* @returns { String } - The package manager name | ||
*/ | ||
|
||
function getPackageManager() { | ||
if (spawn.sync("yarn", [" --version"], { stdio: "ignore" }).error) { | ||
return "npm"; | ||
} | ||
|
||
return "yarn"; | ||
} | ||
|
||
module.exports = { | ||
getPackageManager, | ||
spawnChild | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"use strict"; | ||
|
||
jest.mock("cross-spawn"); | ||
jest.mock("fs"); | ||
|
||
describe("package-manager", () => { | ||
const packageManager = require("./package-manager"); | ||
const spawn = require("cross-spawn"); | ||
const fs = require("fs"); | ||
|
||
const defaultSyncResult = { | ||
pid: 1234, | ||
output: [null, null, null], | ||
stdout: null, | ||
stderr: null, | ||
signal: null, | ||
status: 1, | ||
error: null | ||
}; | ||
|
||
function mockSpawnErrorOnce() { | ||
spawn.sync.mockReturnValueOnce( | ||
Object.assign({}, defaultSyncResult, { | ||
status: null, | ||
error: new Error() | ||
}) | ||
); | ||
} | ||
|
||
spawn.sync.mockReturnValue(defaultSyncResult); | ||
|
||
it("should return 'yarn' from getPackageManager if it's installed", () => { | ||
expect(packageManager.getPackageManager()).toEqual("yarn"); | ||
}); | ||
|
||
it("should return 'npm' from getPackageManager if yarn is not installed", () => { | ||
mockSpawnErrorOnce(); | ||
expect(packageManager.getPackageManager()).toEqual("npm"); | ||
}); | ||
|
||
it("should spawn yarn add from spawnChild", () => { | ||
const packageName = "some-pkg"; | ||
|
||
packageManager.spawnChild(packageName); | ||
expect(spawn.sync).toHaveBeenLastCalledWith( | ||
"yarn", | ||
["global", "add", packageName], | ||
{ stdio: "inherit" } | ||
); | ||
}); | ||
|
||
it("should spawn yarn upgrade from spawnChild", () => { | ||
const packageName = "some-pkg"; | ||
|
||
fs.existsSync.mockReturnValueOnce(true); | ||
|
||
packageManager.spawnChild(packageName); | ||
expect(spawn.sync).toHaveBeenLastCalledWith( | ||
"yarn", | ||
["global", "upgrade", packageName], | ||
{ stdio: "inherit" } | ||
); | ||
}); | ||
|
||
it("should spawn npm install from spawnChild", () => { | ||
const packageName = "some-pkg"; | ||
|
||
mockSpawnErrorOnce(); | ||
packageManager.spawnChild(packageName); | ||
expect(spawn.sync).toHaveBeenLastCalledWith( | ||
"npm", | ||
["install", "-g", packageName], | ||
{ stdio: "inherit" } | ||
); | ||
}); | ||
|
||
it("should spawn npm update from spawnChild", () => { | ||
const packageName = "some-pkg"; | ||
|
||
mockSpawnErrorOnce(); | ||
fs.existsSync.mockReturnValueOnce(true); | ||
|
||
packageManager.spawnChild(packageName); | ||
expect(spawn.sync).toHaveBeenLastCalledWith( | ||
"npm", | ||
["update", "-g", packageName], | ||
{ stdio: "inherit" } | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters