react-native-navigation的迁移库

release.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*eslint-disable no-console*/
  2. const exec = require('shell-utils').exec;
  3. const p = require('path');
  4. const semver = require('semver');
  5. const fs = require('fs');
  6. const ONLY_ON_BRANCH = 'v2';
  7. const VERSION_TAG = 'alpha';
  8. const VERSION_INC = 'patch';
  9. function validateEnv() {
  10. if (!process.env.CI || !process.env.TRAVIS) {
  11. throw new Error(`releasing is only available from Travis CI`);
  12. }
  13. if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
  14. console.log(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
  15. return false;
  16. }
  17. if (process.env.TRAVIS_BRANCH !== ONLY_ON_BRANCH) {
  18. console.log(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
  19. return false;
  20. }
  21. return true;
  22. }
  23. function setupGit() {
  24. exec.execSyncSilent(`git config --global push.default simple`);
  25. exec.execSyncSilent(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  26. exec.execSyncSilent(`git config --global user.name "${process.env.GIT_USER}"`);
  27. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(exec.execSyncRead(`git remote -v`))[1];
  28. exec.execSyncSilent(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  29. exec.execSync(`git checkout ${ONLY_ON_BRANCH}`);
  30. }
  31. function getNextVersion() {
  32. const packageVersion = semver.clean(process.env.npm_package_version);
  33. console.log(`package version: ${packageVersion}`);
  34. const current = exec.execSyncRead(`npm view ${process.env.npm_package_name} dist-tags.${VERSION_TAG}`);
  35. console.log(`current published version: ${current}`);
  36. if (semver.gt(packageVersion, current)) {
  37. return packageVersion;
  38. } else {
  39. return semver.inc(current, VERSION_INC);
  40. }
  41. }
  42. function calcNewVersion() {
  43. let candidate = getNextVersion();
  44. while (isAlreadyPublished(candidate)) {
  45. console.log(`${candidate} already published`);
  46. candidate = semver.inc(candidate, VERSION_INC);
  47. }
  48. return candidate;
  49. }
  50. function isAlreadyPublished(version) {
  51. return exec.execSyncRead(`npm view ${process.env.npm_package_name}@${version} version`).length > 0;
  52. }
  53. function createNpmRc() {
  54. const content = `
  55. email=\${NPM_EMAIL}
  56. //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
  57. `;
  58. fs.writeFileSync(`.npmrc`, content);
  59. }
  60. function tagAndPublish(newVersion) {
  61. exec.execSync(`npm version ${newVersion} -f -m "v${newVersion} [ci skip]"`);
  62. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  63. exec.execSyncSilent(`git push deploy --tags || true`);
  64. }
  65. function run() {
  66. if (!validateEnv()) {
  67. return;
  68. }
  69. setupGit();
  70. createNpmRc();
  71. const newVersion = calcNewVersion();
  72. console.log(`new version is: ${newVersion}`);
  73. tagAndPublish(newVersion);
  74. }
  75. run();