diff --git a/lib/resin-lint.coffee b/lib/resin-lint.coffee index 785dfd9..afbb796 100644 --- a/lib/resin-lint.coffee +++ b/lib/resin-lint.coffee @@ -9,8 +9,17 @@ coffeelint = require('coffeelint') reporter = require('coffeelint/lib/reporters/default') tslint = require('tslint') -CS_CONFIG_PATH = path.join(__dirname, '../config/coffeelint.json') -TS_CONFIG_PATH = path.join(__dirname, '../config/tslint.json') +configurations = + coffeescript: + configPath: path.join(__dirname, '../config/coffeelint.json') + configFileName: 'coffeelint.json' + extensions: ['coffee'] + lang: 'coffeescript' + typescript: + configPath: path.join(__dirname, '../config/tslint.json') + configFileName: 'tslint.json' + extensions: ['ts', 'tsx'] + lang: 'typescript' # The linter expects the path to actual source files, for example: # src/ @@ -73,14 +82,41 @@ lintTsFiles = (files, config) -> linter.lint(file, source, parsedConfig) return linter.getResult() +runLint = (resinLintConfig, paths, config) -> + scripts = findFiles(resinLintConfig.extensions, paths) + + if resinLintConfig.lang is 'typescript' + errorReport = lintTsFiles(scripts, config) + + console.log(errorReport.output) + + process.on 'exit', -> + process.exit(errorReport.errorCount > 0 : 1 : 0) + + return + + if resinLintConfig.lang is 'coffeescript' + errorReport = lintCoffeeFiles(scripts, config) + + report = new reporter errorReport, + colorize: process.stdout.isTTY + quiet: false + report.publish() + + process.on 'exit', -> + process.exit(errorReport.getExitCode()) + + return + + throw new Error("Language #{resinlintConfig.language} is not supported by resin-lint.") module.exports = (passed_params) -> try options = optimist(passed_params) .usage('Usage: resin-lint [options] [...]') - .describe('f', 'Specify a coffeelint config file to override resin-lint rules') - .describe('p', 'Print default resin-lint coffeelint.json') - .describe('i', 'Ignore coffeelint.json files in project directory and its parents') + .describe('f', 'Specify a linting config file to override resin-lint rules') + .describe('p', 'Print default resin-lint linting rules') + .describe('i', 'Ignore linting config files in project directory and its parents') .describe('typescript', 'Lint typescript files instead of coffeescript') .boolean('u', 'Run unused import check') @@ -88,8 +124,6 @@ module.exports = (passed_params) -> options.showHelp() process.exit(1) - configPath = if options.argv.typescript then TS_CONFIG_PATH else CS_CONFIG_PATH - Promise.try -> if options.argv.u return Promise.map options.argv._, (dir) -> @@ -112,18 +146,20 @@ module.exports = (passed_params) -> console.log('No unused dependencies!') console.log() .then -> + + resinLintConfiguration = if options.argv.typescript then configurations.typescript else configurations.coffeescript + if options.argv.p - console.log(fs.readFileSync(configPath).toString()) + console.log(fs.readFileSync(resinLintConfiguration.configPath).toString()) process.exit(0) - config = parseJSON(configPath) + config = parseJSON(resinLintConfiguration.configPath) if options.argv.f configOverridePath = fs.realpathSync(options.argv.f) if not options.argv.i - configFileName = if options.argv.typescript then 'tslint.json' else 'coffeelint.json' - configOverridePath ?= findFile(configFileName) + configOverridePath ?= findFile(resinLintConfiguration.configFileName) if configOverridePath # Override default config configOverride = parseJSON(configOverridePath) @@ -131,26 +167,7 @@ module.exports = (passed_params) -> paths = options.argv._ - if options.argv.typescript - scripts = findFiles(['ts', 'tsx'], paths) - errorReport = lintTsFiles(scripts, config) - - console.log(errorReport.output) - - process.on 'exit', -> - process.exit(errorReport.errorCount > 0 : 1 : 0) - - else - scripts = findFiles(['coffee'], paths) - errorReport = lintCoffeeFiles(scripts, config) - - report = new reporter errorReport, - colorize: process.stdout.isTTY - quiet: false - report.publish() - - process.on 'exit', -> - process.exit(errorReport.getExitCode()) + runLint(resinLintConfiguration, paths, config) catch err console.log(err.stack)