|
@@ -3,6 +3,7 @@ const exec = require('shell-utils').exec;
|
3
|
3
|
const semver = require('semver');
|
4
|
4
|
const fs = require('fs');
|
5
|
5
|
const _ = require('lodash');
|
|
6
|
+const path = require('path');
|
6
|
7
|
|
7
|
8
|
// Workaround JS
|
8
|
9
|
const isRelease = process.env.RELEASE_BUILD === 'true';
|
|
@@ -12,88 +13,120 @@ const VERSION_TAG = isRelease ? 'latest' : 'snapshot';
|
12
|
13
|
const VERSION_INC = 'patch';
|
13
|
14
|
|
14
|
15
|
function run() {
|
15
|
|
- if (!validateEnv()) {
|
16
|
|
- return;
|
17
|
|
- }
|
18
|
|
- setupGit();
|
19
|
|
- createNpmRc();
|
20
|
|
- versionTagAndPublish();
|
|
16
|
+ if (!validateEnv()) {
|
|
17
|
+ return;
|
|
18
|
+ }
|
|
19
|
+ setupGit();
|
|
20
|
+ createNpmRc();
|
|
21
|
+ versionTagAndPublish();
|
21
|
22
|
}
|
22
|
23
|
|
23
|
24
|
function validateEnv() {
|
24
|
|
- if (!process.env.JENKINS_CI) {
|
25
|
|
- throw new Error(`releasing is only available from CI`);
|
26
|
|
- }
|
|
25
|
+ if (!process.env.JENKINS_CI) {
|
|
26
|
+ throw new Error(`releasing is only available from CI`);
|
|
27
|
+ }
|
27
|
28
|
|
28
|
|
- if (!process.env.JENKINS_MASTER) {
|
29
|
|
- console.log(`not publishing on a different build`);
|
30
|
|
- return false;
|
31
|
|
- }
|
|
29
|
+ if (!process.env.JENKINS_MASTER) {
|
|
30
|
+ console.log(`not publishing on a different build`);
|
|
31
|
+ return false;
|
|
32
|
+ }
|
32
|
33
|
|
33
|
|
- if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
|
34
|
|
- console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
|
35
|
|
- return false;
|
36
|
|
- }
|
|
34
|
+ if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
|
|
35
|
+ console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
|
|
36
|
+ return false;
|
|
37
|
+ }
|
37
|
38
|
|
38
|
|
- return true;
|
|
39
|
+ return true;
|
39
|
40
|
}
|
40
|
41
|
|
41
|
42
|
function setupGit() {
|
42
|
|
- exec.execSyncSilent(`git config --global push.default simple`);
|
43
|
|
- exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
|
44
|
|
- exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
|
45
|
|
- const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
|
46
|
|
- exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
|
47
|
|
- // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
|
|
43
|
+ exec.execSyncSilent(`git config --global push.default simple`);
|
|
44
|
+ exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
|
|
45
|
+ exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
|
|
46
|
+ const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
|
|
47
|
+ exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
|
|
48
|
+ // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
|
48
|
49
|
}
|
49
|
50
|
|
50
|
51
|
function createNpmRc() {
|
51
|
|
- exec.execSync(`rm -f package-lock.json`);
|
52
|
|
- const content = `
|
|
52
|
+ exec.execSync(`rm -f package-lock.json`);
|
|
53
|
+ const content = `
|
53
|
54
|
email=\${NPM_EMAIL}
|
54
|
55
|
//registry.npmjs.org/:_authToken=\${NPM_TOKEN}
|
55
|
56
|
`;
|
56
|
|
- fs.writeFileSync(`.npmrc`, content);
|
|
57
|
+ fs.writeFileSync(`.npmrc`, content);
|
57
|
58
|
}
|
58
|
59
|
|
59
|
60
|
function versionTagAndPublish() {
|
60
|
|
- const currentPublished = findCurrentPublishedVersion();
|
61
|
|
- console.log(`current published version: ${currentPublished}`);
|
|
61
|
+ const packageVersion = semver.clean(process.env.npm_package_version);
|
|
62
|
+ console.log(`package version: ${packageVersion}`);
|
62
|
63
|
|
63
|
|
- const version = isRelease ? process.env.VERSION : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
|
64
|
|
- console.log(`Publishing version: ${version}`);
|
|
64
|
+ const currentPublished = findCurrentPublishedVersion();
|
|
65
|
+ console.log(`current published version: ${currentPublished}`);
|
65
|
66
|
|
66
|
|
- tryPublishAndTag(version);
|
|
67
|
+ const version = isRelease
|
|
68
|
+ ? process.env.VERSION
|
|
69
|
+ : semver.gt(packageVersion, currentPublished)
|
|
70
|
+ ? `${packageVersion}-snapshot.${process.env.BUILD_ID}`
|
|
71
|
+ : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
|
|
72
|
+
|
|
73
|
+ console.log(`Publishing version: ${version}`);
|
|
74
|
+
|
|
75
|
+ tryPublishAndTag(version);
|
67
|
76
|
}
|
68
|
77
|
|
69
|
78
|
function findCurrentPublishedVersion() {
|
70
|
|
- return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
|
|
79
|
+ return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
|
71
|
80
|
}
|
72
|
81
|
|
73
|
82
|
function tryPublishAndTag(version) {
|
74
|
|
- let theCandidate = version;
|
75
|
|
- for (let retry = 0; retry < 5; retry++) {
|
76
|
|
- try {
|
77
|
|
- tagAndPublish(theCandidate);
|
78
|
|
- console.log(`Released ${theCandidate}`);
|
79
|
|
- return;
|
80
|
|
- } catch (err) {
|
81
|
|
- const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
|
82
|
|
- if (!alreadyPublished) {
|
83
|
|
- throw err;
|
84
|
|
- }
|
85
|
|
- console.log(`previously published. retrying with increased ${VERSION_INC}...`);
|
86
|
|
- theCandidate = semver.inc(theCandidate, VERSION_INC);
|
|
83
|
+ let theCandidate = version;
|
|
84
|
+ for (let retry = 0; retry < 5; retry++) {
|
|
85
|
+ try {
|
|
86
|
+ tagAndPublish(theCandidate);
|
|
87
|
+ console.log(`Released ${theCandidate}`);
|
|
88
|
+ return;
|
|
89
|
+ } catch (err) {
|
|
90
|
+ const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
|
|
91
|
+ if (!alreadyPublished) {
|
|
92
|
+ throw err;
|
|
93
|
+ }
|
|
94
|
+ console.log(`previously published. retrying with increased ${VERSION_INC}...`);
|
|
95
|
+ theCandidate = semver.inc(theCandidate, VERSION_INC);
|
|
96
|
+ }
|
87
|
97
|
}
|
88
|
|
- }
|
89
|
98
|
}
|
90
|
99
|
|
91
|
100
|
function tagAndPublish(newVersion) {
|
92
|
|
- console.log(`trying to publish ${newVersion}...`);
|
93
|
|
- exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
|
94
|
|
- exec.execSync(`npm publish --tag ${VERSION_TAG}`);
|
95
|
|
- exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
|
96
|
|
- exec.execSyncSilent(`git push deploy ${newVersion} || true`);
|
|
101
|
+ console.log(`trying to publish ${newVersion}...`);
|
|
102
|
+ exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
|
|
103
|
+ exec.execSync(`npm publish --tag ${VERSION_TAG}`);
|
|
104
|
+ exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
|
|
105
|
+ exec.execSyncSilent(`git push deploy ${newVersion} || true`);
|
|
106
|
+ if (isRelease) {
|
|
107
|
+ updatePackageJsonGit(newVersion);
|
|
108
|
+ }
|
|
109
|
+}
|
|
110
|
+
|
|
111
|
+function getPackageJsonPath() {
|
|
112
|
+ return `${process.cwd()}/package.json`;
|
|
113
|
+}
|
|
114
|
+
|
|
115
|
+function writePackageJson(packageJson) {
|
|
116
|
+ fs.writeFileSync(getPackageJsonPath(), JSON.stringify(packageJson, null, 2));
|
|
117
|
+}
|
|
118
|
+
|
|
119
|
+function readPackageJson() {
|
|
120
|
+ return JSON.parse(fs.readFileSync(getPackageJsonPath()));
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+function updatePackageJsonGit(version) {
|
|
124
|
+ const packageJson = readPackageJson();
|
|
125
|
+ packageJson.version = version;
|
|
126
|
+ writePackageJson(packageJson);
|
|
127
|
+ exec.execSync(`git add package.json`);
|
|
128
|
+ exec.execSync(`git commit -m"Update package.json version to ${version} [ci skip]"`);
|
|
129
|
+ exec.execSync(`git push`);
|
97
|
130
|
}
|
98
|
131
|
|
99
|
132
|
run();
|