react-native-navigation的迁移库

release.js 3.0KB

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