react-native-navigation的迁移库

release.js 3.1KB

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