react-native-navigation的迁移库

release.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = 'origin/master';
  7. const VERSION_TAG = process.env.RELEASE_BUILD ? 'latest' : 'snapshot';
  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.JENKINS_CI) {
  19. throw new Error(`releasing is only available from CI`);
  20. }
  21. if (!process.env.JENKINS_MASTER) {
  22. console.log(`not publishing on a different build`);
  23. return false;
  24. }
  25. if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
  26. console.log(`not publishing on branch ${process.env.GIT_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. exec.execSync(`rm -f package-lock.json`);
  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 currentPublished = findCurrentPublishedVersion();
  49. console.log(`current published version: ${currentPublished}`);
  50. const version = process.env.RELEASE_BUILD ? process.env.VERSION : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
  51. tryPublishAndTag(version);
  52. }
  53. function findCurrentPublishedVersion() {
  54. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
  55. }
  56. function tryPublishAndTag(version) {
  57. let theCandidate = version;
  58. for (let retry = 0; retry < 5; retry++) {
  59. try {
  60. tagAndPublish(theCandidate);
  61. console.log(`Released ${theCandidate}`);
  62. return;
  63. } catch (err) {
  64. const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
  65. if (!alreadyPublished) {
  66. throw err;
  67. }
  68. console.log(`previously published. retrying with increased ${VERSION_INC}...`);
  69. theCandidate = semver.inc(theCandidate, VERSION_INC);
  70. }
  71. }
  72. }
  73. function tagAndPublish(newVersion) {
  74. console.log(`trying to publish ${newVersion}...`);
  75. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  76. exec.execSyncRead(`npm publish --tag ${VERSION_TAG}`);
  77. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  78. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  79. }
  80. run();