Skip to content

Commit

Permalink
cli: add commander and program commands
Browse files Browse the repository at this point in the history
  • Loading branch information
grvcoelho committed Apr 12, 2018
1 parent d110c10 commit 1076678
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
13 changes: 9 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"dependencies": {
"bluebird": "^3.5.1",
"commander": "^2.15.1",
"execa": "^0.10.0",
"ramda": "^0.25.0"
},
Expand Down
29 changes: 18 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import Promise from 'bluebird'
import exec from 'execa'
import program from 'commander'
import path from 'path'
import { CLIEngine } from 'eslint'
import {
assoc,
curry,
endsWith,
evolve,
filter,
Expand All @@ -20,25 +22,26 @@ import { getChangedLinesFromDiff } from './lib/git'

const linter = new CLIEngine()
const formatter = linter.getFormatter()
const COMMIT_RANGE = 'HEAD^..HEAD'

const getDiff = filename => exec('git', ['diff', COMMIT_RANGE, filename])
.then(prop('stdout'))

const getChangedFiles = pipeP(
() => exec('git', ['diff', COMMIT_RANGE, '--name-only']),
commitRange => exec('git', ['diff', commitRange, '--name-only']),
prop('stdout'),
split('\n'),
filter(endsWith('.js')),
map(path.resolve)
)

const getChangedFileLineMap = filePath => pipeP(
getDiff,
const getDiff = curry((commitRange, filename) =>
exec('git', ['diff', commitRange, filename])
.then(prop('stdout')))

const getChangedFileLineMap = curry((commitRange, filePath) => pipeP(
getDiff(commitRange),
getChangedLinesFromDiff,
objOf('changedLines'),
assoc('filePath', filePath)
)(filePath)
)(filePath))


const lintChangedLines = pipe(
map(prop('filePath')),
Expand Down Expand Up @@ -74,11 +77,15 @@ const reportResults = pipe(
formatter
)

const run = () => Promise.resolve()
const run = commitRange => Promise.resolve(commitRange)
.then(getChangedFiles)
.map(getChangedFileLineMap)
.map(getChangedFileLineMap(commitRange))
.then(applyLinter)
.then(reportResults)
.tap(console.log)

run()
program
.command('lint <commit_range>')
.action(run)

program.parse(process.argv)

0 comments on commit 1076678

Please sign in to comment.