react-native-navigation的迁移库

release.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. const VERSION_TAG = process.env.NPM_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. return true;
  29. }
  30. function setupGit() {
  31. exec.execSyncSilent(`git config --global push.default simple`);
  32. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  33. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  34. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  35. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  36. // exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  37. }
  38. function createNpmRc() {
  39. exec.execSync(`rm -f package-lock.json`);
  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 = isRelease
  52. ? process.env.VERSION
  53. : semver.gt(packageVersion, currentPublished)
  54. ? `${packageVersion}-snapshot.${process.env.BUILD_ID}`
  55. : `${currentPublished}-snapshot.${process.env.BUILD_ID}`;
  56. console.log(`Publishing version: ${version}`);
  57. tryPublishAndTag(version);
  58. }
  59. function findCurrentPublishedVersion() {
  60. return exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.latest`);
  61. }
  62. function tryPublishAndTag(version) {
  63. let theCandidate = version;
  64. for (let retry = 0; retry < 5; retry++) {
  65. try {
  66. tagAndPublish(theCandidate);
  67. console.log(`Released ${theCandidate}`);
  68. return;
  69. } catch (err) {
  70. const alreadyPublished = includes(err.toString(), 'You cannot publish over the previously published version');
  71. if (!alreadyPublished) {
  72. throw err;
  73. }
  74. console.log(`previously published. retrying with increased ${VERSION_INC}...`);
  75. theCandidate = semver.inc(theCandidate, VERSION_INC);
  76. }
  77. }
  78. }
  79. function tagAndPublish(newVersion) {
  80. console.log(`trying to publish ${newVersion}...`);
  81. exec.execSync(`npm --no-git-tag-version version ${newVersion}`);
  82. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  83. exec.execSync(`git tag -a ${newVersion} -m "${newVersion}"`);
  84. exec.execSyncSilent(`git push deploy ${newVersion} || true`);
  85. if (isRelease) {
  86. updatePackageJsonGit(newVersion);
  87. }
  88. }
  89. function getPackageJsonPath() {
  90. return `${process.cwd()}/package.json`;
  91. }
  92. function writePackageJson(packageJson) {
  93. fs.writeFileSync(getPackageJsonPath(), JSON.stringify(packageJson, null, 2));
  94. }
  95. function readPackageJson() {
  96. return JSON.parse(fs.readFileSync(getPackageJsonPath()));
  97. }
  98. function updatePackageJsonGit(version) {
  99. exec.execSync(`git checkout ${BRANCH}`);
  100. const packageJson = readPackageJson();
  101. packageJson.version = version;
  102. writePackageJson(packageJson);
  103. exec.execSync(`git add package.json`);
  104. exec.execSync(`git commit -m"Update package.json version to ${version} [ci skip]"`);
  105. exec.execSync(`git push deploy ${BRANCH}`);
  106. }
  107. run();