|
| 1 | +#!/usr/bin/env node |
| 2 | +var shell = require('shelljs'), |
| 3 | + chalk = require('chalk'), |
| 4 | + async = require('async'), |
| 5 | + ESLintCLIEngine = require('eslint').CLIEngine, |
| 6 | + |
| 7 | + /** |
| 8 | + * The list of source code files / directories to be linted. |
| 9 | + * |
| 10 | + * @type {Array} |
| 11 | + */ |
| 12 | + LINT_SOURCE_DIRS = [ |
| 13 | + './lib', |
| 14 | + './test', |
| 15 | + './npm/*.js', |
| 16 | + './index.js' |
| 17 | + ]; |
| 18 | + |
| 19 | +module.exports = function (exit) { |
| 20 | + // banner line |
| 21 | + console.info(chalk.yellow.bold('\nLinting files using eslint...')); |
| 22 | + |
| 23 | + async.waterfall([ |
| 24 | + |
| 25 | + /** |
| 26 | + * Instantiates an ESLint CLI engine and runs it in the scope defined within LINT_SOURCE_DIRS. |
| 27 | + * |
| 28 | + * @param {Function} next - The callback function whose invocation marks the end of the lint test run. |
| 29 | + * @returns {*} |
| 30 | + */ |
| 31 | + function (next) { |
| 32 | + next(null, (new ESLintCLIEngine()).executeOnFiles(LINT_SOURCE_DIRS)); |
| 33 | + }, |
| 34 | + |
| 35 | + /** |
| 36 | + * Processes a test report from the Lint test runner, and displays meaningful results. |
| 37 | + * |
| 38 | + * @param {Object} report - The overall test report for the current lint test. |
| 39 | + * @param {Object} report.results - The set of test results for the current lint run. |
| 40 | + * @param {Function} next - The callback whose invocation marks the completion of the post run tasks. |
| 41 | + * @returns {*} |
| 42 | + */ |
| 43 | + function (report, next) { |
| 44 | + var errorReport = ESLintCLIEngine.getErrorResults(report.results); |
| 45 | + // log the result to CLI |
| 46 | + console.info(ESLintCLIEngine.getFormatter()(report.results)); |
| 47 | + // log the success of the parser if it has no errors |
| 48 | + (errorReport && !errorReport.length) && console.info(chalk.green('eslint ok!')); |
| 49 | + // ensure that the exit code is non zero in case there was an error |
| 50 | + next(Number(errorReport && errorReport.length) || 0); |
| 51 | + } |
| 52 | + ], exit); |
| 53 | +}; |
| 54 | + |
| 55 | +// ensure we run this script exports if this is a direct stdin.tty run |
| 56 | +!module.parent && module.exports(shell.exit); |
0 commit comments