forked from patternfly/patternfly-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbundle-release.cjs
More file actions
141 lines (108 loc) · 4.3 KB
/
bundle-release.cjs
File metadata and controls
141 lines (108 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const sleep = ms => new Promise(r => setTimeout(r, ms));
const NPM_OUTPUT_FILENAME_RE = /^(?:[\s]+)?(?:npm )?(?<name>[-\w.]+\.tgz)$/mg;
async function execCommand(exec, command) {
const [cmd, ...args] = command.split(' ');
let stdout = '';
let stderr = '';
const code = await exec.exec(cmd, args, {
stdout: data => {
stdout += data.toString();
},
stderr: data => {
stderr += data.toString();
},
});
if (code !== 0) {
throw new Error(stderr);
} else {
return stdout;
}
}
/** Wait exponentially longer, by seconds, each time we fail to fetch the release */
async function backoff(fn, retries = 0, max = 10) {
try {
return await fn();
} catch (e) {
if (retries > max) {
throw e;
}
await sleep(2 ** retries * 1000);
return backoff(fn, retries + 1);
}
}
async function getBundle({ core, glob, workspace }) {
const tar = require('tar');
const { copyFile } = require('fs').promises;
const { singleFileBuild } = await import('../tools/pfe-tools/esbuild.js');
await copyFile(`${workspace}/core/pfe-styles/pfe.min.css`, `${workspace}/pfe.min.css`);
// Create or fetch artifacts
await singleFileBuild({ outfile: `${workspace}/pfe.min.js` });
const globber = await glob.create('pfe.min.*');
const files = (await globber.glob() ?? []).map(path =>
path.replace(workspace, '').replace(/^\//, ''));
const file = 'pfe.min.tgz';
core.debug(`Creating ${file} with`, files.join('\n'), '\n');
await tar.c({ gzip: true, file }, files);
core.debug('Tarball contents:');
await Promise.resolve(tar.t({ file, onentry: x => core.debug(x.header.path) }));
return file;
}
module.exports = async function bundle({ core, exec, github, glob, tags = '', workspace }) {
await execCommand(exec, 'git config advice.detachedHead false');
const { readFile } = require('fs').promises;
tags = tags.split(',').map(x => x.trim());
// https://github.com/patternfly/patternfly-elements
const owner = 'patternfly';
const repo = 'patternfly-elements';
for (const tag of tags) {
core.info(`Bundling tag ${tag}`);
core.debug('Fetching release');
const response = await backoff(() =>
github.rest.repos.getReleaseByTag({ owner, repo, tag }));
const release = response.data;
core.debug(release);
const params = { owner, release_id: release.id, repo };
core.info(`Checking out ${tag}`);
await execCommand(exec, `git checkout ${tag}`);
core.info(`Installing dependencies for ${tag}`);
await execCommand(exec, `npm ci --prefer-offline`);
core.info(`Building tools for ${tag}`);
await execCommand(exec, `npm run build -w @patternfly/pfe-tools -w @patternfly/pfe-styles`);
core.info(`Bundling Packages for ${tag}`);
const bundleFileName = await getBundle({ core, github, glob, workspace });
// Delete any existing asset with that name
for (const { id, name } of release.assets ?? []) {
if (name === bundleFileName) {
core.info(`${name} exists for ${tag}, deleting`);
await github.rest.repos.deleteReleaseAsset({ owner, repo, asset_id: id });
}
}
// Upload the all-repo bundle to the release
const data = await readFile(`${workspace}/${bundleFileName}`);
core.info(`Uploading ${bundleFileName} to ${tag}`);
await github.rest.repos.uploadReleaseAsset({ ...params, name: bundleFileName, data });
// Download the package tarball from NPM
const stdout = await execCommand(exec, `npm pack ${tag}`);
// multiple fallbacks for parsing that output
const {
name = stdout.split('\n').pop().replace(/^npm /, '') ||
`${tag.replace(/[@/]/g, '-')}.tgz`.replace(/^-/, '')
} =
NPM_OUTPUT_FILENAME_RE.exec(stdout)?.groups ?? {};
if (name) {
for (const { id, name: existing } of release.assets ?? []) {
if (existing === name) {
core.info(`${name} exists for ${tag}, deleting`);
await github.rest.repos.deleteReleaseAsset({ owner, repo, asset_id: id });
}
}
// Upload the NPM tarball to the release
const data = await readFile(`${workspace}/${name}`);
core.info(`Uploading ${name} to ${tag}`);
await github.rest.repos.uploadReleaseAsset({ ...params, name, data });
} else {
core.error(stdout);
core.setFailed(`Could not get NPM tarball for ${tag}`);
}
}
};