ci-release.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /* eslint no-console: 0 */
  2. const execa = require('execa');
  3. const ALLOW_VERSION = ['major', 'minor', 'patch', 'premajor', 'preminor', 'prepatch', 'prerelease'];
  4. const ALLOW_TAG = ['alpha', 'beta', 'next'];
  5. // release: major release
  6. // release: patch release on alpha
  7. // release: prepatch release on beta
  8. function parseMsg(msg) {
  9. if (!msg.startsWith('release:')) {
  10. throw Error('This is not a release message');
  11. }
  12. const words = msg.replace('release: ', '').split(' ');
  13. const version = words[0];
  14. const tag = words[3];
  15. if (ALLOW_VERSION.indexOf(version) === -1) {
  16. throw new Error(`Invalid version: ${version}`);
  17. }
  18. if (tag && ALLOW_TAG.indexOf(tag) === -1) {
  19. throw new Error(`Invalid tag: ${tag}`);
  20. }
  21. return {
  22. version,
  23. tag
  24. };
  25. }
  26. function release(version, tag) {
  27. console.log(`Ready to release a ${version} version${tag ? ' on ' + tag : ''}.`);
  28. const cmds = [
  29. 'lerna',
  30. 'version',
  31. version,
  32. '--yes',
  33. '--force-publish',
  34. '--conventional-commits',
  35. '--create-release',
  36. 'github'
  37. ];
  38. if (tag) {
  39. // cmds.push('--dist-tag', tag);
  40. }
  41. console.log('EXEC: ' + cmds.join(' '));
  42. execa('npx', cmds)
  43. .then(res => console.log(res.stdout))
  44. .then(() => {
  45. const cmds = ['lerna', 'exec', '--', 'npm', 'publish', '--access', 'public'];
  46. if (tag) {
  47. cmds.push('--tag', tag);
  48. }
  49. return execa('npx', cmds);
  50. })
  51. .then(res => console.log(res.stdout))
  52. .catch(e => console.log(e));
  53. }
  54. const commitMsg = process.env.COMMIT_MESSAGE;
  55. if (commitMsg) {
  56. const { version, tag } = parseMsg(commitMsg);
  57. release(version, tag);
  58. }