Skip to content

Commit

Permalink
feat(exit): non-zero exit code for getUnusedRules (#34)
Browse files Browse the repository at this point in the history
use -n|no-error to suppress
fixed #31
  • Loading branch information
sarbbottam committed Apr 14, 2016
1 parent 7d50552 commit adb8505
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,20 @@ The intended usage is as an npm script:
{
...
"scripts": {
"eslint-find-option-rules": "eslint-find-rules [option] <file>"
"eslint-find-option-rules": "eslint-find-rules [option] <file> [flag]"
}
...
}
```

```
available options are -c|--current, -a|--all-available, -p|--plugin, -u|--unused
available flag is -n|--no-error
```

By default it will error out only for `-u|--unused`,
however if you do not want the `process` to `exit` with a `non-zero` exit code, use the `-n|--no-error` along with `-u|--unused`

Then run it with: `$ npm run eslint-find-option-rules -s` (the `-s` is to silence npm output).

### Specify a file
Expand Down
11 changes: 9 additions & 2 deletions src/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,32 @@ var options = {
getPluginRules: ['plugin', 'p'],
getAllAvailableRules: ['all-available', 'a'],
getUnusedRules: ['unused', 'u'],
n: ['no-error'],
}

var argv = require('yargs')
.boolean(Object.keys(options))
.alias(options)
.argv

var processExitCode = argv.u && !argv.n ? 1 : 0
var getRuleFinder = require('./rule-finder')
var specifiedFile = argv._[0]

var ruleFinder = getRuleFinder(specifiedFile)
Object.keys(options).forEach(function findRules(option) {
var rules
if (argv[option]) {
rules = ruleFinder[option]()
var ruleFinderMethod = ruleFinder[option]
if (argv[option] && ruleFinderMethod) {
rules = ruleFinderMethod()
/* istanbul ignore next */
if (rules.length) {
console.log('\n' + options[option][0], 'rules\n') // eslint-disable-line no-console
console.log(rules.join(', ')) // eslint-disable-line no-console
}
}
})

if (processExitCode) {
process.exit(processExitCode)
}
16 changes: 16 additions & 0 deletions test/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var proxyquire = require('proxyquire')
var sinon = require('sinon')

var consoleLog = console.log // eslint-disable-line no-console
var processExit = process.exit

var getCurrentRules = sinon.stub().returns(['current'])
var getPluginRules = sinon.stub().returns(['plugin'])
Expand All @@ -28,11 +29,13 @@ describe('bin', function() {
}
consoleLog.apply(null, arguments)
}
process.exit = function noop() {}
process.argv = process.argv.slice(0, 2)
})

afterEach(function() {
console.log = consoleLog // eslint-disable-line no-console
process.exit = processExit
// purge yargs cache
delete require.cache[require.resolve('yargs')]
})
Expand All @@ -56,7 +59,20 @@ describe('bin', function() {
})

it('option -u|--unused', function() {
process.exit = function(status) {
assert.equal(status, 1)
}
process.argv[2] = '-u'
proxyquire('../src/bin', stub)
assert.ok(getUnusedRules.called)
})

it('option -u|--unused along with -n|no-error', function() {
process.exit = function(status) {
assert.equal(status, 0)
}
process.argv[2] = '-u'
process.argv[3] = '-n'
proxyquire('../src/bin', stub)
assert.ok(getUnusedRules.called)
})
Expand Down

0 comments on commit adb8505

Please sign in to comment.