Skip to content

Commit d0fb42c

Browse files
cli(init): revise installation steps (#441)
* cli(init): revise installation steps * chore(formatting): format code * cli(tests): refactor tests
1 parent 51851e8 commit d0fb42c

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

lib/generators/init-generator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,9 @@ module.exports = class InitGenerator extends Generator {
409409
})
410410
.then(() => {
411411
asyncNamePrompt();
412-
this.runInstall(getPackageManager(), this.dependencies, {
413-
"save-dev": true
414-
});
412+
const packager = getPackageManager();
413+
const opts = packager === "yarn" ? { dev: true } : { "save-dev": true };
414+
this.runInstall(packager, this.dependencies, opts);
415415
});
416416
}
417417

lib/utils/package-manager.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ function spawnChild(pkg) {
6464
*/
6565

6666
function getPackageManager() {
67-
if (spawn.sync("yarn", [" --version"], { stdio: "ignore" }).error) {
67+
const hasLocalNPM = fs.existsSync(
68+
path.resolve(process.cwd(), "package-lock.json")
69+
);
70+
const hasLocalYarn = fs.existsSync(path.resolve(process.cwd(), "yarn.lock"));
71+
if (hasLocalNPM) {
6872
return "npm";
73+
} else if (hasLocalYarn) {
74+
return "yarn";
75+
} else if (spawn.sync("yarn", [" --version"], { stdio: "ignore" }).error) {
76+
return "npm";
77+
} else {
78+
return "yarn";
6979
}
70-
71-
return "yarn";
7280
}
7381

7482
/**

lib/utils/package-manager.test.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ describe("package-manager", () => {
3434
mockSpawnErrorOnce();
3535
}
3636

37+
function mockUpdateYarnOnce() {
38+
fs.existsSync.mockReturnValueOnce(false);
39+
fs.existsSync.mockReturnValueOnce(true);
40+
fs.existsSync.mockReturnValueOnce(false);
41+
fs.existsSync.mockReturnValueOnce(true);
42+
fs.existsSync.mockReturnValueOnce(true);
43+
}
44+
45+
function mockUpdateNPMOnce() {
46+
fs.existsSync.mockReturnValueOnce(true);
47+
fs.existsSync.mockReturnValueOnce(false);
48+
fs.existsSync.mockReturnValueOnce(true);
49+
fs.existsSync.mockReturnValueOnce(true);
50+
fs.existsSync.mockReturnValueOnce(true);
51+
}
52+
3753
spawn.sync.mockReturnValue(defaultSyncResult);
3854

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

62-
fs.existsSync.mockReturnValueOnce(true);
78+
mockUpdateYarnOnce();
6379

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

87-
mockSpawnErrorTwice();
88-
fs.existsSync.mockReturnValueOnce(true);
103+
mockUpdateNPMOnce();
89104

90105
packageManager.spawnChild(packageName);
91106
expect(spawn.sync).toHaveBeenLastCalledWith(

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)