react-native-navigation的迁移库

release.js 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*eslint-disable no-console*/
  2. const exec = require('shell-utils').exec;
  3. const p = require('path');
  4. const semver = require('semver');
  5. const fs = require('fs');
  6. const ONLY_ON_BRANCH = 'v2';
  7. const VERSION_TAG = 'alpha';
  8. const VERSION_INC = 'patch';
  9. function validateEnv() {
  10. if (!process.env.CI || !process.env.TRAVIS) {
  11. throw new Error(`releasing is only available from Travis CI`);
  12. }
  13. if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
  14. console.log(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
  15. return false;
  16. }
  17. if (process.env.TRAVIS_BRANCH !== ONLY_ON_BRANCH) {
  18. console.log(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
  19. return false;
  20. }
  21. return true;
  22. }
  23. function setupGit() {
  24. exec.execSyncSilent(`git config --global push.default simple`);
  25. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  26. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  27. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  28. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  29. exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  30. }
  31. function calcNewVersion() {
  32. const packageVersion = semver.clean(process.env.npm_package_version);
  33. console.log(`package version: ${packageVersion}`);
  34. const commitCount = exec.execSyncRead(`git rev-list --count ${ONLY_ON_BRANCH}`);
  35. console.log(`new patch: ${commitCount}`);
  36. return `${semver.major(packageVersion)}.${semver.minor(packageVersion)}.${commitCount}`;
  37. }
  38. function createNpmRc() {
  39. const content = `
  40. email=\${NPM_EMAIL}
  41. //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
  42. `;
  43. fs.writeFileSync(`.npmrc`, content);
  44. }
  45. function tagAndPublish(newVersion) {
  46. exec.execSync(`npm version ${newVersion} -f -m "v${newVersion} [ci skip]"`);
  47. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  48. exec.execSyncSilent(`git push deploy --tags || true`);
  49. }
  50. function run() {
  51. if (!validateEnv()) {
  52. return;
  53. }
  54. setupGit();
  55. createNpmRc();
  56. const newVersion = calcNewVersion();
  57. console.log(`new version is: ${newVersion}`);
  58. tagAndPublish(newVersion);
  59. }
  60. run();