forked from vime-js/vime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
165 lines (135 loc) · 4.36 KB
/
release.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const { cyan, dim, green, red, yellow } = require('kleur');
const execa = require('execa');
const Listr = require('listr');
const path = require('path');
const { Octokit } = require('@octokit/rest');
const common = require('./common');
const fs = require('fs-extra');
async function main() {
try {
const dryRun = process.argv.indexOf('--dry-run') > -1;
if (!process.env.GH_TOKEN) {
throw new Error('env.GH_TOKEN is undefined');
}
const { version } = common.readPkg('core');
checkProductionRelease();
const tasks = [];
const changelog = findChangelog();
// Repo must be clean
common.checkGit(tasks);
const NPM_TAG = 'latest';
if(!dryRun) {
// Copy license
common.packages.forEach(package => { copyLicenseToPackage(package, tasks); });
// copy changelog
common.packages.forEach(package => { copyChangelogToPackage(package, tasks); });
// publish each package in NPM
common.publishPackages(tasks, common.packages, version, NPM_TAG);
// push tag to git remote
publishGit(tasks, version, changelog, NPM_TAG);
}
const listr = new Listr(tasks);
await listr.run();
// Dry run doesn't publish to npm or git
if (dryRun) {
console.log(`
\n${yellow('Did not publish. Remove the "--dry-run" flag to publish:')}\n${green(version)} to ${cyan(NPM_TAG)}\n
`);
} else {
console.log(`\nvime ${version} published to ${NPM_TAG}!! 🎉\n`);
}
} catch (err) {
console.log('\n', red(err), '\n');
process.exit(1);
}
}
function checkProductionRelease() {
const corePath = common.projectPath('core');
const hasEsm = fs.existsSync(path.join(corePath, 'dist', 'esm'));
const hasCjs = fs.existsSync(path.join(corePath, 'dist', 'cjs'));
if (!hasEsm || !hasCjs) {
throw new Error('@vime/core build is not a production build');
}
}
function publishGit(tasks, version, changelog, npmTag) {
const gitTag = `v${version}`;
tasks.push(
{
title: `Tag latest commit ${dim(`(${gitTag})`)}`,
task: () => execa('git', ['tag', `${gitTag}`], { cwd: common.rootDir })
},
{
title: 'Push branches to remote',
task: () => execa('git', ['push'], { cwd: common.rootDir })
},
{
title: 'Push tags to remove',
task: () => execa('git', ['push', '--follow-tags'], { cwd: common.rootDir })
},
{
title: 'Publish Github release',
task: () => publishGithub(version, gitTag, changelog, npmTag)
}
);
}
function copyLicenseToPackage(package, tasks) {
const pkg = common.readPkg(package);
const licenseFileName = 'LICENSE';
const licensePath = path.resolve(common.rootDir, licenseFileName);
const pkgLicensePath = path.resolve(common.projectPath(package), licenseFileName);
tasks.push({
title: `copying license to ${pkg.name}`,
task: () => fs.copyFile(licensePath, pkgLicensePath),
});
}
function copyChangelogToPackage(package, tasks) {
const pkg = common.readPkg(package);
const changelogFileName = 'CHANGELOG.md';
const changelogPath = path.resolve(common.rootDir, changelogFileName);
const pkgChangelogPath = path.resolve(common.projectPath(package), changelogFileName);
tasks.push({
title: `copying changelog to ${pkg.name}`,
task: () => fs.copyFile(changelogPath, pkgChangelogPath),
});
}
function findChangelog() {
const lines = fs.readFileSync('CHANGELOG.md', 'utf-8').toString().split('\n');
let start = -1;
let end = -1;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (/^#{1,2} \d/.test(line)) {
if (start === -1) {
start = i + 1;
} else {
end = i - 1;
break;
}
}
}
if(start === -1 || end === -1) {
throw new Error('changelog diff was not found');
}
return lines.slice(start, end).join('\n').trim();
}
async function publishGithub(version, gitTag, changelog, npmTag) {
// If the npm tag is next then publish as a prerelease
const prerelease = npmTag === 'next' ? true : false;
const octokit = new Octokit({
auth: process.env.GH_TOKEN
});
let branch = (await execa('git', ['symbolic-ref', '--short', 'HEAD'])).stdout;
if (!branch) {
branch = 'master';
}
await octokit.repos.createRelease({
owner: 'vime-js',
repo: 'vime',
target_commitish: branch,
tag_name: gitTag,
name: version,
body: changelog,
prerelease: prerelease
});
}
main();