Skip to content

Commit 7e8957b

Browse files
committed
ncu-ci: initial implementation
1 parent 7918645 commit 7e8957b

18 files changed

+33339
-6
lines changed

bin/ncu-ci

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const {
6+
PRBuild, BenchmarkRun, CommitBuild, jobCache, parseJobFromURL, constants
7+
} = require('../lib/ci');
8+
9+
const {
10+
PR, COMMIT, BENCHMARK
11+
} = constants;
12+
13+
const { runPromise } = require('../lib/run');
14+
const Request = require('../lib/request');
15+
const CLI = require('../lib/cli');
16+
const yargs = require('yargs');
17+
18+
// This is used for testing
19+
// Default cache dir is ${ncu-source-dir}/.ncu/cache
20+
jobCache.enable();
21+
22+
// eslint-disable-next-line no-unused-vars
23+
const argv = yargs
24+
.command({
25+
command: 'url <url>',
26+
desc: 'Automatically detect CI type and show results',
27+
builder: (yargs) => {
28+
yargs
29+
.positional('url', {
30+
describe: 'URL of the PR or the CI',
31+
type: 'string'
32+
});
33+
},
34+
handler
35+
})
36+
.command({
37+
command: 'pr <jobid>',
38+
desc: 'Show results of a node-test-pull-request CI job',
39+
builder: (yargs) => {
40+
yargs
41+
.positional('jobid', {
42+
describe: 'id of the job',
43+
type: 'number'
44+
});
45+
},
46+
handler
47+
})
48+
.command({
49+
command: 'commit <jobid>',
50+
desc: 'Show results of a node-test-commit CI job',
51+
builder: (yargs) => {
52+
yargs
53+
.positional('jobid', {
54+
describe: 'id of the job',
55+
type: 'number'
56+
});
57+
},
58+
handler
59+
})
60+
.command({
61+
command: 'benchmark <jobid>',
62+
desc: 'Show results of a benchmark-node-micro-benchmarks CI job',
63+
builder: (yargs) => {
64+
yargs
65+
.positional('jobid', {
66+
describe: 'id of the job',
67+
type: 'number'
68+
});
69+
},
70+
handler
71+
})
72+
.demandCommand(1, 'must provide a valid command')
73+
.option('markdown', {
74+
alias: 'm',
75+
type: 'string',
76+
describe: 'file to write results in markdown'
77+
})
78+
.help()
79+
.argv;
80+
81+
async function getResults(cli, request, job) {
82+
let build;
83+
const { type, jobid, markdown } = job;
84+
if (type === PR) {
85+
build = new PRBuild(cli, request, jobid);
86+
await build.getResults();
87+
} else if (type === COMMIT) {
88+
build = new CommitBuild(cli, request, jobid);
89+
await build.getResults();
90+
} else if (type === BENCHMARK) {
91+
build = new BenchmarkRun(cli, request, jobid);
92+
await build.getResults();
93+
} else {
94+
yargs.showHelp();
95+
return;
96+
}
97+
98+
build.display();
99+
100+
if (markdown) {
101+
build.appendToMarkdown(markdown);
102+
}
103+
return build;
104+
}
105+
106+
async function main(command, argv) {
107+
const cli = new CLI();
108+
const request = new Request({});
109+
const queue = [];
110+
111+
const commandToType = {
112+
'commit': COMMIT,
113+
'pr': PR,
114+
'benchmark': BENCHMARK
115+
};
116+
117+
if (command === 'url') {
118+
let parsed = parseJobFromURL(argv.url);
119+
if (parsed) {
120+
queue.push({
121+
type: parsed.type,
122+
jobid: parsed.jobid,
123+
markdown: argv.markdown
124+
});
125+
} else {
126+
// TODO: parse CI links from PR thread
127+
return yargs.showHelp();
128+
}
129+
} else {
130+
queue.push({
131+
type: commandToType[command],
132+
jobid: argv.jobid,
133+
markdown: argv.markdown
134+
});
135+
}
136+
137+
for (let job of queue) {
138+
await getResults(cli, request, job);
139+
}
140+
}
141+
142+
function handler(argv) {
143+
const [ command ] = argv._;
144+
runPromise(main(command, argv));
145+
}

lib/cache.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ function isAsync(fn) {
1010

1111
class Cache {
1212
constructor(dir) {
13-
this.dir = dir || path.join(__dirname, '..', '.ncu', 'cache');
13+
this.dir = dir || this.computeCacheDir(path.join(__dirname, '..'));
1414
this.originals = {};
1515
this.disabled = true;
1616
}
1717

18+
computeCacheDir(base) {
19+
return path.join(base, '.ncu', 'cache');
20+
}
21+
1822
disable() {
1923
this.disabled = true;
2024
}

0 commit comments

Comments
 (0)