Skip to content

Commit de074e7

Browse files
OttoAllmendingeraider
andcommitted
refactor(scripts): reuse code in verify-release
Co-authored-by: aider <[email protected]> Issue: BTC-1947
1 parent ebe6d59 commit de074e7

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

scripts/verify-release.ts

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,41 @@
11
import * as execa from 'execa';
22
import { readFileSync } from 'fs';
33
import * as path from 'path';
4-
import { get as httpGet } from 'https';
4+
import { getLernaModules, getDistTags } from './prepareRelease';
55

66
let lernaModuleLocations: string[] = [];
77

8-
/**
9-
* Create a function which can run lerna commands
10-
* @param {String} lernaPath - path to lerna binary
11-
* @returns {function(string, string[], Object.<string, string>): Promise<string>}
12-
*/
13-
function getLernaRunner(lernaPath: string) {
14-
return async (command: string, args: string[] = [], options = {}) => {
15-
const { stdout } = await execa(lernaPath, [command, ...args], options);
16-
return stdout;
17-
};
18-
}
19-
20-
const getLernaModules = async (): Promise<void> => {
21-
const { stdout: lernaBinary } = await execa('yarn', ['bin', 'lerna'], { cwd: process.cwd() });
22-
23-
const lerna = getLernaRunner(lernaBinary);
24-
const modules: Array<{ name: string; location: string }> = JSON.parse(
25-
await lerna('list', ['--loglevel', 'silent', '--json', '--all', '--toposort'])
26-
);
27-
8+
const getLernaModuleLocations = async (): Promise<void> => {
9+
const modules = await getLernaModules();
2810
lernaModuleLocations = modules.map(({ location }) => location);
2911
};
3012

31-
/**
32-
* Makes an HTTP request to fetch all the dist tags for a given package.
33-
*/
34-
const getDistTags = async (packageName: string): Promise<Record<string, string>> => {
35-
return new Promise((resolve) => {
36-
httpGet(`https://registry.npmjs.org/-/package/${packageName}/dist-tags`, (res) => {
37-
let data = '';
38-
res.on('data', (d) => {
39-
data += d;
40-
});
41-
res.on('end', () => {
42-
const tags: Record<string, string> = JSON.parse(data);
43-
resolve(tags);
44-
});
45-
});
46-
});
47-
};
48-
49-
const verifyPackage = async (dir: string, preid: string = 'beta'): Promise<boolean> => {
13+
const verifyPackage = async (dir: string, preid = 'beta'): Promise<boolean> => {
5014
const cwd = dir;
5115
const json = JSON.parse(readFileSync(path.join(cwd, 'package.json'), { encoding: 'utf-8' }));
5216
if (json.private) {
5317
return true;
5418
}
55-
const distTags = await getDistTags(json.name);
56-
if (json.version !== distTags[preid]) {
57-
console.log(`${json.name} missing. Expected ${json.version}, latest is ${distTags[preid]}`);
58-
const { stdout, exitCode } = await execa('npm', ['publish', '--tag', preid], { cwd });
59-
console.log(stdout);
60-
return exitCode === 0;
61-
} else {
62-
console.log(`${json.name} matches expected version ${json.version}`);
19+
20+
try {
21+
const distTags = await getDistTags(json.name);
22+
if (json.version !== distTags[preid]) {
23+
console.log(`${json.name} missing. Expected ${json.version}, latest is ${distTags[preid]}`);
24+
const { stdout, exitCode } = await execa('npm', ['publish', '--tag', preid], { cwd });
25+
console.log(stdout);
26+
return exitCode === 0;
27+
} else {
28+
console.log(`${json.name} matches expected version ${json.version}`);
29+
}
30+
return true;
31+
} catch (e) {
32+
console.warn(`Failed to fetch dist tags for ${json.name}`, e);
33+
return false;
6334
}
64-
return true;
6535
};
6636

6737
const verify = async (preid?: string) => {
68-
await getLernaModules();
38+
await getLernaModuleLocations();
6939
for (let i = 0; i < lernaModuleLocations.length; i++) {
7040
const dir = lernaModuleLocations[i];
7141
if (!(await verifyPackage(dir, preid))) {

0 commit comments

Comments
 (0)