|
1 | 1 | import * as execa from 'execa'; |
2 | 2 | import { readFileSync } from 'fs'; |
3 | 3 | import * as path from 'path'; |
4 | | -import { get as httpGet } from 'https'; |
| 4 | +import { getLernaModules, getDistTags } from './prepareRelease'; |
5 | 5 |
|
6 | 6 | let lernaModuleLocations: string[] = []; |
7 | 7 |
|
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(); |
28 | 10 | lernaModuleLocations = modules.map(({ location }) => location); |
29 | 11 | }; |
30 | 12 |
|
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> => { |
50 | 14 | const cwd = dir; |
51 | 15 | const json = JSON.parse(readFileSync(path.join(cwd, 'package.json'), { encoding: 'utf-8' })); |
52 | 16 | if (json.private) { |
53 | 17 | return true; |
54 | 18 | } |
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; |
63 | 34 | } |
64 | | - return true; |
65 | 35 | }; |
66 | 36 |
|
67 | 37 | const verify = async (preid?: string) => { |
68 | | - await getLernaModules(); |
| 38 | + await getLernaModuleLocations(); |
69 | 39 | for (let i = 0; i < lernaModuleLocations.length; i++) { |
70 | 40 | const dir = lernaModuleLocations[i]; |
71 | 41 | if (!(await verifyPackage(dir, preid))) { |
|
0 commit comments