react-native-navigation的迁移库

release.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 _ = require('lodash');
  7. const ONLY_ON_BRANCH = 'v2';
  8. const VERSION_TAG = 'alpha';
  9. const VERSION_INC = 'patch';
  10. function run() {
  11. if (!validateEnv()) {
  12. return;
  13. }
  14. setupGit();
  15. createNpmRc();
  16. versionTagAndPublish();
  17. }
  18. function validateEnv() {
  19. if (!process.env.CI || !process.env.TRAVIS) {
  20. throw new Error(`releasing is only available from Travis CI`);
  21. }
  22. if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
  23. console.log(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
  24. return false;
  25. }
  26. if (process.env.TRAVIS_BRANCH !== ONLY_ON_BRANCH) {
  27. console.log(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
  28. return false;
  29. }
  30. return true;
  31. }
  32. function setupGit() {
  33. exec.execSyncSilent(`git config --global push.default simple`);
  34. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  35. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  36. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  37. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  38. exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  39. }
  40. function createNpmRc() {
  41. const content = `
  42. email=\${NPM_EMAIL}
  43. //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
  44. `;
  45. fs.writeFileSync(`.npmrc`, content);
  46. }
  47. function versionTagAndPublish() {
  48. const packageVersion = semver.clean(process.env.npm_package_version);
  49. console.log(`package version: ${packageVersion}`);
  50. const currentPublished = findCurrentPublishedVersion();
  51. console.log(`current published version: ${currentPublished}`);
  52. const version = semver.gt(packageVersion, currentPublished) ? packageVersion : semver.inc(currentPublished, VERSION_INC);
  53. tryPublishAndTag(version);
  54. }
  55. function findCurrentPublishedVersion() {
  56. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
  57. }
  58. function tryPublishAndTag(version) {
  59. let theCandidate = version;
  60. let retry = 0;
  61. while (retry < 5) {
  62. try {
  63. tagAndPublish(theCandidate);
  64. console.log(`Released ${theCandidate}`);
  65. return;
  66. } catch (e) {
  67. const alreadyPublished = _.includes(e.message, 'You cannot publish over the previously published version');
  68. if (!alreadyPublished) {
  69. throw e;
  70. }
  71. retry++;
  72. theCandidate = semver.inc(theCandidate, VERSION_INC);
  73. }
  74. }
  75. }
  76. function tagAndPublish(newVersion) {
  77. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  78. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  79. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  80. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  81. }
  82. run();