update-docs-branch.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 (e) {
  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. if (/-.*$/.test(require('../package.json').version)) {
  21. console.error('Refusing to update docs: prerelease detected');
  22. process.exit(0);
  23. }
  24. const current = match.groups;
  25. const docsBranch = `docs-v${current.major}.x`;
  26. // Fetch remotes and find the docs branch if it exists
  27. run('git fetch --all --no-tags');
  28. const matchingDocsBranches = tryRead(`git rev-parse --glob='*/${docsBranch}'`);
  29. if (!matchingDocsBranches) {
  30. // Create the branch
  31. run(`git checkout --orphan ${docsBranch}`);
  32. } else {
  33. const [publishedRef, ...others] = new Set(matchingDocsBranches.split('\n'));
  34. if (others.length > 0) {
  35. console.error(
  36. `Found conflicting ${docsBranch} branches.\n` +
  37. 'Either local branch is outdated or there are multiple matching remote branches.',
  38. );
  39. process.exit(1);
  40. }
  41. const publishedVersion = JSON.parse(read(`git show ${publishedRef}:package.json`)).version;
  42. const publishedMinor = publishedVersion.match(/\d+\.(?<minor>\d+)\.\d+/).groups.minor;
  43. if (current.minor < publishedMinor) {
  44. console.error('Refusing to update docs: newer version is published');
  45. process.exit(0);
  46. }
  47. run('git checkout --quiet --detach');
  48. run(`git reset --soft ${publishedRef}`);
  49. run(`git checkout ${docsBranch}`);
  50. }
  51. run('npm run prepare-docs');
  52. run('git add -f docs'); // --force needed because generated docs files are gitignored
  53. run('git commit -m "Update docs"');
  54. run(`git checkout ${currentBranch}`);