react-native-navigation的迁移库

release.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* tslint:disable: no-console */
  2. const exec = require('shell-utils').exec;
  3. const semver = require('semver');
  4. const fs = require('fs');
  5. const includes = require('lodash/includes');
  6. const path = require('path');
  7. // Workaround JS
  8. const isRelease = process.env.RELEASE_BUILD === 'true';
  9. const BRANCH = process.env.BRANCH;
  10. let VERSION_TAG = process.env.NPM_TAG;
  11. if (!VERSION_TAG) {
  12. VERSION_TAG = isRelease ? 'latest' : 'snapshot';
  13. }
  14. const VERSION_INC = 'patch';
  15. function run() {
  16. if (!validateEnv()) {
  17. return;
  18. }
  19. setupGit();
  20. createNpmRc();
  21. versionTagAndPublish();
  22. }
  23. function validateEnv() {
  24. if (!process.env.JENKINS_CI) {
  25. throw new Error(`releasing is only available from CI`);
  26. }
  27. if (!process.env.JENKINS_MASTER) {
  28. console.log(`not publishing on a different build`);
  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 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 = isRelease
  55. ? process.env.VERSION
  56. : semver.gt(packageVersion, currentPublished)
  57. ? `${packageVersion}-snapshot.${process.env.BUILD_ID}`
  58. : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
  59. console.log(`Publishing version: ${version}`);
  60. tryPublishAndTag(version);
  61. }
  62. function findCurrentPublishedVersion() {
  63. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
  64. }
  65. function tryPublishAndTag(version) {
  66. let theCandidate = version;
  67. for (let retry = 0; retry < 5; retry++) {
  68. try {
  69. tagAndPublish(theCandidate);
  70. console.log(`Released ${theCandidate}`);
  71. return;
  72. } catch (err) {
  73. const alreadyPublished = includes(err.toString(), 'You cannot publish over the previously published version');
  74. if (!alreadyPublished) {
  75. throw err;
  76. }
  77. console.log(`previously published. retrying with increased ${VERSION_INC}...`);
  78. theCandidate = semver.inc(theCandidate, VERSION_INC);
  79. }
  80. }
  81. }
  82. function tagAndPublish(newVersion) {
  83. console.log(`trying to publish ${newVersion}...`);
  84. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  85. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  86. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  87. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  88. if (isRelease) {
  89. updatePackageJsonGit(newVersion);
  90. }
  91. }
  92. function getPackageJsonPath() {
  93. return `${process.cwd()}/package.json`;
  94. }
  95. function writePackageJson(packageJson) {
  96. fs.writeFileSync(getPackageJsonPath(), JSON.stringify(packageJson, null, 2));
  97. }
  98. function readPackageJson() {
  99. return JSON.parse(fs.readFileSync(getPackageJsonPath()));
  100. }
  101. function updatePackageJsonGit(version) {
  102. exec.execSync(`git checkout ${BRANCH}`);
  103. const packageJson = readPackageJson();
  104. packageJson.version = version;
  105. writePackageJson(packageJson);
  106. exec.execSync(`git add package.json`);
  107. exec.execSync(`git commit -m"Update package.json version to ${version} [ci skip]"`);
  108. exec.execSync(`git push deploy ${BRANCH}`);
  109. }
  110. run();