-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.js
119 lines (106 loc) · 3.37 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env node
'use strict'
const meow = require('meow')
const fn = require('./src/index.js')
const Promise = require('bluebird')
const gitconfig = require('gitconfiglocal')
const pify = require('pify')
const ghauth = pify(require('ghauth'))
const lib = require('./src/lib.js')
const cli = meow([`
Usage
$ watch-gh-repos <input> [opts]
Options
-g, --get Get repo watching details
-e, --enterprise A different GitHub enterprise endpoint
-i, --ignore Ignore notifications from a repository
-o, --org Specify all repositories from an organization or user
-r, --ratelimit Skip checks making sure GitHub repo is valid (Skips 1 hit per repo)
-t, --token A token
-u, --unwatch Unwatch instead of watch
-w, --watch Specify a repo (default)
Examples
~/src/RichardLitt/unwatch-gh-repos $ watch-gh-repos
Watched: RichardLitt/unwatch-gh-repos
$ watch-gh-repos RichardLitt/watch-gh-repos
Watched: RichardLitt/watch-gh-repos
$ watch-gh-repos RichardLittCorp/watch-gh-repos -e https://github.net/api/v3 -t <token>
Watched: RichardLittCorp/watch-gh-repos
$ watch-gh-repos --unwatch RichardLitt/watch-gh-repos
Unwatched: RichardLitt/watch-gh-repos
$ watch-gh-repos --org --watch RichardLitt
Watched: RichardLitt/first-repo
...
`], {
string: ['get', 'ignore', 'watch', 'token', 'unwatch'],
boolean: ['org', 'ratelimit'],
alias: {
g: 'get',
e: 'enterprise',
i: 'ignore',
o: 'org',
t: 'token',
u: 'unwatch',
w: 'watch',
r: 'ratelimit'
}
})
const authOptions = {
clientId: 'bfec45dffc45ea593ead',
configName: (cli.flags.e) ? 'watch-ghe-repos' : 'watch-gh-repos',
note: 'Watch, unwatch, or ignore GitHub repositories',
userAgent: (cli.flags.e) ? 'watch-ghe-repos' : 'watch-gh-repos',
scopes: ['repo', 'user', 'notifications'],
authURL: (cli.flags.e) ? cli.flags.e + '/authorizations' : null,
promptName: (cli.flags.e) ? 'GitHub Enterprise' : null
}
function getResponse (opts) {
return Promise.resolve(fn(opts))
.then((res) => {
lib.logStringOrArray(res)
})
}
function getRepoFromConfig () {
return pify(gitconfig)(process.cwd())
.then(config => {
if (config && config.remote && config.remote.origin && config.remote.origin.url) {
var url = config.remote.origin.url
return url.match(/([^/:]+\/[^/.]+)(\.git)?$/)[1]
} else {
console.log('Unable to get remote.')
}
})
}
Promise.resolve().then(() => {
if (!cli.flags.token && !process.env.WATCH_GH_REPOS) {
return Promise.resolve(ghauth(authOptions))
.then((user) => user.token)
.catch((err) => {
if (err) {
throw new Error('Unable to validate ghauth')
}
})
}
}).then((token) => {
cli.flags.token = token || cli.flags.token // Env set in index
if (cli.input.length === 0 && lib.noOpts(cli.flags)) {
return getRepoFromConfig()
.then((res) => {
cli.flags['watch'] = cli.flags['w'] = res
return cli.flags
}).catch((err) => {
if (err) {}
throw new Error('Error: Unable to find organization! Please specify some options.')
})
} else {
if (cli.input.length === 1 && lib.noOpts(cli.flags)) {
cli.flags['watch'] = cli.flags['w'] = cli.input[0]
}
return cli.flags
}
}).then(() => {
getResponse(cli.flags)
}).catch((err) => {
console.log(err.message)
cli.showHelp(1)
})