123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- /*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 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 getNextVersion() {
- const packageVersion = semver.clean(process.env.npm_package_version);
- console.log(`package version: ${packageVersion}`);
- const current = exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
- console.log(`current published version: ${current}`);
-
- if (semver.gt(packageVersion, current)) {
- return packageVersion;
- } else {
- return semver.inc(current, VERSION_INC);
- }
- }
-
- function calcNewVersion() {
- let candidate = getNextVersion();
- while (isAlreadyPublished(candidate)) {
- console.log(`${candidate} already published`);
- candidate = semver.inc(candidate, VERSION_INC);
- }
- return candidate;
- }
-
- function isAlreadyPublished(version) {
- return exec.execSyncRead(`npm view ${process.env.npm_package_name}@${version} version`).length > 0;
- }
-
- function createNpmRc() {
- const content = `
- email=\${NPM_EMAIL}
- //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
- `;
- fs.writeFileSync(`.npmrc`, content);
- }
-
- function tagAndPublish(newVersion) {
- exec.execSync(`npm version ${newVersion} -f -m "v${newVersion} [ci skip]"`);
- exec.execSync(`npm publish --tag ${VERSION_TAG}`);
- exec.execSyncSilent(`git push deploy --tags || true`);
- }
-
- function run() {
- if (!validateEnv()) {
- return;
- }
- setupGit();
- createNpmRc();
- const newVersion = calcNewVersion();
- console.log(`new version is: ${newVersion}`);
- tagAndPublish(newVersion);
- }
-
- run();
|