Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli(init): revise installation steps #441

Merged
merged 3 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.