Skip to content

Commit 1d48e98

Browse files
Merge pull request #5834 from BitGo/BTC-1947.refactor-scripts-convert-arrow-functions
refactor(scripts): improve verify-release function readability
2 parents a5fb7eb + 0afed78 commit 1d48e98

File tree

2 files changed

+36
-66
lines changed

2 files changed

+36
-66
lines changed

scripts/prepare-release.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,28 +15,28 @@ let lernaModuleLocations: string[] = [];
1515
let TARGET_SCOPE = '@bitgo-beta';
1616
let filesChanged = 0;
1717

18-
const setLernaModules = async (): Promise<void> => {
18+
async function setLernaModules(): Promise<void> {
1919
const modules = await getLernaModules();
2020
lernaModules = modules.map(({ name }) => name);
2121
lernaModuleLocations = modules.map(({ location }) => location);
22-
};
22+
}
2323

24-
const replacePackageScopes = () => {
24+
function replacePackageScopes() {
2525
// replace all @bitgo packages & source code with alternate SCOPE
2626
const filePaths = [...walk(path.join(__dirname, '../', 'modules')), ...walk(path.join(__dirname, '../', 'webpack'))];
2727
filePaths.forEach((file) => {
2828
filesChanged += changeScopeInFile(file, lernaModules, TARGET_SCOPE);
2929
});
30-
};
30+
}
3131

3232
// modules/bitgo is the only package we publish without an `@bitgo` prefix, so
3333
// we must manually set one
34-
const replaceBitGoPackageScope = () => {
34+
function replaceBitGoPackageScope() {
3535
const cwd = path.join(__dirname, '../', 'modules', 'bitgo');
3636
const json = JSON.parse(readFileSync(path.join(cwd, 'package.json'), { encoding: 'utf-8' }));
3737
json.name = `${TARGET_SCOPE}/bitgo`;
3838
writeFileSync(path.join(cwd, 'package.json'), JSON.stringify(json, null, 2) + '\n');
39-
};
39+
}
4040

4141
/** Small version checkers in place of an npm dependency installation */
4242
function compareversion(version1, version2) {
@@ -73,7 +73,7 @@ function compareversion(version1, version2) {
7373
*
7474
* @param {String | undefined} preid
7575
*/
76-
const incrementVersions = async (preid = 'beta') => {
76+
async function incrementVersions(preid = 'beta') {
7777
const distTags = await getDistTagsForModuleLocations(lernaModuleLocations);
7878
for (let i = 0; i < lernaModuleLocations.length; i++) {
7979
try {
@@ -118,19 +118,19 @@ const incrementVersions = async (preid = 'beta') => {
118118
console.warn(`Couldn't set next version for ${lernaModuleLocations[i]}`, e);
119119
}
120120
}
121-
};
121+
}
122122

123-
const getArgs = () => {
123+
function getArgs() {
124124
const args = process.argv.slice(2) || [];
125125
const scopeArg = args.find((arg) => arg.startsWith('scope='));
126126
if (scopeArg) {
127127
const split = scopeArg.split('=');
128128
TARGET_SCOPE = split[1] || TARGET_SCOPE;
129129
}
130130
console.log(`Preparing to re-target to ${TARGET_SCOPE}`);
131-
};
131+
}
132132

133-
const main = async (preid?: string) => {
133+
async function main(preid?: string) {
134134
getArgs();
135135
await setLernaModules();
136136
replacePackageScopes();
@@ -143,6 +143,6 @@ const main = async (preid?: string) => {
143143
console.error('No files were changed, something must have gone wrong.');
144144
process.exit(1);
145145
}
146-
};
146+
}
147147

148148
main(process.argv.slice(2)[0]);

scripts/verify-release.ts

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,49 @@
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+
async function getLernaModuleLocations(): Promise<void> {
9+
const modules = await getLernaModules();
2810
lernaModuleLocations = modules.map(({ location }) => location);
29-
};
30-
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-
};
11+
}
4812

49-
const verifyPackage = async (dir: string, preid: string = 'beta'): Promise<boolean> => {
13+
async function verifyPackage(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;
65-
};
35+
}
6636

67-
const verify = async (preid?: string) => {
68-
await getLernaModules();
37+
async function verify(preid?: string) {
38+
await getLernaModuleLocations();
6939
for (let i = 0; i < lernaModuleLocations.length; i++) {
7040
const dir = lernaModuleLocations[i];
7141
if (!(await verifyPackage(dir, preid))) {
7242
console.error('Failed to verify outstanding packages.');
7343
return;
7444
}
7545
}
76-
};
46+
}
7747

7848
// e.g. for alpha releases: `npx ts-node ./scripts/verify-beta.ts alpha`
7949
verify(process.argv.slice(2)[0]);

0 commit comments

Comments
 (0)