react-native-navigation的迁移库

release.js 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 calcNewVersion() {
  32. const packageVersion = semver.clean(process.env.npm_package_version);
  33. console.log(`package version: ${packageVersion}`);
  34. exec.execSync(`git fetch --unshallow`);
  35. const commitCount = exec.execSyncRead(`git rev-list --count ${ONLY_ON_BRANCH}`);
  36. console.log(`commits in ${ONLY_ON_BRANCH}: ${commitCount}`);
  37. return `${semver.major(packageVersion)}.${semver.minor(packageVersion)}.${commitCount}`;
  38. }
  39. function createNpmRc() {
  40. const content = `
  41. email=\${NPM_EMAIL}
  42. //registry.npmjs.org/:_authToken=\${NPM_TOKEN}
  43. `;
  44. fs.writeFileSync(`.npmrc`, content);
  45. }
  46. function tagAndPublish(newVersion) {
  47. exec.execSync(`npm version ${newVersion} -f -m "v${newVersion} [ci skip]"`);
  48. exec.execSync(`npm publish --tag ${VERSION_TAG}`);
  49. exec.execSyncSilent(`git push deploy --tags || true`);
  50. }
  51. function run() {
  52. if (!validateEnv()) {
  53. return;
  54. }
  55. setupGit();
  56. createNpmRc();
  57. const newVersion = calcNewVersion();
  58. console.log(`new version is: ${newVersion}`);
  59. tagAndPublish(newVersion);
  60. }
  61. run();