Skip to content

Commit

Permalink
cli(init): revise installation steps (#441)
Browse files Browse the repository at this point in the history
* cli(init): revise installation steps

* chore(formatting): format code

* cli(tests): refactor tests
  • Loading branch information
evenstensberg committed May 8, 2018
1 parent 51851e8 commit d0fb42c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/generators/init-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ module.exports = class InitGenerator extends Generator {
})
.then(() => {
asyncNamePrompt();
this.runInstall(getPackageManager(), this.dependencies, {
"save-dev": true
});
const packager = getPackageManager();
const opts = packager === "yarn" ? { dev: true } : { "save-dev": true };
this.runInstall(packager, this.dependencies, opts);
});
}

Expand Down
14 changes: 11 additions & 3 deletions lib/utils/package-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,19 @@ function spawnChild(pkg) {
*/

function getPackageManager() {
if (spawn.sync("yarn", [" --version"], { stdio: "ignore" }).error) {
const hasLocalNPM = fs.existsSync(
path.resolve(process.cwd(), "package-lock.json")
);
const hasLocalYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
if (hasLocalNPM) {
return "npm";
} else if (hasLocalYarn) {
return "yarn";
} else if (spawn.sync("yarn", [" --version"], { stdio: "ignore" }).error) {
return "npm";
} else {
return "yarn";
}

return "yarn";
}

/**
Expand Down
21 changes: 18 additions & 3 deletions lib/utils/package-manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ describe("package-manager", () => {
mockSpawnErrorOnce();
}

function mockUpdateYarnOnce() {
fs.existsSync.mockReturnValueOnce(false);
fs.existsSync.mockReturnValueOnce(true);
fs.existsSync.mockReturnValueOnce(false);
fs.existsSync.mockReturnValueOnce(true);
fs.existsSync.mockReturnValueOnce(true);
}

function mockUpdateNPMOnce() {
fs.existsSync.mockReturnValueOnce(true);
fs.existsSync.mockReturnValueOnce(false);
fs.existsSync.mockReturnValueOnce(true);
fs.existsSync.mockReturnValueOnce(true);
fs.existsSync.mockReturnValueOnce(true);
}

spawn.sync.mockReturnValue(defaultSyncResult);

it("should return 'yarn' from getPackageManager if it's installed", () => {
Expand All @@ -59,7 +75,7 @@ describe("package-manager", () => {
it("should spawn yarn upgrade from spawnChild", () => {
const packageName = "some-pkg";

fs.existsSync.mockReturnValueOnce(true);
mockUpdateYarnOnce();

packageManager.spawnChild(packageName);
expect(spawn.sync).toHaveBeenLastCalledWith(
Expand All @@ -84,8 +100,7 @@ describe("package-manager", () => {
it("should spawn npm update from spawnChild", () => {
const packageName = "some-pkg";

mockSpawnErrorTwice();
fs.existsSync.mockReturnValueOnce(true);
mockUpdateNPMOnce();

packageManager.spawnChild(packageName);
expect(spawn.sync).toHaveBeenLastCalledWith(
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d0fb42c

Please sign in to comment.