Skip to content

Commit

Permalink
Addressing PR review
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianBuzzo committed Dec 22, 2017
1 parent 960e2fe commit 10a0374
Showing 1 changed file with 48 additions and 31 deletions.
79 changes: 48 additions & 31 deletions lib/resin-lint.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down Expand Up @@ -73,23 +82,48 @@ 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')

if options.argv._.length < 1 and not options.argv.p
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) ->
Expand All @@ -112,45 +146,28 @@ 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)
config = merge.recursive(config, configOverride)

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)

0 comments on commit 10a0374

Please sign in to comment.