Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bin): Improve output #42

Merged
merged 3 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
],
"license": "MIT",
"dependencies": {
"cliui": "^3.2.0",
"path-is-absolute": "1.0.0",
"window-size": "^0.2.0",
"yargs": "^4.4.0"
},
"devDependencies": {
Expand Down
37 changes: 33 additions & 4 deletions src/bin/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,45 @@

'use strict'

var size = require('window-size')
var availableWidth = size.width || /* istanbul ignore next */ 80
var ui = require('cliui')({width: availableWidth})
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Available width for tabular display is calculated with respect to individual terminal (window) settings.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!! responsive ui 👍


var getRuleFinder = require('../lib/rule-finder')
var difference = require('../lib/array-diff')
var sortRules = require('../lib/sort-rules')

var rules = difference(
getRuleFinder(process.argv[2]).getCurrentRules(),
getRuleFinder(process.argv[3]).getCurrentRules()
var rules = sortRules(
difference(
getRuleFinder(process.argv[2]).getCurrentRules(),
getRuleFinder(process.argv[3]).getCurrentRules()
)
)

var outputRules, outputRuleCellMapper
var outputPadding = ' '
var outputMaxWidth = 0
var outputMaxCols = 0

/* istanbul ignore next */
if (rules.length) {
console.log('\n diff rules\n') // eslint-disable-line no-console
console.log(rules.join(', ')) // eslint-disable-line no-console
rules = rules.map(function columnSpecification(rule) {
rule = rule + outputPadding
outputMaxWidth = Math.max(rule.length, outputMaxWidth)
return rule
})
outputMaxCols = Math.floor(availableWidth / outputMaxWidth)
outputRuleCellMapper = getOutputRuleCellMapper(Math.floor(availableWidth / outputMaxCols))
while (rules.length) {
outputRules = rules.splice(0, outputMaxCols).map(outputRuleCellMapper)
ui.div.apply(ui, outputRules)
}
console.log(ui.toString()) // eslint-disable-line no-console
}

function getOutputRuleCellMapper(width) {
return function curriedOutputRuleCellMapper(rule) {
return {text: rule, width: width}
}
}
28 changes: 26 additions & 2 deletions src/bin/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,38 @@ var argv = require('yargs')
.alias(options)
.argv

var size = require('window-size')
var availableWidth = size.width || /* istanbul ignore next */ 80
var ui = require('cliui')({width: availableWidth})

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

var ruleFinder = getRuleFinder(specifiedFile)
Object.keys(options).forEach(function findRules(option) {
var rules
var rules, outputRules, outputRuleCellMapper
var outputPadding = ' '
var outputMaxWidth = 0
var outputMaxCols = 0
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
rules = rules.map(function columnSpecification(rule) {
rule = rule + outputPadding
outputMaxWidth = Math.max(rule.length, outputMaxWidth)
return rule
})
outputMaxCols = Math.floor(availableWidth / outputMaxWidth)
outputRuleCellMapper = getOutputRuleCellMapper(Math.floor(availableWidth / outputMaxCols))
while (rules.length) {
outputRules = rules.splice(0, outputMaxCols).map(outputRuleCellMapper)
ui.div.apply(ui, outputRules)
}
console.log(ui.toString()) // eslint-disable-line no-console
} else if (option === 'getUnusedRules') {
processExitCode = 0
}
Expand All @@ -38,3 +56,9 @@ Object.keys(options).forEach(function findRules(option) {
if (processExitCode) {
process.exit(processExitCode)
}

function getOutputRuleCellMapper(width) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be refactoring the function (duplicate, exists in src/bin/diff.js and src/bin/find.js) out of these two files, when tackling #7

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible it will be great to see it along with this PR. Other wise just create an issue for cleaning up dups.

return function curriedOutputRuleCellMapper(rule) {
return {text: rule, width: width}
}
}
9 changes: 5 additions & 4 deletions src/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var fs = require('fs')
var eslint = require('eslint')
var isAbsolute = require('path-is-absolute')
var difference = require('./array-diff')
var sortRules = require('./sort-rules')

function _getConfigFile(specifiedFile) {
if (specifiedFile) {
Expand Down Expand Up @@ -71,21 +72,21 @@ function RuleFinder(specifiedFile) {

// get all the current rules instead of referring the extended files or documentation
this.getCurrentRules = function getCurrentRules() {
return currentRules
return sortRules(currentRules)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have named sortRules to getSortedRules as its is not sorting in-place but returning the sorted list.

}

// get all the plugin rules instead of referring the extended files or documentation
this.getPluginRules = function getPluginRules() {
return pluginRules
return sortRules(pluginRules)
}

// get all the available rules instead of referring eslint and plugin packages or documentation
this.getAllAvailableRules = function getAllAvailableRules() {
return allRules
return sortRules(allRules)
}

this.getUnusedRules = function getUnusedRules() {
return unusedRules
return sortRules(unusedRules)
}

}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/sort-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

function sortRules(rules) {
return rules.sort(function sort(a, b) {
return a > b ? 1 : -1
})
}

module.exports = sortRules
18 changes: 9 additions & 9 deletions test/lib/rule-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,52 +63,52 @@ describe('rule-finder', function() {
return noSpecifiedFile
}
ruleFinder = getRuleFinder()
assert.deepEqual(ruleFinder.getAllAvailableRules(), ['foo-rule', 'bar-rule', 'baz-rule'])
assert.deepEqual(ruleFinder.getAllAvailableRules(), ['bar-rule', 'baz-rule', 'foo-rule'])
})

it('specifiedFile (relative path) is passed to the constructor', function() {
var ruleFinder = getRuleFinder(specifiedFileRelative)
assert.deepEqual(ruleFinder.getUnusedRules(), ['baz-rule', 'react/foo-rule', 'react/bar-rule', 'react/baz-rule'])
assert.deepEqual(ruleFinder.getUnusedRules(), ['baz-rule', 'react/bar-rule', 'react/baz-rule', 'react/foo-rule'])
})

it('specifiedFile (relative path) - curent rules', function() {
var ruleFinder = getRuleFinder(specifiedFileRelative)
assert.deepEqual(ruleFinder.getCurrentRules(), ['foo-rule', 'bar-rule'])
assert.deepEqual(ruleFinder.getCurrentRules(), ['bar-rule', 'foo-rule'])
})

it('specifiedFile (relative path) - plugin rules', function() {
var ruleFinder = getRuleFinder(specifiedFileRelative)
assert.deepEqual(ruleFinder.getPluginRules(), ['react/foo-rule', 'react/bar-rule', 'react/baz-rule'])
assert.deepEqual(ruleFinder.getPluginRules(), ['react/bar-rule', 'react/baz-rule', 'react/foo-rule'])
})

it('specifiedFile (relative path) - all available rules', function() {
var ruleFinder = getRuleFinder(specifiedFileRelative)
assert.deepEqual(
ruleFinder.getAllAvailableRules(),
['foo-rule', 'bar-rule', 'baz-rule', 'react/foo-rule', 'react/bar-rule', 'react/baz-rule']
['bar-rule', 'baz-rule', 'foo-rule', 'react/bar-rule', 'react/baz-rule', 'react/foo-rule']
)
})

it('specifiedFile (absolut path) is passed to the constructor', function() {
var ruleFinder = getRuleFinder(specifiedFileAbsolute)
assert.deepEqual(ruleFinder.getUnusedRules(), ['baz-rule', 'react/foo-rule', 'react/bar-rule', 'react/baz-rule'])
assert.deepEqual(ruleFinder.getUnusedRules(), ['baz-rule', 'react/bar-rule', 'react/baz-rule', 'react/foo-rule'])
})

it('specifiedFile (absolut path) - curent rules', function() {
var ruleFinder = getRuleFinder(specifiedFileAbsolute)
assert.deepEqual(ruleFinder.getCurrentRules(), ['foo-rule', 'bar-rule'])
assert.deepEqual(ruleFinder.getCurrentRules(), ['bar-rule', 'foo-rule'])
})

it('specifiedFile (absolut path) - plugin rules', function() {
var ruleFinder = getRuleFinder(specifiedFileAbsolute)
assert.deepEqual(ruleFinder.getPluginRules(), ['react/foo-rule', 'react/bar-rule', 'react/baz-rule'])
assert.deepEqual(ruleFinder.getPluginRules(), ['react/bar-rule', 'react/baz-rule', 'react/foo-rule'])
})

it('specifiedFile (absolut path) - all available rules', function() {
var ruleFinder = getRuleFinder(specifiedFileAbsolute)
assert.deepEqual(
ruleFinder.getAllAvailableRules(),
['foo-rule', 'bar-rule', 'baz-rule', 'react/foo-rule', 'react/bar-rule', 'react/baz-rule']
['bar-rule', 'baz-rule', 'foo-rule', 'react/bar-rule', 'react/baz-rule', 'react/foo-rule']
)
})
})
19 changes: 19 additions & 0 deletions test/lib/sort-rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var assert = require('assert')
var sortRules = require('../../src/lib/sort-rules')

describe('sort-rules', function() {
it('should return sorted rules', function() {
assert.deepEqual(
sortRules(['a', 'b', 'c']),
['a', 'b', 'c']
)
assert.deepEqual(
sortRules(['c', 'b', 'a']),
['a', 'b', 'c']
)
assert.deepEqual(
sortRules(['aa', 'a', 'ab', 'b', 'c']),
['a', 'aa', 'ab', 'b', 'c']
)
})
})