react-native-navigation的迁移库

release.js 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 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. let retry = 0;
  60. while (retry < 5) { //eslint-disable-line
  61. try {
  62. tagAndPublish(theCandidate);
  63. console.log(`Released ${theCandidate}`);
  64. return;
  65. } catch (e) {
  66. const alreadyPublished = _.includes(e.message, 'You cannot publish over the previously published version');
  67. if (!alreadyPublished) {
  68. throw e;
  69. }
  70. retry++;
  71. theCandidate = semver.inc(theCandidate, VERSION_INC);
  72. }
  73. }
  74. }
  75. function tagAndPublish(newVersion) {
  76. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  77. exec.execSync(`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();