|
| 1 | +const exec = require('shell-utils').exec; |
1 | 2 | const cp = require('child_process');
|
2 |
| -const p = require('path'); |
3 | 3 | const semver = require('semver');
|
| 4 | +const _ = require('lodash'); |
| 5 | +const p = require('path'); |
4 | 6 |
|
5 |
| -function execSync(cmd) { |
6 |
| - cp.execSync(cmd, { stdio: ['inherit', 'inherit', 'inherit'] }); |
7 |
| -} |
| 7 | +// Workaround JS |
8 | 8 |
|
9 |
| -function execSyncRead(cmd) { |
10 |
| - return String(cp.execSync(cmd, { stdio: ['inherit', 'pipe', 'inherit'] })).trim(); |
| 9 | +// Export buildkite variables for Release build |
| 10 | +// We cast toString() because function returns 'object' |
| 11 | +const isRelease = process.env.BUILDKITE_MESSAGE.match(/^release$/i); |
| 12 | +let VERSION; |
| 13 | +if (isRelease) { |
| 14 | + VERSION = cp.execSync(`buildkite-agent meta-data get version`).toString(); |
11 | 15 | }
|
12 | 16 |
|
13 |
| -function execSyncSilently(cmd) { |
14 |
| - cp.execSync(cmd, { stdio: ['ignore', 'ignore', 'ignore'] }); |
| 17 | +const VERSION_TAG = isRelease ? 'latest' : 'snapshot'; |
| 18 | +const VERSION_INC = 'patch'; |
| 19 | +function run() { |
| 20 | + if (!validateEnv()) { |
| 21 | + return; |
| 22 | + } |
| 23 | + setupGit(); |
| 24 | + createNpmRc(); |
| 25 | + versionTagAndPublish(); |
15 | 26 | }
|
16 | 27 |
|
17 | 28 | function validateEnv() {
|
18 |
| - if (!process.env.CI || !process.env.TRAVIS) { |
19 |
| - throw new Error(`releasing is only available from Travis CI`); |
20 |
| - } |
21 |
| - |
22 |
| - if (process.env.TRAVIS_BRANCH !== 'master') { |
23 |
| - console.error(`not publishing on branch ${process.env.TRAVIS_BRANCH}`); |
24 |
| - return false; |
| 29 | + if (!process.env.CI) { |
| 30 | + throw new Error('releasing is only available from CI'); |
25 | 31 | }
|
26 |
| - |
27 |
| - if (process.env.TRAVIS_PULL_REQUEST !== 'false') { |
28 |
| - console.error(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`); |
29 |
| - return false; |
30 |
| - } |
31 |
| - |
32 | 32 | return true;
|
33 | 33 | }
|
34 | 34 |
|
35 | 35 | function setupGit() {
|
36 |
| - execSyncSilently(`git config --global push.default simple`); |
37 |
| - execSyncSilently(`git config --global user.email "${process.env.GIT_EMAIL}"`); |
38 |
| - execSyncSilently(`git config --global user.name "${process.env.GIT_USERNAME}"`); |
39 |
| - const remoteUrl = new RegExp(`https?://(\\S+)`).exec(execSyncRead(`git remote -v`))[1]; |
40 |
| - execSyncSilently(`git remote add deploy "https://${process.env.GIT_USERNAME}:${process.env.GIT_TOKEN}@${remoteUrl}"`); |
41 |
| - execSync(`git checkout master`); |
| 36 | + exec.execSyncSilent('git config --global push.default simple'); |
| 37 | + exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`); |
| 38 | + exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`); |
| 39 | + const remoteUrl = new RegExp('https?://(\\S+)').exec(exec.execSyncRead('git remote -v'))[1]; |
| 40 | + exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`); |
| 41 | + // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`); |
42 | 42 | }
|
43 | 43 |
|
44 |
| -function calcNewVersion() { |
45 |
| - const latestVersion = execSyncRead(`npm view ${process.env.npm_package_name}@latest version`); |
46 |
| - console.log(`latest version is: ${latestVersion}`); |
47 |
| - console.log(`package version is: ${process.env.npm_package_version}`); |
48 |
| - if (semver.gt(process.env.npm_package_version, latestVersion)) { |
49 |
| - return semver.inc(process.env.npm_package_version, 'patch'); |
50 |
| - } else { |
51 |
| - return semver.inc(latestVersion, 'patch'); |
52 |
| - } |
| 44 | +function createNpmRc() { |
| 45 | + exec.execSync('rm -f package-lock.json'); |
| 46 | + const npmrcPath = p.resolve(`${__dirname}/.npmrc`); |
| 47 | + exec.execSync(`cp -rf ${npmrcPath} .`); |
53 | 48 | }
|
54 | 49 |
|
55 |
| -function copyNpmRc() { |
56 |
| - execSync(`rm -f package-lock.json`); |
57 |
| - const npmrcPath = p.resolve(`${__dirname}/.npmrc`); |
58 |
| - execSync(`cp -rf ${npmrcPath} .`); |
| 50 | +function versionTagAndPublish() { |
| 51 | + const currentPublished = findCurrentPublishedVersion(); |
| 52 | + console.log(`current published version: ${currentPublished}`); |
| 53 | + |
| 54 | + const version = isRelease ? VERSION : `${currentPublished}-snapshot.${process.env.BUILDKITE_BUILD_NUMBER}`; |
| 55 | + console.log(`Publishing version: ${version}`); |
| 56 | + |
| 57 | + tryPublishAndTag(version); |
59 | 58 | }
|
60 | 59 |
|
61 |
| -function tagAndPublish(newVersion) { |
62 |
| - console.log(`new version is: ${newVersion}`); |
63 |
| - execSync(`npm version ${newVersion} -m "v${newVersion} [ci skip]"`); |
64 |
| - execSync(`npm publish --tag latest`); |
65 |
| - execSyncSilently(`git push deploy --tags`); |
| 60 | +function findCurrentPublishedVersion() { |
| 61 | + return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`); |
66 | 62 | }
|
67 | 63 |
|
68 |
| -function run() { |
69 |
| - if (!validateEnv()) { |
70 |
| - return; |
| 64 | +function tryPublishAndTag(version) { |
| 65 | + let theCandidate = version; |
| 66 | + for (let retry = 0; retry < 5; retry++) { |
| 67 | + try { |
| 68 | + tagAndPublish(theCandidate); |
| 69 | + console.log(`Released ${theCandidate}`); |
| 70 | + return; |
| 71 | + } catch (err) { |
| 72 | + const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version'); |
| 73 | + if (!alreadyPublished) { |
| 74 | + throw err; |
| 75 | + } |
| 76 | + console.log(`previously published. retrying with increased ${VERSION_INC}...`); |
| 77 | + theCandidate = semver.inc(theCandidate, VERSION_INC); |
| 78 | + } |
71 | 79 | }
|
72 |
| - setupGit(); |
73 |
| - copyNpmRc(); |
74 |
| - tagAndPublish(calcNewVersion()); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +function tagAndPublish(newVersion) { |
| 84 | + console.log(`trying to publish ${newVersion}...`); |
| 85 | + exec.execSync(`npm --no-git-tag-version version ${newVersion}`); |
| 86 | + exec.execSync(`npm publish --tag ${VERSION_TAG}`); |
| 87 | + if (isRelease) { |
| 88 | + exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`); |
| 89 | + } |
| 90 | + exec.execSyncSilent(`git push deploy ${newVersion} || true`); |
75 | 91 | }
|
76 | 92 |
|
77 | 93 | run();
|
0 commit comments