Skip to content

Commit

Permalink
Merge pull request #1133 from sasstools/feature/fix-output
Browse files Browse the repository at this point in the history
Fix output
  • Loading branch information
DanPurdy committed Sep 7, 2017
2 parents e3d0d9a + 64cec28 commit deabaf6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 28 deletions.
42 changes: 23 additions & 19 deletions bin/sass-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,13 @@ var tooManyWarnings = function (detects, userConfig) {
return warningCount > 0 && warningCount > userConfig.options['max-warnings'];
};

var detectPattern = function (pattern, userConfig) {
var detectPattern = function (pattern) {
var detects = lint.lintFiles(pattern, configOptions, configPath);

if (program.verbose) {
lint.outputResults(detects, configOptions, configPath);
}

if (lint.errorCount(detects).count || tooManyWarnings(detects, userConfig)) {
exitCode = 1;
}

if (program.exit) {
lint.failOnError(detects, configOptions, configPath);
}
return detects;
};

program
Expand Down Expand Up @@ -72,17 +65,28 @@ if (program.maxWarnings && program.maxWarnings !== true) {
configOptions.options['max-warnings'] = program.maxWarnings;
}

// load our config here so we only load it once for each file
config = lint.getConfig(configOptions, configPath);
(function () {
// load our config here so we only load it once for each file
config = lint.getConfig(configOptions, configPath);
var results = [];

if (program.args.length === 0) {
detectPattern(null, config);
}
else {
program.args.forEach(function (path) {
detectPattern(path, config);
});
}
if (program.args.length === 0) {
results = results.concat(detectPattern(null));
}
else {
program.args.forEach(function (path) {
results = results.concat(detectPattern(path));
});
}

if (program.verbose) {
lint.outputResults(results, configOptions, configPath);
}

if (lint.errorCount(results).count || tooManyWarnings(results, config)) {
exitCode = 1;
}
}());

process.on('exit', function () {
process.exit(exitCode); // eslint-disable-line no-process-exit
Expand Down
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sassLint.getConfig = function (config, configPath) {
* Parses our results object to count errors and return
* paths to files with detected errors.
*
* @param {object} results our results object
* @param {Array} results our results Array
* @returns {object} errors object containing the error count and paths for files incl. errors
*/
sassLint.errorCount = function (results) {
Expand All @@ -58,7 +58,7 @@ sassLint.errorCount = function (results) {
* Parses our results object to count warnings and return
* paths to files with detected warnings.
*
* @param {object} results our results object
* @param {Array} results our results array
* @returns {object} warnings object containing the error count and paths for files incl. warnings
*/
sassLint.warningCount = function (results) {
Expand All @@ -81,7 +81,7 @@ sassLint.warningCount = function (results) {
* Parses our results object to count warnings and errors and return
* a cumulative count of both
*
* @param {object} results our results object
* @param {Array} results our results array
* @returns {int} the cumulative count of errors and warnings detected
*/
sassLint.resultCount = function (results) {
Expand Down Expand Up @@ -188,7 +188,7 @@ sassLint.lintFileText = function (file, options, configPath) {
* @param {string} files a glob pattern or single file path as a lint target
* @param {object} options user specified rules/options passed in
* @param {string} configPath path to a config file
* @returns {object} results object containing all results
* @returns {Array} results object containing all results
*/
sassLint.lintFiles = function (files, options, configPath) {
var that = this,
Expand Down Expand Up @@ -245,10 +245,10 @@ sassLint.lintFiles = function (files, options, configPath) {
/**
* Handles formatting of results using EsLint formatters
*
* @param {object} results our results object
* @param {Array} results our results array
* @param {object} options user specified rules/options passed in
* @param {string} configPath path to a config file
* @returns {object} results our results object in the user specified format
* @returns {string} results our results object in the user specified format
*/
sassLint.format = function (results, options, configPath) {
var config = this.getConfig(options, configPath),
Expand All @@ -263,10 +263,10 @@ sassLint.format = function (results, options, configPath) {
* Handles outputting results whether this be straight to the console/stdout or to a file.
* Passes results to the format function to ensure results are output in the chosen format
*
* @param {object} results our results object
* @param {Array} results our results array
* @param {object} options user specified rules/options passed in
* @param {string} configPath path to a config file
* @returns {object} results our results object
* @returns {string} the formatted results string
*/
sassLint.outputResults = function (results, options, configPath) {
var config = this.getConfig(options, configPath);
Expand Down Expand Up @@ -295,7 +295,7 @@ sassLint.outputResults = function (results, options, configPath) {
* Throws an error if there are any errors detected. The error includes a count of all errors
* and a list of all files that include errors.
*
* @param {object} results - our results object
* @param {Array} results - our results array
* @param {object} [options] - extra options to use when running failOnError, e.g. max-warnings
* @param {string} [configPath] - path to the config file
* @returns {void}
Expand Down
23 changes: 23 additions & 0 deletions tests/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ describe('cli', function () {
});
});

it('CLI format option should output valid JSON', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/*.scss --verbose --format json';

exec(command, function (err, stdout) {

if (err) {
return done(err);
}
else {
try {
var result = JSON.parse(stdout);
if (result && result.length && result.length > 1) {
return done();
}
return done(new Error('Output is not combined'));
}
catch (e) {
return done(new Error('Not JSON'));
}
}
});
});

it('CLI output option should write to test file', function (done) {
var command = 'node bin/sass-lint -c tests/yml/.stylish-output.yml tests/cli/cli.scss --verbose --format json --output tests/cli-output.json',
outputFile = path.resolve(process.cwd(), 'tests/cli-output.json');
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/cli-warn.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.cli {
color: red;
}

0 comments on commit deabaf6

Please sign in to comment.