From 3e690fb2cbb427c52cdb8836edb7eba667095b17 Mon Sep 17 00:00:00 2001 From: Kai Cataldo Date: Tue, 21 Jun 2016 13:53:20 -0400 Subject: [PATCH] Fix: Exit init early if guide is chosen w/ no package.json (fixes #6476) (#6478) --- lib/config/config-initializer.js | 11 ++++++++--- lib/util/npm-util.js | 13 ++++++++++++- tests/lib/util/npm-util.js | 22 +++++++++++++++++++++- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/config/config-initializer.js b/lib/config/config-initializer.js index 3d0e78fefe1..91d2454a8a4 100644 --- a/lib/config/config-initializer.js +++ b/lib/config/config-initializer.js @@ -46,7 +46,6 @@ function writeFile(config, format) { extname = ".json"; } - ConfigFile.write(config, "./.eslintrc" + extname); log.info("Successfully created .eslintrc" + extname + " file in " + process.cwd()); @@ -318,7 +317,8 @@ function promptUser(callback) { message: "Which style guide do you want to follow?", choices: [{name: "Google", value: "google"}, {name: "AirBnB", value: "airbnb"}, {name: "Standard", value: "standard"}], when: function(answers) { - return answers.source === "guide"; + answers.packageJsonExists = npmUtil.checkPackageJson(); + return answers.source === "guide" && answers.packageJsonExists; } }, { @@ -342,13 +342,18 @@ function promptUser(callback) { default: "JavaScript", choices: ["JavaScript", "YAML", "JSON"], when: function(answers) { - return (answers.source === "guide" || answers.source === "auto"); + return ((answers.source === "guide" && answers.packageJsonExists) || answers.source === "auto"); } } ], function(earlyAnswers) { // early exit if you are using a style guide if (earlyAnswers.source === "guide") { + if (!earlyAnswers.packageJsonExists) { + log.info("A package.json is necessary to install plugins such as style guides. Run `npm init` to create a package.json file and try again."); + return; + } + try { config = getConfigForStyleGuide(earlyAnswers.styleguide); writeFile(config, earlyAnswers.format); diff --git a/lib/util/npm-util.js b/lib/util/npm-util.js index 27120ad817d..9f28dc2b7ed 100644 --- a/lib/util/npm-util.js +++ b/lib/util/npm-util.js @@ -124,6 +124,16 @@ function checkDevDeps(packages) { return check(packages, {devDependencies: true}); } +/** + * Check whether package.json is found in current path. + * + * @param {string=} startDir Starting directory + * @returns {boolean} Whether a package.json is found in current path. + */ +function checkPackageJson(startDir) { + return !!findPackageJson(startDir); +} + //------------------------------------------------------------------------------ // Public Interface //------------------------------------------------------------------------------ @@ -131,5 +141,6 @@ function checkDevDeps(packages) { module.exports = { installSyncSaveDev: installSyncSaveDev, checkDeps: checkDeps, - checkDevDeps: checkDevDeps + checkDevDeps: checkDevDeps, + checkPackageJson: checkPackageJson }; diff --git a/tests/lib/util/npm-util.js b/tests/lib/util/npm-util.js index 09d08620cd7..2ec68346f01 100644 --- a/tests/lib/util/npm-util.js +++ b/tests/lib/util/npm-util.js @@ -13,7 +13,8 @@ var assert = require("chai").assert, shell = require("shelljs"), sinon = require("sinon"), npmUtil = require("../../../lib/util/npm-util"), - log = require("../../../lib/logging"); + log = require("../../../lib/logging"), + mockFs = require("mock-fs"); //------------------------------------------------------------------------------ // Tests @@ -168,6 +169,25 @@ describe("npmUtil", function() { }); }); + describe("checkPackageJson()", function() { + after(function() { + mockFs.restore(); + }); + + it("should return true if package.json exists", function() { + mockFs({ + "package.json": "{ \"file\": \"contents\" }" + }); + + assert.equal(npmUtil.checkPackageJson(), true); + }); + + it("should return false if package.json does not exist", function() { + mockFs({}); + assert.equal(npmUtil.checkPackageJson(), false); + }); + }); + describe("installSyncSaveDev()", function() { it("should invoke npm to install a single desired package", function() { var stub = sandbox.stub(shell, "exec");