|
1 | 1 | #!/usr/bin/env node
|
2 |
| -import meow from 'meow'; |
3 |
| -import { calculate, download, history, success, clean, cache } from './index'; |
| 2 | +import meow from "meow"; |
| 3 | +import main from "./index"; |
| 4 | +import Text from "./lib/text"; |
4 | 5 |
|
5 |
| -interface Flags { |
6 |
| - auth?: string; |
7 |
| - branch?: string; |
8 |
| - concurrency: number; |
9 |
| - json?: boolean; |
10 |
| - last?: number; |
11 |
| - period?: number; |
12 |
| - result?: string; |
13 |
| - since?: number; |
14 |
| - threshold?: number; |
15 |
| -} |
16 |
| - |
17 |
| -async function main(argv: string[]) { |
| 6 | +async function cli(argv: string[]) { |
18 | 7 | const cli = meow({
|
19 | 8 | argv,
|
20 |
| - help: ` |
21 |
| - Usage |
22 |
| - $ build-stats <service>:<user>/<repo> <command> [...opts] |
23 |
| -
|
24 |
| - Commands |
25 |
| - download Download history for a repository |
26 |
| - calculate Calculate average build time and success rates over time |
27 |
| - history List individual builds |
28 |
| - success Get quick stats of number of success and failed builds |
29 |
| - clean Delete the downloaded history of repository |
30 |
| - cache Outputs the directory where data will be cached |
31 |
| -
|
32 |
| - Options |
33 |
| - --auth [authentication] (download) Authentication to access private repo |
34 |
| - --concurrency [number] (download) How many parallel downloads should be used when downloading data (Default: 10) |
35 |
| - --since [buildNumber] (download) Overrides the normal logic of which builds to download data for. |
36 |
| - This should only be required in debugging/fixing errors (Default: last downloaded build) |
37 |
| - --branch [name] (calculate/history) Which branch(es) to display (Comma-separated list) (Default: *) |
38 |
| - --result [name] (calculate/history) Which branch(es) to display (Comma-separated list) (Default: *) |
39 |
| - --period [days] (calculate) How many days in a time period to calculate the means for (Default: 1) |
40 |
| - --last [count] (calculate) How many periods to calculate back to (Default: 30) |
41 |
| - --threshold [time] (calculate) Time under which builds graph is shown in green color. Default is mean of all the builds in that period |
42 |
| -
|
43 |
| - Services |
44 |
| - - bitbucket Bitbucket Pipelines |
45 |
| - - travis Travis CI |
46 |
| -
|
47 |
| - Examples |
48 |
| - Download travis builds history to .data folder: |
49 |
| - $ build-stats travis:boltpkg/bolt download |
50 |
| -
|
51 |
| - Download travis builds history to .data folder for private repository: |
52 |
| - $ build-stats travis:boltpkg/bolt download --auth <token> |
53 |
| -
|
54 |
| - Download a subset of builds very quickly: |
55 |
| - $ build-stats travis:boltpkg/bolt download --concurrency=20 --since=300 |
56 |
| -
|
57 |
| - Calculate monthly average build time and success rate of a repo over the last year |
58 |
| - $ build-stats travis:boltpkg/bolt calculate |
59 |
| -
|
60 |
| - Calculate daily average build time and success rate of a repo over the last month |
61 |
| - $ build-stats travis:boltpkg/bolt calculate --period 1 --last 30 |
62 |
| -
|
63 |
| - Calculate daily average build time and success rate of the master branch of a repo over the last 90 days |
64 |
| - $ build-stats travis:boltpkg/bolt calculate --branch master --period 1 --last 90 |
65 |
| -
|
66 |
| - Display build history |
67 |
| - $ build-stats travis:boltpkg/bolt history |
68 |
| -
|
69 |
| - Display build history for master branch for builds that were either successful or failed |
70 |
| - $ build-stats travis:boltpkg/bolt history --branch master --result SUCCESSFUL,FAILED |
71 |
| -
|
72 |
| - Display the number of success and failed builds |
73 |
| - $ build-stats travis:boltpkg/bolt success |
74 |
| -
|
75 |
| - Delete the downloaded history of repository |
76 |
| - $ build-stats travis:boltpkg/bolt clean |
77 |
| -
|
78 |
| - Output the cache directory of a repository |
79 |
| - $ build-stats travis:boltpkg/bolt cache |
80 |
| - `, |
| 9 | + help: Text.mainText.help, |
81 | 10 | flags: {
|
82 | 11 | auth: {
|
83 |
| - type: 'string' |
| 12 | + type: "string", |
84 | 13 | },
|
85 | 14 | branch: {
|
86 |
| - type: 'string' |
| 15 | + type: "string", |
87 | 16 | },
|
88 | 17 | concurrency: {
|
89 |
| - type: 'number', |
90 |
| - default: 10 |
| 18 | + type: "number", |
| 19 | + default: 10, |
91 | 20 | },
|
92 | 21 | json: {
|
93 |
| - type: 'boolean' |
| 22 | + type: "boolean", |
94 | 23 | },
|
95 | 24 | last: {
|
96 |
| - type: 'number' |
| 25 | + type: "number", |
97 | 26 | },
|
98 | 27 | period: {
|
99 |
| - type: 'number' |
| 28 | + type: "number", |
100 | 29 | },
|
101 | 30 | since: {
|
102 |
| - type: 'number' |
103 |
| - } |
104 |
| - } |
| 31 | + type: "number", |
| 32 | + }, |
| 33 | + }, |
105 | 34 | });
|
106 | 35 |
|
107 | 36 | if (cli.input.length < 2) {
|
108 | 37 | cli.showHelp();
|
109 | 38 | }
|
110 | 39 |
|
111 |
| - let match = cli.input[0].match(/(.*):(.*)\/(.*)/); |
| 40 | + const repoSlug = cli.input[0].match(/(.*):(.*)\/(.*)/); |
112 | 41 |
|
113 |
| - if (!match) { |
| 42 | + if (!repoSlug) { |
114 | 43 | throw new Error(
|
115 | 44 | `Invalid repo "${cli.input[0]}", should be "host:user/repo"`
|
116 | 45 | );
|
117 | 46 | }
|
118 | 47 |
|
119 |
| - let [, host, user, repo]: string[] = match; |
120 |
| - let command: string = cli.input[1]; |
121 |
| - let flags: Flags = cli.flags; |
122 |
| - let cwd: string = __dirname; |
123 |
| - |
124 |
| - if (command === 'download') { |
125 |
| - await download({ |
126 |
| - cwd, |
127 |
| - host, |
128 |
| - user, |
129 |
| - repo, |
130 |
| - auth: flags.auth, |
131 |
| - concurrency: flags.concurrency, |
132 |
| - since: flags.since |
133 |
| - }); |
134 |
| - } else if (command === 'calculate') { |
135 |
| - await calculate({ |
136 |
| - cwd, |
137 |
| - host, |
138 |
| - user, |
139 |
| - repo, |
140 |
| - branch: flags.branch, |
141 |
| - result: flags.result, |
142 |
| - period: flags.period, |
143 |
| - last: flags.last, |
144 |
| - threshold: flags.threshold, |
145 |
| - json: flags.json |
146 |
| - }); |
147 |
| - } else if (command === 'history') { |
148 |
| - await history({ |
149 |
| - cwd, |
150 |
| - host, |
151 |
| - user, |
152 |
| - repo, |
153 |
| - branch: flags.branch, |
154 |
| - result: flags.result, |
155 |
| - threshold: flags.threshold, |
156 |
| - json: flags.json |
157 |
| - }); |
158 |
| - } else if (command === 'success') { |
159 |
| - await success({ |
160 |
| - cwd, |
161 |
| - host, |
162 |
| - user, |
163 |
| - repo, |
164 |
| - branch: flags.branch, |
165 |
| - result: flags.result, |
166 |
| - period: flags.period, |
167 |
| - last: flags.last, |
168 |
| - json: flags.json |
169 |
| - }); |
170 |
| - } else if (command === 'clean') { |
171 |
| - await clean({ |
172 |
| - cwd, |
173 |
| - host, |
174 |
| - user, |
175 |
| - repo |
176 |
| - }); |
177 |
| - } else if (command === 'cache') { |
178 |
| - await cache({ cwd, host, user, repo }); |
179 |
| - } else { |
180 |
| - throw new Error( |
181 |
| - `Unknown command "${command}", should be "download", "calculate", "history", "success", "clean", "cache"` |
182 |
| - ); |
183 |
| - } |
| 48 | + await main({ |
| 49 | + cwd: __dirname, |
| 50 | + repoSlug, |
| 51 | + command: cli.input[1], |
| 52 | + flags: cli.flags, |
| 53 | + }); |
184 | 54 | }
|
185 | 55 |
|
186 |
| -main(process.argv.slice(2)).catch(err => { |
| 56 | +cli(process.argv.slice(2)).catch((err) => { |
187 | 57 | console.error(err);
|
188 | 58 | process.exit(1);
|
189 | 59 | });
|
0 commit comments