diff --git a/lib/generators/init-generator.js b/lib/generators/init-generator.js index efd7db24439..2e3b07c00d7 100644 --- a/lib/generators/init-generator.js +++ b/lib/generators/init-generator.js @@ -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); }); } diff --git a/lib/utils/package-manager.js b/lib/utils/package-manager.js index 819f3ba0a35..cb67afa9539 100644 --- a/lib/utils/package-manager.js +++ b/lib/utils/package-manager.js @@ -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"; } /** diff --git a/lib/utils/package-manager.test.js b/lib/utils/package-manager.test.js index c99fa0bbf79..b0d5ee6c4ed 100644 --- a/lib/utils/package-manager.test.js +++ b/lib/utils/package-manager.test.js @@ -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", () => { @@ -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( @@ -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( diff --git a/package-lock.json b/package-lock.json index fa5139cf6d8..aec79ae11cb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "webpack-cli", - "version": "2.1.3", + "version": "0.0.0-development", "lockfileVersion": 1, "requires": true, "dependencies": {