Skip to content

Commit

Permalink
Fix: Exit init early if guide is chosen w/ no package.json (fixes #6476
Browse files Browse the repository at this point in the history
…) (#6478)
  • Loading branch information
kaicataldo authored and nzakas committed Jun 21, 2016
1 parent 031a356 commit 3e690fb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
11 changes: 8 additions & 3 deletions lib/config/config-initializer.js
Expand Up @@ -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());

Expand Down Expand Up @@ -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;
}
},
{
Expand All @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion lib/util/npm-util.js
Expand Up @@ -124,12 +124,23 @@ 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
//------------------------------------------------------------------------------

module.exports = {
installSyncSaveDev: installSyncSaveDev,
checkDeps: checkDeps,
checkDevDeps: checkDevDeps
checkDevDeps: checkDevDeps,
checkPackageJson: checkPackageJson
};
22 changes: 21 additions & 1 deletion tests/lib/util/npm-util.js
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3e690fb

Please sign in to comment.