forked from wix/react-native-navigation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.js
152 lines (132 loc) · 4.9 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
/* tslint:disable: no-console */
const exec = require('shell-utils').exec;
const semver = require('semver');
const fs = require('fs');
const cp = require('child_process');
const includes = require('lodash/includes');
const documentation = require('./documentation');
const packageJsonPath = `${process.cwd()}/package.json`;
// Export buildkite variables for Release build
// We cast toString() because 'buildkite-agent meta-data get' function returns 'object'
const BRANCH = process.env.BUILDKITE_BRANCH;
const isRelease = process.env.BUILDKITE_MESSAGE.match(/^release$/i);
let VERSION, VERSION_TAG, BUILD_DOCUMENTATION_VERSION, REMOVE_DOCUMENTATION_VERSION;
if (isRelease) {
VERSION = cp.execSync(`buildkite-agent meta-data get version`).toString();
VERSION_TAG = cp.execSync(`buildkite-agent meta-data get npm-tag`).toString();
BUILD_DOCUMENTATION_VERSION = cp
.execSync(`buildkite-agent meta-data get build-documentation-version`)
.toString();
REMOVE_DOCUMENTATION_VERSION = cp
.execSync(`buildkite-agent meta-data get remove-documentation-version`)
.toString();
}
console.log(typeof VERSION);
// Workaround JS
if (VERSION_TAG == 'null') {
VERSION_TAG = isRelease ? 'latest' : 'snapshot';
}
const VERSION_INC = 'patch';
function run() {
if (!validateEnv()) {
return;
}
setupGit();
createNpmRc();
versionTagAndPublish();
}
function validateEnv() {
if (!process.env.CI) {
throw new Error(`releasing is only available from CI`);
}
return true;
}
function setupGit() {
exec.execSyncSilent(`git config --global push.default simple`);
exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
exec.execSyncSilent(
`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`
);
// exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
}
function createNpmRc() {
exec.execSync(`rm -f package-lock.json`);
const content = `
email=\${NPM_EMAIL}
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
`;
fs.writeFileSync(`.npmrc`, content);
}
function versionTagAndPublish() {
const packageVersion = semver.clean(process.env.npm_package_version);
console.log(`package version: ${packageVersion}`);
const currentPublished = findCurrentPublishedVersion();
console.log(`current published version: ${currentPublished}`);
const version = isRelease
? VERSION
: semver.gt(packageVersion, currentPublished)
? `${packageVersion}-snapshot.${process.env.BUILDKITE_BUILD_NUMBER}`
: `${currentPublished}-snapshot.${process.env.BUILDKITE_BUILD_NUMBER}`;
console.log(`Publishing version: ${version}`);
tryPublishAndTag(version);
}
function findCurrentPublishedVersion() {
return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
}
function tryPublishAndTag(version) {
let theCandidate = version;
for (let retry = 0; retry < 5; retry++) {
try {
tagAndPublish(theCandidate);
console.log(`Released ${theCandidate}`);
return;
} catch (err) {
const alreadyPublished = includes(
err.toString(),
'You cannot publish over the previously published version'
);
if (!alreadyPublished) {
throw err;
}
console.log(`previously published. retrying with increased ${VERSION_INC}...`);
theCandidate = semver.inc(theCandidate, VERSION_INC);
}
}
}
function tagAndPublish(newVersion) {
console.log(`trying to publish ${newVersion}...`);
if (BUILD_DOCUMENTATION_VERSION && BUILD_DOCUMENTATION_VERSION !== 'null')
documentation.release(BUILD_DOCUMENTATION_VERSION, REMOVE_DOCUMENTATION_VERSION);
exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
exec.execSync(`npm publish --tag ${VERSION_TAG}`);
if (isRelease) {
exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
exec.execSyncSilent(`git push deploy ${newVersion} || true`);
updatePackageJsonGit(newVersion);
}
}
function writePackageJson(packageJson) {
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
}
function readPackageJson() {
return JSON.parse(fs.readFileSync(packageJsonPath));
}
function updatePackageJsonGit(version) {
exec.execSync(`git checkout ${BRANCH}`);
const packageJson = readPackageJson();
packageJson.version = version;
writePackageJson(packageJson);
exec.execSync(`git add package.json`);
exec.execSync(`git commit -m"Update package.json version to ${version} [buildkite skip]"`);
exec.execSync(`git push deploy ${BRANCH}`);
draftGitRelease(version);
}
function draftGitRelease(version) {
exec.execSync(`npx gren release --tags=${version}`);
exec.execSync(`sleep 30`);
// For some unknown reason, gren release works well only when calling it twice.
exec.execSync(`npx gren release --tags=${version}`);
}
run();