react-native-navigation的迁移库

release.js 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const cp = require('child_process');
  2. const p = require('path');
  3. const semver = require('semver');
  4. function execSync(cmd) {
  5. cp.execSync(cmd, { stdio: ['inherit', 'inherit', 'inherit'] });
  6. }
  7. function execSyncRead(cmd) {
  8. return String(cp.execSync(cmd, { stdio: ['inherit', 'pipe', 'inherit'] })).trim();
  9. }
  10. function execSyncSilently(cmd) {
  11. cp.execSync(cmd, { stdio: ['ignore', 'ignore', 'ignore'] });
  12. }
  13. function validateEnv() {
  14. if (!process.env.CI || !process.env.TRAVIS) {
  15. throw new Error(`releasing is only available from Travis CI`);
  16. }
  17. if (process.env.TRAVIS_BRANCH !== 'master') {
  18. console.error(`not publishing on branch ${process.env.TRAVIS_BRANCH}`);
  19. return false;
  20. }
  21. if (process.env.TRAVIS_PULL_REQUEST !== 'false') {
  22. console.error(`not publishing as triggered by pull request ${process.env.TRAVIS_PULL_REQUEST}`);
  23. return false;
  24. }
  25. return true;
  26. }
  27. function setupGit() {
  28. execSyncSilently(`git config --global push.default simple`);
  29. execSyncSilently(`git config --global user.email "${process.env.GIT_EMAIL}"`);
  30. execSyncSilently(`git config --global user.name "${process.env.GIT_USER}"`);
  31. const remoteUrl = new RegExp(`https?://(\\S+)`).exec(execSyncRead(`git remote -v`))[1];
  32. execSyncSilently(`git remote add deploy "https://${process.env.GIT_USER}:${process.env.GIT_TOKEN}@${remoteUrl}"`);
  33. execSync(`git checkout master`);
  34. }
  35. function calcNewVersion() {
  36. const latestVersion = execSyncRead(`npm view ${process.env.npm_package_name}@latest version`);
  37. console.log(`latest version is: ${latestVersion}`);
  38. console.log(`package version is: ${process.env.npm_package_version}`);
  39. if (semver.gt(process.env.npm_package_version, latestVersion)) {
  40. return semver.inc(process.env.npm_package_version, 'patch');
  41. } else {
  42. return semver.inc(latestVersion, 'patch');
  43. }
  44. }
  45. function copyNpmRc() {
  46. const npmrcPath = p.resolve(`${__dirname}/.npmrc`);
  47. execSync(`cp -rf ${npmrcPath} .`);
  48. }
  49. function tagAndPublish(newVersion) {
  50. console.log(`new version is: ${newVersion}`);
  51. execSync(`npm version ${newVersion} -m "v${newVersion} [ci skip]"`);
  52. execSyncSilently(`git push deploy --tags`);
  53. execSync(`npm publish --tag latest`);
  54. }
  55. function run() {
  56. if (!validateEnv()) {
  57. return;
  58. }
  59. setupGit();
  60. copyNpmRc();
  61. tagAndPublish(calcNewVersion());
  62. }
  63. run();