react-native-navigation的迁移库

release.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. // Workaround JS
  7. const isRelease = process.env.RELEASE_BUILD === 'true';
  8. const ONLY_ON_BRANCH = 'origin/master';
  9. const VERSION_TAG = isRelease ? 'latest' : 'snapshot';
  10. const VERSION_INC = 'patch';
  11. function run() {
  12. if (!validateEnv()) {
  13. return;
  14. }
  15. setupGit();
  16. createNpmRc();
  17. versionTagAndPublish();
  18. }
  19. function validateEnv() {
  20. if (!process.env.JENKINS_CI) {
  21. throw new Error(`releasing is only available from CI`);
  22. }
  23. if (!process.env.JENKINS_MASTER) {
  24. console.log(`not publishing on a different build`);
  25. return false;
  26. }
  27. if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
  28. console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
  29. return false;
  30. }
  31. return true;
  32. }
  33. function setupGit() {
  34. exec.execSyncSilent(`git config --global push.default simple`);
  35. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  36. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  37. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  38. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  39. // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  40. }
  41. function createNpmRc() {
  42. exec.execSync(`rm -f package-lock.json`);
  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 currentPublished = findCurrentPublishedVersion();
  51. console.log(`current published version: ${currentPublished}`);
  52. const version = isRelease ? process.env.VERSION : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
  53. console.log(`Publishing version: ${version}`);
  54. tryPublishAndTag(version);
  55. }
  56. function findCurrentPublishedVersion() {
  57. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
  58. }
  59. function tryPublishAndTag(version) {
  60. let theCandidate = version;
  61. for (let retry = 0; retry < 5; retry++) {
  62. try {
  63. tagAndPublish(theCandidate);
  64. console.log(`Released ${theCandidate}`);
  65. return;
  66. } catch (err) {
  67. const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
  68. if (!alreadyPublished) {
  69. throw err;
  70. }
  71. console.log(`previously published. retrying with increased ${VERSION_INC}...`);
  72. theCandidate = semver.inc(theCandidate, VERSION_INC);
  73. }
  74. }
  75. }
  76. function tagAndPublish(newVersion) {
  77. console.log(`trying to publish ${newVersion}...`);
  78. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  79. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  80. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  81. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  82. }
  83. run();