update-docs-branch.js 1.8 KB

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