Skip to content
Permalink
Browse files

Fix: Improve autoconfig logging (fixes #5119)

  • Loading branch information...
IanVS committed Feb 1, 2016
1 parent 67e1743 commit a24d8ad128aa8878b3f0f169a419cfef3a25c643
@@ -285,14 +285,11 @@ Registry.prototype = {
ruleSetIdx = 0;
ruleSets.forEach(function(ruleSet) {
lintConfig = assign({}, config, {rules: ruleSet});
try {
var lintResults = eslint.verify(sourceCodes[filename], lintConfig);
lintResults.forEach(function(result) {
lintedRegistry.rules[result.ruleId][ruleSetIdx].errorCount += 1;
});
} catch (err) {
console.error(err);
}
var lintResults = eslint.verify(sourceCodes[filename], lintConfig);
lintResults.forEach(function(result) {
lintedRegistry.rules[result.ruleId][ruleSetIdx].errorCount += 1;
});

ruleSetIdx += 1;
if (cb) {
cb(totalFilesLinting); // eslint-disable-line callback-return
@@ -10,7 +10,9 @@
// Requirements
//------------------------------------------------------------------------------

var assign = require("object-assign"),
var util = require("util"),
assign = require("object-assign"),
debug = require("debug"),
inquirer = require("inquirer"),
ProgressBar = require("progress"),
autoconfig = require("./autoconfig.js"),
@@ -20,6 +22,8 @@ var assign = require("object-assign"),
recConfig = require("../../conf/eslint.json"),
log = require("../logging");

debug = debug("eslint:config-initializer");

//------------------------------------------------------------------------------
// Private
//------------------------------------------------------------------------------
@@ -71,17 +75,14 @@ function installModules(config) {
}));
}
if (config.extends && config.extends.indexOf("eslint:") === -1) {
modules.push(config.extends);
}
if (config.parser && config.parser !== "espree") {
modules.push(config.parser);
modules.push("eslint-config-" + config.extends);
}

// Determine which modules are already installed
if (modules.length === 0) {
return;
}
installStatus = npmUtil.checkInstalled(modules);
installStatus = npmUtil.checkDevDeps(modules);

// Install packages which aren't already installed
modulesToInstall = Object.keys(installStatus).filter(function(module) {
@@ -99,12 +100,12 @@ function installModules(config) {
* Note: This clones the config object and returns a new config to avoid mutating
* the original config parameter.
*
* @param {Object} answers answers received from inquirer
* @param {Object} config config object
* @param {Object} ui Inpuirer ui object, used to exit if necessary
* @returns {Object} config object with configured rules
* @param {Object} answers answers received from inquirer
* @param {Object} config config object
* @param {Object} callback function to call with any erorrs.
* @returns {Object} config object with configured rules
*/
function configureRules(answers, config, ui) {
function configureRules(answers, config, callback) {
var BAR_TOTAL = 20,
BAR_SOURCE_CODE_TOTAL = 4;

@@ -130,14 +131,18 @@ function configureRules(answers, config, ui) {

// Get the SourceCode of all chosen files
patterns = answers.patterns.split(/[\s]+/);
sourceCodes = getSourceCodeOfFiles(patterns, { baseConfig: newConfig, useEslintrc: false }, function(total) {
bar.tick((BAR_SOURCE_CODE_TOTAL / total));
});
try {
sourceCodes = getSourceCodeOfFiles(patterns, { baseConfig: newConfig, useEslintrc: false }, function(total) {
bar.tick((BAR_SOURCE_CODE_TOTAL / total));
});
} catch (e) {
log.info("\n");
return callback(e);
}
fileQty = Object.keys(sourceCodes).length;
if (!sourceCodes || fileQty === 0) {
bar.update(BAR_TOTAL);
log.error("\nFailed. No files resolved from: " + patterns);
ui.complete(); // Only way to bail out from inquirer?
if (fileQty === 0) {
log.info("\n");
return callback(new Error("Automatic Configuration failed. No files were able to be parsed."));
}

// Create a registry of rule configs
@@ -148,6 +153,7 @@ function configureRules(answers, config, ui) {
registry = registry.lintSourceCode(sourceCodes, newConfig, function(total) {
bar.tick((BAR_TOTAL - BAR_SOURCE_CODE_TOTAL) / total); // Subtract out ticks used at beginning
});
debug("\nRegistry: " + util.inspect(registry.rules, {depth: null}));

// Create a list of recommended rules, because we don't want to disable them
var recRules = Object.keys(recConfig.rules).filter(function(ruleId) {
@@ -203,10 +209,10 @@ function configureRules(answers, config, ui) {
/**
* process user's answers and create config object
* @param {Object} answers answers received from inquirer
* @param {Object} ui Inpuirer ui object, used to exit Inquirer if necessary
* @param {Object} callback function to call with any erorrs.
* @returns {Object} config object
*/
function processAnswers(answers, ui) {
function processAnswers(answers, callback) {
var config = {rules: {}, env: {}};

if (answers.es6) {
@@ -243,7 +249,7 @@ function processAnswers(answers, ui) {
installModules(config);

if (answers.source === "auto") {
config = configureRules(answers, config, ui);
config = configureRules(answers, config, callback);
config = autoconfig.extendFromRecommended(config);
}
return config;
@@ -263,6 +269,9 @@ function getConfigForStyleGuide(guide) {
if (!guides[guide]) {
throw new Error("You referenced an unsupported guide.");
}

installModules(guides[guide]);

return guides[guide];
}

@@ -274,7 +283,7 @@ function getConfigForStyleGuide(guide) {
*/
function promptUser(callback) {
var config;
var inquirerUi = inquirer.prompt([
inquirer.prompt([
{
type: "list",
name: "source",
@@ -324,7 +333,6 @@ function promptUser(callback) {
// early exit if you are using a style guide
if (earlyAnswers.source === "guide") {
config = getConfigForStyleGuide(earlyAnswers.styleguide);
installModules(config);
writeFile(config, earlyAnswers.format, callback);
return;
}
@@ -388,7 +396,7 @@ function promptUser(callback) {
return;
}
var combinedAnswers = assign({}, earlyAnswers, secondAnswers);
config = processAnswers(combinedAnswers, inquirerUi);
config = processAnswers(combinedAnswers, callback);
installModules(config);
writeFile(config, earlyAnswers.format, callback);
return;
@@ -291,7 +291,8 @@ function createCoreRuleConfigs() {
var ruleList = loadRules();
return Object.keys(ruleList).reduce(function(accumulator, id) {
var rule = rules.get(id);
accumulator[id] = generateConfigsFromSchema(rule.schema);
var schema = (typeof rule === "function") ? rule.schema : rule.meta.schema;
accumulator[id] = generateConfigsFromSchema(schema);
return accumulator;
}, {});
}
@@ -52,20 +52,23 @@ function installSyncSaveDev(packages) {
if (Array.isArray(packages)) {
packages = packages.join(" ");
}
shell.execSync("npm i --save-dev " + packages, {stdio: "inherit"});
shell.exec("npm i --save-dev " + packages, {stdio: "inherit"});
}

/**
* Check whether node modules are include in a project's package.json.
*
* @param {string[]} packages Array of node module names
* @param {Object} opt Options Object
* @returns {Object} An object whose keys are the module names
* and values are booleans indicating installation.
* @param {string[]} packages Array of node module names
* @param {Object} opt Options Object
* @param {boolean} opt.dependencies Set to true to check for direct dependencies
* @param {boolean} opt.devDependencies Set to true to check for development dependencies
* @param {boolean} opt.startdir Directory to begin searching from
* @returns {Object} An object whose keys are the module names
* and values are booleans indicating installation.
*/
function check(packages, opt) {
var deps = [];
var pkgJson = findPackageJson();
var pkgJson = (opt) ? findPackageJson(opt.startDir) : findPackageJson();
if (!pkgJson) {
throw new Error("Could not find a package.json file");
}
@@ -89,11 +92,12 @@ function check(packages, opt) {
* Convienience wrapper around check().
*
* @param {string[]} packages Array of node modules to check.
* @param {string} rootDir The directory contianing a package.json
* @returns {Object} An object whose keys are the module names
* and values are booleans indicating installation.
*/
function checkDeps(packages) {
return check(packages, {dependencies: true});
function checkDeps(packages, rootDir) {
return check(packages, {dependencies: true, startDir: rootDir});
}

/**
@@ -12,11 +12,14 @@
//------------------------------------------------------------------------------

var assign = require("object-assign"),
debug = require("debug"),
CLIEngine = require("../cli-engine"),
eslint = require("../eslint"),
globUtil = require("./glob-util"),
defaultOptions = require("../../conf/cli-options");

debug = debug("eslint:source-code-util");

//------------------------------------------------------------------------------
// Helpers
//------------------------------------------------------------------------------
@@ -25,12 +28,18 @@ var assign = require("object-assign"),
* Get the SourceCode object for a single file
* @param {string} filename The fully resolved filename to get SourceCode from.
* @param {Object} options A CLIEngine options object.
* @returns {SourceCode} The SourceCode object representing the file.
* @returns {Array} Array of the SourceCode object representing the file
* and fatal error message.
*/
function getSourceCodeOfFile(filename, options) {
debug("getting sourceCode of", filename);
var opts = assign({}, options, { rules: {}});
var cli = new CLIEngine(opts);
cli.executeOnFiles([filename]);
var results = cli.executeOnFiles([filename]);
if (results && results.results[0] && results.results[0].messages[0] && results.results[0].messages[0].fatal) {
var msg = results.results[0].messages[0];
throw new Error("(" + filename + ":" + msg.line + ":" + msg.column + ") " + msg.message);
}
var sourceCode = eslint.getSourceCode();
return sourceCode;
}
@@ -74,12 +83,17 @@ function getSourceCodeOfFiles(patterns, options, cb) {
} else if (typeof options === "object") {
opts = assign({}, defaultOptions, options);
}
debug("constructed options:", opts);

patterns = globUtil.resolveFileGlobPatterns(patterns, opts.extensions);
filenames = globUtil.listFilesToProcess(patterns, opts);
if (filenames.length === 0) {
debug("Did not find any files matching pattern(s): " + patterns);
}
filenames.forEach(function(filename) {
var sourceCode = getSourceCodeOfFile(filename, opts);
if (sourceCode) {
debug("got sourceCode of", filename);
sourceCodes[filename] = sourceCode;
}
if (cb) {
@@ -0,0 +1 @@
+;
Oops, something went wrong.

0 comments on commit a24d8ad

Please sign in to comment.
You can’t perform that action at this time.