react-native-navigation的迁移库

release.js 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 path = require('path');
  7. // Workaround JS
  8. const isRelease = process.env.RELEASE_BUILD === 'true';
  9. const ONLY_ON_BRANCH = 'origin/master';
  10. const VERSION_TAG = isRelease ? 'latest' : 'snapshot';
  11. const VERSION_INC = 'patch';
  12. function run() {
  13. if (!validateEnv()) {
  14. return;
  15. }
  16. setupGit();
  17. createNpmRc();
  18. versionTagAndPublish();
  19. }
  20. function validateEnv() {
  21. if (!process.env.JENKINS_CI) {
  22. throw new Error(`releasing is only available from CI`);
  23. }
  24. if (!process.env.JENKINS_MASTER) {
  25. console.log(`not publishing on a different build`);
  26. return false;
  27. }
  28. if (process.env.GIT_BRANCH !== ONLY_ON_BRANCH) {
  29. console.log(`not publishing on branch ${process.env.GIT_BRANCH}`);
  30. return false;
  31. }
  32. return true;
  33. }
  34. function setupGit() {
  35. exec.execSyncSilent(`git config --global push.default simple`);
  36. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  37. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  38. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  39. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  40. // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  41. }
  42. function createNpmRc() {
  43. exec.execSync(`rm -f package-lock.json`);
  44. const content = `
  45. email=\${NPM_EMAIL}
  46. //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
  47. `;
  48. fs.writeFileSync(`.npmrc`, content);
  49. }
  50. function versionTagAndPublish() {
  51. const packageVersion = semver.clean(process.env.npm_package_version);
  52. console.log(`package version: ${packageVersion}`);
  53. const currentPublished = findCurrentPublishedVersion();
  54. console.log(`current published version: ${currentPublished}`);
  55. const version = isRelease
  56. ? process.env.VERSION
  57. : semver.gt(packageVersion, currentPublished)
  58. ? `${packageVersion}-snapshot.${process.env.BUILD_ID}`
  59. : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
  60. console.log(`Publishing version: ${version}`);
  61. tryPublishAndTag(version);
  62. }
  63. function findCurrentPublishedVersion() {
  64. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
  65. }
  66. function tryPublishAndTag(version) {
  67. let theCandidate = version;
  68. for (let retry = 0; retry < 5; retry++) {
  69. try {
  70. tagAndPublish(theCandidate);
  71. console.log(`Released ${theCandidate}`);
  72. return;
  73. } catch (err) {
  74. const alreadyPublished = _.includes(err.toString(), 'You cannot publish over the previously published version');
  75. if (!alreadyPublished) {
  76. throw err;
  77. }
  78. console.log(`previously published. retrying with increased ${VERSION_INC}...`);
  79. theCandidate = semver.inc(theCandidate, VERSION_INC);
  80. }
  81. }
  82. }
  83. function tagAndPublish(newVersion) {
  84. console.log(`trying to publish ${newVersion}...`);
  85. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  86. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  87. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  88. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  89. if (isRelease) {
  90. updatePackageJsonGit(newVersion);
  91. }
  92. }
  93. function getPackageJsonPath() {
  94. return `${process.cwd()}/package.json`;
  95. }
  96. function writePackageJson(packageJson) {
  97. fs.writeFileSync(getPackageJsonPath(), JSON.stringify(packageJson, null, 2));
  98. }
  99. function readPackageJson() {
  100. return JSON.parse(fs.readFileSync(getPackageJsonPath()));
  101. }
  102. function updatePackageJsonGit(version) {
  103. exec.execSync(`git checkout master`);
  104. const packageJson = readPackageJson();
  105. packageJson.version = version;
  106. writePackageJson(packageJson);
  107. exec.execSync(`git add package.json`);
  108. exec.execSync(`git commit -m"Update package.json version to ${version} [ci skip]"`);
  109. exec.execSync(`git push deploy master`);
  110. }
  111. run();