Skip to content

Commit 112b684

Browse files
committed
chore: update flag upload script
1 parent a4987d4 commit 112b684

File tree

1 file changed

+67
-13
lines changed

1 file changed

+67
-13
lines changed

scripts/codecov-upload-flags.mjs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
1-
import path from 'path';
2-
import { readFileSync } from 'fs';
1+
import { execSync } from 'child_process';
32
import { globSync } from 'glob';
3+
import { chmod, chmodSync, createWriteStream, existsSync, readFileSync } from 'fs';
4+
import path from 'path';
5+
import os from 'os';
6+
import { Readable } from 'stream';
47

58
const readPkg = (dir) => JSON.parse(readFileSync(path.join(dir, 'package.json'), 'utf8'));
9+
const download = async (url, dst) => {
10+
const resp = await fetch(url);
11+
return new Promise((res, rej) => {
12+
if (resp.ok && resp.body) {
13+
console.log("Writing to file:", dst);
14+
let writer = createWriteStream(dst);
15+
Readable.fromWeb(resp.body).pipe(writer);
16+
writer.on('finish', res);
17+
writer.on('error', rej);
18+
} else {
19+
rej(new Error('Could not get body from request'));
20+
}
21+
});
22+
};
623

724
const TOP = process.cwd();
25+
// const TEMP = os.tmpdir();
26+
827
const pkgInfo = readPkg(TOP);
928
const pkgFiles = pkgInfo.workspaces.map((exp) => globSync(path.join(exp, 'package.json')));
10-
const pkgFlags = pkgFiles.flat().map((f) => {
29+
const codecovPath = path.resolve(TOP, 'codecov');
30+
const pkgsWithFlag = pkgFiles.flat().map((f) => {
1131
const path = f.replace('package.json', '');
1232
const info = readPkg(path);
1333
const name = info.name;
1434
const flag = name.replace('@opentelemetry/', '');
15-
16-
return { name, flag, len: flag.length, path };
35+
const report = path + 'coverage/coverage-final.json';
36+
const command = `${codecovPath} do-upload -t <token> -f ${report} --disable-search -F ${flag} -d`;
37+
return { name, flag, len: flag.length, path, report, command };
1738
});
1839

19-
// Print the flags
20-
pkgFlags.forEach((pf) => {
21-
console.log(`
22-
${pf.flag}:
23-
paths:
24-
- ${pf.path}
25-
carryforward: true`)
26-
});
40+
// Download codecov
41+
const urlMap = {
42+
linux: 'https://cli.codecov.io/latest/linux/codecov',
43+
darwin: 'https://cli.codecov.io/latest/macos/codecov',
44+
};
45+
const url = urlMap[process.platform];
46+
const token = process.argv[2];
47+
// Validations
48+
if (typeof token !== 'string') {
49+
console.log('Token is missing. Usage:');
50+
console.log('node ./scripts/codecov-upload-flags.mjs my-codecov-token');
51+
process.exit(-1);
52+
}
53+
if (!url) {
54+
console.log(`No codecov binary available for platform "${process.platform}"`);
55+
console.log(`Available platforms are "${Object.keys(urlMap)}"`);
56+
process.exit(-1);
57+
}
58+
59+
// Download CLI tool if needed
60+
if (existsSync(codecovPath)) {
61+
console.log(`Codecov binary found.`);
62+
} else {
63+
console.log(`Codecov binary missing. Downloading from ${url}`);
64+
await download(url, codecovPath);
65+
console.log(`Codecov binary downloaded to ${codecovPath}`);
66+
}
67+
// make sure we have exec perms
68+
chmodSync(codecovPath, 0o555);
69+
70+
// Compute the commands to run
71+
for (const pkg of pkgsWithFlag) {
72+
if (existsSync(pkg.report)) {
73+
const command = pkg.command.replace('<token>', token)
74+
console.log(`Uploading report of ${pkg.name} with flag ${pkg.flag}`);
75+
console.log(`Command: ${command}`);
76+
execSync(command, {cwd: TOP, encoding: 'utf-8'});
77+
} else {
78+
console.log(`Report of ${pkg.name} not found. Expected existence of ${pkg.report}`);
79+
}
80+
}

0 commit comments

Comments
 (0)