update-docs-branch.js 2.0 KB

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