Skip to content

Commit

Permalink
feat(cli): now takes no args
Browse files Browse the repository at this point in the history
If no args, tries to read the current git repo for the origin URL and
uses that.
  • Loading branch information
tgetgood committed Oct 25, 2017
1 parent 77ac96b commit 6b239e7
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 20 deletions.
70 changes: 52 additions & 18 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
()#!/usr/bin/env node
#!/usr/bin/env node
'use strict'

const meow = require('meow')
Expand Down Expand Up @@ -32,6 +32,7 @@ const cli = meow([`
c: 'csv',
r: 'repo',
t: 'token',
o: 'org',
u: 'user'
}
})
Expand All @@ -43,35 +44,68 @@ const before = cli.flags.b ? new Date(cli.flags.b) : new Date()

const debugMode = cli.flags.debug

if (cli.flags.o && token) {
if (!token) {
console.error('A token is needed to access the GitHub API. Please provide one with -t or the GITHUB_TOKEN environment variable.')
process.exit(1)
}

const formatReturn = x => {
if (cli.flags.csv) {
return main.toCSV(x)
} else {
return JSON.stringify(x, null, 2)
}
}

const handleOut = console.log

const handleError = e => {
console.error(e.stack)
process.exit(1)
}

if (cli.flags.o) {
main.orgContributors({
debug: debugMode,
token: token,
orgName: cli.flags.o,
before: before,
after: after
}).then(json => JSON.stringify(json, null, 2))
.then(console.log)
.catch(e => console.error(e.message))
} else if (cli.flags.u && cli.flags.r && token) {
}).then(formatReturn)
.then(handleOut)
.catch(handleError)
} else if (cli.flags.u && cli.flags.r) {
main.repoContributors({
debug: debugMode,
token: token,
user: cli.flags.u,
repo: cli.flags.r,
before: before,
after: after
}).then(x => {
if (cli.flags.csv) {
return main.toCSV(x)
} else {
return JSON.stringify(x, null, 2)
}
}).then(console.log)
.catch(e => {
console.error(e.stack)
})
}).then(formatReturn)
.then(handleOut)
.catch(handleError)
} else {
console.error('You must currently specify both a user and a repo name. And provide a token.')
process.exit(1)
(async () => {
const creds = await main.getCurrentRepoInfo()

return main.repoContributors({
token,
debug: debugMode,
user: creds.user,
repo: creds.repo,
before,
after
}).then(x => {
if (!x.user || !x.repo) {
return x
} else {
console.error('Not in a git repository')
console.error(cli.help)
process.exit(1)
}
}).then(formatReturn)
.then(handleOut)
.catch(handleError)
})()
}
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ const shellOut = command =>
}
}))

const gitConfig = 'git config --get remote.origin.url'
const gitConfigCommand = 'git config --get remote.origin.url'

const parseGitURL = new RegExp('.*github\\.com[:/]([^/]+)\\/(.+)\\n?$')

const current = shellOut(gitConfig).then(x => parseGitURL.exec(x))
const getCurrentRepoInfo = () => shellOut(gitConfigCommand)
.then(x => parseGitURL.exec(x))
.then(x => { return {user: x[1], repo: x[2]} })

//
// CSV Output
Expand Down Expand Up @@ -78,6 +80,7 @@ const orgContributors = ({token, orgName, before, after, debug}) =>

module.exports = {
toCSV,
getCurrentRepoInfo,
repoContributors,
orgContributors,
userRepoNames
Expand Down

0 comments on commit 6b239e7

Please sign in to comment.