-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathgenerate-releases.js
More file actions
82 lines (69 loc) · 2.56 KB
/
generate-releases.js
File metadata and controls
82 lines (69 loc) · 2.56 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
const path = require('path');
const fs = require('fs');
const childProcess = require('child_process');
const { getPackages } = require('@manypkg/get-packages');
const { Octokit } = require('octokit');
const semver = require('semver');
const cwd = process.cwd();
// Create release on github
const createRelease = async (octokit, { pkg, tagName }) => {
const changelogPath = path.join(pkg.dir, 'CHANGELOG.md');
const changelog = await fs.promises.readFile(changelogPath, 'utf8');
const changelogArr = changelog.split('\n');
let releaseNotes = [];
// Get release notes from changelog
for (const line of changelogArr) {
if (line.match(/^#{3}\s/)) {
releaseNotes.push(line);
} else if (line.match(/^#{1,3}\s/) && releaseNotes.length > 0) {
break;
} else if (releaseNotes.length > 0) {
releaseNotes.push(line);
}
}
// Check if it's a prerelease
const prereleaseParts =
semver.prerelease(tagName.replace(`${pkg.packageJson.name}@`, '')) || [];
// Create release on github
await octokit.rest.repos.createRelease({
owner: 'contentful',
repo: 'forma-36',
name: tagName,
tag_name: tagName,
body: releaseNotes.join('\n'),
prerelease: prereleaseParts.length > 0,
});
};
// Get only packages that have a new version published
const getReleasedPackages = async (csOutput, pkgs) => {
const tagNameRegex = /New tag:\s+(@contentful\/[^@]+)@([^\s]+)/;
return csOutput.split('\n').reduce((acc, line) => {
const match = line.match(tagNameRegex);
if (match === null) {
return acc;
}
const tagName = [match[1], match[2]].join('@');
const pkg = pkgs.find((p) => p.packageJson?.name === match[1]);
return [...acc, { tagName, pkg }];
}, []);
};
async function main() {
const env = process.env;
const octokit = new Octokit({
auth: `token ${env.GITHUB_TOKEN}`,
});
// Run changesets publish and get stdout
const csOutput = childProcess.execSync('yarn changeset publish').toString();
console.log(csOutput);
const gitPushCommand = `git add . && yarn run pretty:quick
git diff --staged --quiet || git commit -m "docs(changelog): add changelogs for $(git rev-parse --short HEAD) [skip ci]" && git push origin ${env.CIRCLE_BRANCH} --follow-tags`;
// Push updated packages to github with tags
console.log(childProcess.execSync(gitPushCommand));
const { packages: pkgs } = await getPackages(cwd);
const releasedPkgs = await getReleasedPackages(csOutput, pkgs);
// Create release for each published package
for (const pkg of releasedPkgs) {
await createRelease(octokit, pkg);
}
}
main();