-
Notifications
You must be signed in to change notification settings - Fork 3
/
release.mjs
103 lines (93 loc) · 2.83 KB
/
release.mjs
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
// Adapted from https://github.com/coveo/semantic-monorepo-tools/blob/main/scripts/release.mjs
import {
getLastTag,
parseCommits,
getCommits,
getCurrentVersion,
getNextVersion,
npmBumpVersion,
generateChangelog,
writeChangelog,
gitPushTags,
gitTag,
gitSetupUser,
npmPublish,
gitAdd,
gitCommit,
gitPush,
} from '@coveo/semantic-monorepo-tools';
import angularChangelogConvention from 'conventional-changelog-angular';
import {Octokit} from 'octokit';
import {createActionAuth} from '@octokit/auth-action';
// Get all commits since last release bump the root package.json version.
(async () => {
//#region Constants
const PATH = '.';
const VERSION_PREFIX = 'v';
const CONVENTION = await angularChangelogConvention();
const REPO_OWNER = 'coveo';
const REPO_NAME = 'push-api-client.js';
const GIT_USERNAME = 'github-actions';
const GIT_EMAIL = '[email protected]';
//#endregion
// #region Setup Git
await gitSetupUser(GIT_USERNAME, GIT_EMAIL);
//#endregion
//#region GitHub authentication
const octokit = new Octokit({
authStrategy: createActionAuth,
});
//#endregion
//#region Find current and new versions
const lastTag = await getLastTag(VERSION_PREFIX);
// Passing an empty string allow empty commits (i.e. that does not modify any files) to be included.
const commits = await getCommits('', lastTag);
const parsedCommits = parseCommits(commits, CONVENTION.parserOpts);
const bumpInfo = CONVENTION.whatBump(parsedCommits);
const currentVersion = getCurrentVersion(PATH);
const newVersion = getNextVersion(currentVersion, bumpInfo);
const newVersionTag = `${VERSION_PREFIX}${newVersion}`;
//#endregion
// Bump the NPM version.
await npmBumpVersion(newVersion, PATH);
//#region Generate changelog if needed
let changelog = '';
if (parsedCommits.length > 0) {
changelog = await generateChangelog(
parsedCommits,
newVersion,
{
host: 'https://github.com',
owner: REPO_OWNER,
repository: REPO_NAME,
linkReferences: true,
currentTag: newVersionTag,
previousTag: lastTag,
},
CONVENTION.writerOpts
);
await writeChangelog(PATH, changelog);
}
//#endregion
//#region Commit changelog, tag version and push
await gitAdd(PATH);
await gitCommit(`chore(release): ${newVersion} [skip ci]`, PATH);
await gitPush();
//#endregion
//#region Create & push tag
await gitTag(newVersionTag);
await gitPushTags();
//#endregion
// Publish the new version on NPM
await npmPublish(PATH);
//#region Create GitHub Release on last tag
const [, ...bodyArray] = changelog.split('\n');
await octokit.rest.repos.createRelease({
owner: REPO_OWNER,
repo: REPO_NAME,
tag_name: newVersionTag,
name: `Release ${newVersionTag}`,
body: bodyArray.join('\n'),
});
//#endregion
})();