update-docs-branch.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. const proc = require('child_process');
  2. const read = cmd => proc.execSync(cmd, { encoding: 'utf8' }).trim();
  3. const run = cmd => {
  4. proc.execSync(cmd, { stdio: 'inherit' });
  5. };
  6. const tryRead = cmd => {
  7. try {
  8. return read(cmd);
  9. } catch {
  10. return undefined;
  11. }
  12. };
  13. const releaseBranchRegex = /^release-v(?<version>(?<major>\d+)\.(?<minor>\d+)(?:\.(?<patch>\d+))?)$/;
  14. const currentBranch = read('git rev-parse --abbrev-ref HEAD');
  15. const match = currentBranch.match(releaseBranchRegex);
  16. if (!match) {
  17. console.error('Not currently on a release branch');
  18. process.exit(1);
  19. }
  20. const pkgVersion = require('../package.json').version;
  21. if (pkgVersion.includes('-') && !pkgVersion.includes('.0.0-')) {
  22. console.error('Refusing to update docs: non-major prerelease detected');
  23. process.exit(0);
  24. }
  25. const current = match.groups;
  26. const docsBranch = `docs-v${current.major}.x`;
  27. // Fetch remotes and find the docs branch if it exists
  28. run('git fetch --all --no-tags');
  29. const matchingDocsBranches = tryRead(`git rev-parse --glob='*/${docsBranch}'`);
  30. if (!matchingDocsBranches) {
  31. // Create the branch
  32. run(`git checkout --orphan ${docsBranch}`);
  33. } else {
  34. const [publishedRef, ...others] = new Set(matchingDocsBranches.split('\n'));
  35. if (others.length > 0) {
  36. console.error(
  37. `Found conflicting ${docsBranch} branches.\n` +
  38. 'Either local branch is outdated or there are multiple matching remote branches.',
  39. );
  40. process.exit(1);
  41. }
  42. const publishedVersion = JSON.parse(read(`git show ${publishedRef}:package.json`)).version;
  43. const publishedMinor = publishedVersion.match(/\d+\.(?<minor>\d+)\.\d+/).groups.minor;
  44. if (current.minor < publishedMinor) {
  45. console.error('Refusing to update docs: newer version is published');
  46. process.exit(0);
  47. }
  48. run('git checkout --quiet --detach');
  49. run(`git reset --soft ${publishedRef}`);
  50. run(`git checkout ${docsBranch}`);
  51. }
  52. run('npm run prepare-docs');
  53. run('git add -f docs'); // --force needed because generated docs files are gitignored
  54. run('git commit -m "Update docs"');
  55. run(`git checkout ${currentBranch}`);