|
1 | | -const sleep = ms => new Promise(res => setTimeout(res, ms)); |
2 | | - |
3 | | -/** exponential back-off function to await results while avoiding rate limits */ |
4 | | -async function backoff(fn, depth = 0) { |
5 | | - try { |
6 | | - return await fn(); |
7 | | - } catch (e) { |
8 | | - if (depth > 7) { |
9 | | - throw e; |
10 | | - } else { |
11 | | - await sleep(2 ** depth * 10); |
12 | | - return backoff(fn, depth + 1); |
13 | | - } |
14 | | - } |
15 | | -} |
16 | | - |
17 | | -module.exports = async function({ github, glob, workspace, publishedPackages }) { |
| 1 | +module.exports = async function({ github, glob, workspace, context }) { |
18 | 2 | const tar = require('tar'); |
19 | 3 | const { readFile, copyFile } = require('fs').promises; |
20 | 4 |
|
21 | 5 | const { singleFileBuild } = await import('../tools/pfe-tools/esbuild.js'); |
22 | 6 | const { execaCommand } = await import('execa'); |
23 | 7 |
|
| 8 | + const { payload } = context; |
| 9 | + const { release } = payload; |
| 10 | + |
24 | 11 | // https://github.com/patternfly/patternfly-elements |
25 | 12 | const owner = 'patternfly'; |
26 | 13 | const repo = 'patternfly-elements'; |
27 | 14 |
|
| 15 | + const tag = release.tag_name; |
| 16 | + |
28 | 17 | // repo root |
29 | 18 | const cwd = `${workspace}`; |
30 | 19 | const outfile = `${cwd}/pfe.min.js`; |
31 | 20 |
|
32 | 21 | await singleFileBuild({ outfile }); |
33 | 22 |
|
34 | | - async function getRelease({ owner, repo, tag }) { |
35 | | - const release = await github.request(`GET /repos/{owner}/{repo}/releases/tags/{tag}`, { owner, repo, tag }); |
36 | | - if (!release.id) { |
37 | | - throw new Error(`Could not find release for tag: ${tag}`); |
38 | | - } |
39 | | - } |
| 23 | + const params = { |
| 24 | + owner, |
| 25 | + release_id: release.id, |
| 26 | + repo |
| 27 | + }; |
| 28 | + |
| 29 | + await copyFile(`${cwd}/core/pfe-styles/pfe.min.css`, `${cwd}/pfe.min.css`); |
40 | 30 |
|
41 | | - for (const { name: packageName, version } of publishedPackages) { |
42 | | - // get the tag for the release for this package |
43 | | - const tag = `${packageName}@${version}`; |
| 31 | + const globber = await glob.create('pfe.min.*'); |
| 32 | + const files = await globber.glob(); |
44 | 33 |
|
45 | | - // wait until the release is created |
46 | | - const release = await backoff(() => getRelease({ owner, repo, tag })); |
| 34 | + // eslint-disable-next-line |
| 35 | + console.log('creating tarball for', files); |
47 | 36 |
|
48 | | - const params = { |
49 | | - owner, |
50 | | - release_id: release.id, |
51 | | - repo |
52 | | - }; |
| 37 | + await tar.c({ gzip: true, file: 'pfe.min.tgz' }, files); |
53 | 38 |
|
54 | | - await copyFile(`${cwd}/core/pfe-styles/pfe.min.css`, `${cwd}/pfe.min.css`); |
| 39 | + // upload the bundle to each release |
| 40 | + await github.rest.repos.uploadReleaseAsset({ |
| 41 | + ...params, |
| 42 | + name: 'pfe.min.tgz', |
| 43 | + data: await readFile(`${cwd}/pfe.min.tgz`), |
| 44 | + }); |
55 | 45 |
|
56 | | - const globber = await glob.create('pfe.min.*'); |
57 | | - const files = await globber.glob(); |
| 46 | + const { packageName } = tag.match(/^(?<packageName>@[-\w]+[/]{1}[-\w]+)@(.*)$/)?.groups ?? {}; |
58 | 47 |
|
| 48 | + if (!packageName) { |
59 | 49 | // eslint-disable-next-line |
60 | | - console.log('creating tarball for', files); |
| 50 | + console.log(release); |
| 51 | + throw new Error('No Package found'); |
| 52 | + } |
61 | 53 |
|
62 | | - await tar.c({ gzip: true, file: 'pfe.min.tgz' }, files); |
| 54 | + // make a tarball for the package |
| 55 | + // this was already published to npm in the changesets action |
| 56 | + const { stdout } = await execaCommand(`npm run pack -w ${packageName}`); |
| 57 | + const match = stdout.match(/^[\w-.]+\.tgz$/g); |
63 | 58 |
|
64 | | - // upload the bundle to each release |
| 59 | + // upload the package tarball to the release |
| 60 | + if (match) { |
| 61 | + const [name] = match; |
65 | 62 | await github.rest.repos.uploadReleaseAsset({ |
66 | 63 | ...params, |
67 | | - name: 'pfe.min.tgz', |
68 | | - data: await readFile(`${cwd}/pfe.min.tgz`), |
| 64 | + name, |
| 65 | + data: await readFile(`${cwd}/${name}`), |
69 | 66 | }); |
70 | | - |
71 | | - // make a tarball for the package |
72 | | - // this was already published to npm in the changesets action |
73 | | - const { stdout } = await execaCommand(`npm run pack -w ${packageName}`); |
74 | | - const match = stdout.match(/^[\w-.]+\.tgz$/g); |
75 | | - |
76 | | - // upload the package tarball to the release |
77 | | - if (match) { |
78 | | - const [name] = match; |
79 | | - await github.rest.repos.uploadReleaseAsset({ |
80 | | - ...params, |
81 | | - name, |
82 | | - data: await readFile(`${cwd}/${name}`), |
83 | | - }); |
84 | | - } |
85 | 67 | } |
86 | 68 | }; |
0 commit comments