-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.ts
158 lines (148 loc) · 4.46 KB
/
bin.ts
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env ts-node
import compareOne from './src/commands/compare-one'
import init from './src/commands/init'
import compareAll from './src/commands/compare-all'
import appName from './src/app-name'
import { processGithubUrl } from './src/utils/github'
import yargs from 'yargs'
import invariant from 'tiny-invariant'
import chalk from 'chalk'
import { endLog, logComparisonResults } from './src/utils/chalkies'
import { BranchComparison, createCompareLinks } from './src/utils/compare'
import compareOneAllRepos from './src/commands/compare-one-all-repos'
yargs(process.argv.slice(2))
.usage(`Usage: ${appName ?? '$0'} <url> [branch]`)
.command(
`$0 <url> [branch] [options]`,
'compare branches',
(yargs) => {
yargs
.positional('url', {
type: 'string',
describe: `url of github repo
must be in the format: https://github.com/<org>/<repo>/tree/<branch-name>
or https://github.com/<org>/<repo>
where <branch-name> is provided as a second argument\n`,
})
.positional('branch', {
type: 'string',
describe: `branch to compare against
only required if branch name not in url`,
demandOption: true,
})
.option('verbose', {
alias: 'v',
type: 'boolean',
default: false,
description: 'show more output',
})
.option('all', {
alias: 'A',
type: 'boolean',
default: false,
description: 'compare all branches of the given repo',
})
},
async (argv) => {
const url = argv.url as string
let { org, repo, branch } = processGithubUrl(url)
invariant(org, 'No org found in url 🤷')
invariant(repo, 'No repo found in url 🤷')
let comparisons: BranchComparison[] = []
const shouldCompareAll = argv.all as boolean
if (shouldCompareAll) {
if (branch || argv.branch) {
console.warn(
chalk.yellow`🦜: Ignoring branch paramater when using --all | -A`
)
}
comparisons = await compareAll({
owner: org,
repo,
flags: {
verbose: argv.verbose as boolean,
},
})
} else {
if (!branch) {
branch = argv.branch as string | undefined
}
invariant(
branch,
chalk.red`🦜: I couldn't find a branch, if you would like to compare all branches, use the --all | -A flag`
)
comparisons = await compareOne({
owner: org,
repo,
branch,
flags: {
verbose: argv.verbose as boolean,
},
})
}
logComparisonResults(comparisons)
endLog(() => {
const maxComparison = comparisons.at(-1)
if (!maxComparison) return ''
const links = createCompareLinks({
org,
repo,
comparison: maxComparison,
})
return `Here's a link to the comparisons for the most similar branches:\n${links[0]}\n${links[1]}`
})
}
)
.command(
'init [token]',
`initialize a new ${appName} config file`,
(yargs) => {
yargs
.option('token', {
alias: 't',
type: 'string',
describe:
'your github access token, this should have admin:org scope',
})
.positional('token', {
type: 'string',
describe:
'your github access token, this should have admin:org scope',
})
},
(argv) => {
init({ token: argv.token as string | undefined })
}
)
.command(
'student <cohort> <name>',
`compare student's commits across all repositories in a cohort`,
(yargs) => {
yargs
.positional('cohort', {
type: 'string',
describe: 'cohort name',
})
.positional('name', {
type: 'string',
describe: 'student branch name (this can be a partial match)',
})
},
async (argv) => {
const cohort = argv.cohort as string
const studentBranch = argv.name as string
const comparisonsMap = await compareOneAllRepos({
owner: cohort,
branch: studentBranch,
flags: {
verbose: argv.verbose as boolean,
},
})
for (const [repo, comparisons] of comparisonsMap) {
console.log(chalk.bold`🦜: Comparisons for {green ${repo}}:`)
logComparisonResults(comparisons)
}
}
)
.help()
.parseAsync()