12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- /*eslint-disable no-console*/
- const exec = require('shell-utils').exec;
- const p = require('path');
- const semver = require('semver');
- const fs = require('fs');
-
- const ONLY_ON_BRANCH = 'v2';
- const VERSION_TAG = 'alpha';
- const VERSION_INC = 'patch';
-
- function run() {
- // if (!validateEnv()) {
- // return;
- // }
- // setupGit();
- // createNpmRc();
- versionTagAndPublish();
- }
-
- function validateEnv() {
- if (!process.env.CI || !process.env.TRAVIS) {
- throw new Error(`releasing is only available from Travis CI`);
- }
-
- if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
- console.log(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
- return false;
- }
-
- if (process.env.TRAVIS_BRANCH !== ONLY_ON_BRANCH) {
- console.log(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
- return false;
- }
-
- 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() {
- 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 = semver.gt(packageVersion, currentPublished) ? packageVersion : semver.inc(currentPublished, VERSION_INC);
- tryPublishAndTag(version);
- }
-
- function findCurrentPublishedVersion() {
- return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
- }
-
- function tryPublishAndTag(version) {
- let theCandidate = version;
- let retry = 0;
- while (retry < 5) { //eslint-disable-line
- try {
- tagAndPublish(theCandidate);
- console.log(`Released ${theCandidate}`);
- return;
- } catch (e) {
- const alreadyPublished = _.includes(e.message, 'You cannot publish over the previously published version');
- if (!alreadyPublished) {
- throw e;
- }
- retry++;
- theCandidate = semver.inc(theCandidate, VERSION_INC);
- }
- }
- }
-
- function tagAndPublish(newVersion) {
- exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
- exec.execSync(`npm publish --tag ${VERSION_TAG}`);
- exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
- exec.execSyncSilent(`git push deploy ${newVersion} || true`);
- }
-
- run();
|