|
1 | | -import { readFileSync, writeFileSync } from "node:fs"; |
2 | 1 | import { join } from "node:path"; |
3 | 2 | import { err, ok, Result } from "neverthrow"; |
4 | 3 | import { |
@@ -34,13 +33,8 @@ import { SECTIONS } from "../../src/sections/registry.js"; |
34 | 33 | import { agree } from "../../src/text.js"; |
35 | 34 | import type { UndeclaredPolicy } from "../../src/types.js"; |
36 | 35 | import { COUNT_WORD_MAX, countWord } from "./lib/count-word.js"; |
37 | | -import { |
38 | | - block, |
39 | | - blockLine, |
40 | | - blockLines, |
41 | | - GeneratedRegion, |
42 | | - regenerateRegions, |
43 | | -} from "./lib/generated-regions.js"; |
| 36 | +import { block, blockLine, blockLines, GeneratedRegion } from "./lib/generated-regions.js"; |
| 37 | +import { type GeneratedFiles, regenerateFiles } from "./lib/region-driver.js"; |
44 | 38 |
|
45 | 39 | const ROOT = join(import.meta.dir, "..", ".."); |
46 | 40 |
|
@@ -665,96 +659,77 @@ function yamlDeclarations<T>( |
665 | 659 | * Each region reads its body back and re-renders it, so a marker moved elsewhere fails instead of regenerating |
666 | 660 | * in the wrong place or erasing authored text. |
667 | 661 | */ |
668 | | -export const GENERATED_REGIONS: Readonly<Record<string, readonly GeneratedRegion[]>> = { |
669 | | - "action.yml": [ |
670 | | - GeneratedRegion.of<Readonly<Record<string, Pick<InputDecl, "description" | "default">>>>({ |
671 | | - name: "action-inputs", |
672 | | - placement: { kind: "under-key", key: "inputs" }, |
673 | | - data: () => INPUT_DECLS, |
674 | | - render: block(renderActionInputs), |
675 | | - parse: yamlDeclarations("inputs", INPUT_ENTRY), |
676 | | - }), |
677 | | - GeneratedRegion.of<Readonly<Record<string, { readonly description: string }>>>({ |
678 | | - name: "action-outputs", |
679 | | - placement: { kind: "under-key", key: "outputs" }, |
680 | | - data: () => OUTPUT_DECLS, |
681 | | - render: block(renderActionOutputs), |
682 | | - parse: yamlDeclarations("outputs", OUTPUT_ENTRY), |
683 | | - }), |
684 | | - ], |
685 | | - "docs/reference/undeclared-policy.md": [ |
686 | | - GeneratedRegion.of<readonly string[]>({ |
687 | | - name: "policy-count-sentence", |
688 | | - placement: { kind: "under-heading", heading: "# The undeclared policy" }, |
689 | | - data: () => countSentenceKeys(knobbedSections()), |
690 | | - render: block(countSentence), |
691 | | - parse: parseCountSentence, |
692 | | - }), |
693 | | - GeneratedRegion.of<readonly PolicyRow[]>({ |
694 | | - name: "policy-defaults-table", |
695 | | - placement: { kind: "under-heading", heading: "## Defaults per section" }, |
696 | | - data: () => policyRows(knobbedSections(), POLICY_ROW_PROSE), |
697 | | - render: block(renderPolicyRows), |
698 | | - parse: tableBody(DEFAULTS_TABLE_HEADER, "keyed", policyRow), |
699 | | - }), |
700 | | - ], |
701 | | - "docs/reference/permissions.md": [ |
702 | | - GeneratedRegion.of({ |
703 | | - name: "permissions-grant-sentence", |
704 | | - placement: { kind: "under-heading", heading: "## What to grant" }, |
705 | | - data: () => grantLabels(SECTIONS), |
706 | | - render: block(grantSentence), |
707 | | - parse: parseGrantSentence, |
708 | | - }), |
709 | | - GeneratedRegion.of<readonly GatedRead[]>({ |
710 | | - name: "permissions-gated-reads", |
711 | | - placement: { kind: "under-heading", heading: "## How a denial surfaces" }, |
712 | | - data: () => gatedReads(SECTIONS), |
713 | | - render: block(gatedBullets), |
714 | | - parse: (body) => blockLines(body).andThen((lines) => parseGatedBullets(lines, 1)), |
715 | | - }), |
716 | | - ], |
717 | | - "docs/operate/check-mode.md": [ |
718 | | - GeneratedRegion.of<readonly GatedRead[]>({ |
719 | | - name: "check-mode-gated-reads", |
720 | | - placement: { |
721 | | - kind: "under-heading", |
722 | | - heading: "## Checking settings changes on pull requests", |
723 | | - }, |
724 | | - data: () => gatedReads(SECTIONS), |
725 | | - render: block(checkModeGatedReads), |
726 | | - parse: parseCheckModeGatedReads, |
727 | | - }), |
728 | | - ], |
| 662 | +export const FILES: GeneratedFiles = { |
| 663 | + "action.yml": { |
| 664 | + regions: [ |
| 665 | + GeneratedRegion.of<Readonly<Record<string, Pick<InputDecl, "description" | "default">>>>({ |
| 666 | + name: "action-inputs", |
| 667 | + placement: { kind: "under-key", key: "inputs" }, |
| 668 | + data: () => INPUT_DECLS, |
| 669 | + render: block(renderActionInputs), |
| 670 | + parse: yamlDeclarations("inputs", INPUT_ENTRY), |
| 671 | + }), |
| 672 | + GeneratedRegion.of<Readonly<Record<string, { readonly description: string }>>>({ |
| 673 | + name: "action-outputs", |
| 674 | + placement: { kind: "under-key", key: "outputs" }, |
| 675 | + data: () => OUTPUT_DECLS, |
| 676 | + render: block(renderActionOutputs), |
| 677 | + parse: yamlDeclarations("outputs", OUTPUT_ENTRY), |
| 678 | + }), |
| 679 | + ], |
| 680 | + }, |
| 681 | + "docs/reference/undeclared-policy.md": { |
| 682 | + regions: [ |
| 683 | + GeneratedRegion.of<readonly string[]>({ |
| 684 | + name: "policy-count-sentence", |
| 685 | + placement: { kind: "under-heading", heading: "# The undeclared policy" }, |
| 686 | + data: () => countSentenceKeys(knobbedSections()), |
| 687 | + render: block(countSentence), |
| 688 | + parse: parseCountSentence, |
| 689 | + }), |
| 690 | + GeneratedRegion.of<readonly PolicyRow[]>({ |
| 691 | + name: "policy-defaults-table", |
| 692 | + placement: { kind: "under-heading", heading: "## Defaults per section" }, |
| 693 | + data: () => policyRows(knobbedSections(), POLICY_ROW_PROSE), |
| 694 | + render: block(renderPolicyRows), |
| 695 | + parse: tableBody(DEFAULTS_TABLE_HEADER, "keyed", policyRow), |
| 696 | + }), |
| 697 | + ], |
| 698 | + }, |
| 699 | + "docs/reference/permissions.md": { |
| 700 | + regions: [ |
| 701 | + GeneratedRegion.of({ |
| 702 | + name: "permissions-grant-sentence", |
| 703 | + placement: { kind: "under-heading", heading: "## What to grant" }, |
| 704 | + data: () => grantLabels(SECTIONS), |
| 705 | + render: block(grantSentence), |
| 706 | + parse: parseGrantSentence, |
| 707 | + }), |
| 708 | + GeneratedRegion.of<readonly GatedRead[]>({ |
| 709 | + name: "permissions-gated-reads", |
| 710 | + placement: { kind: "under-heading", heading: "## How a denial surfaces" }, |
| 711 | + data: () => gatedReads(SECTIONS), |
| 712 | + render: block(gatedBullets), |
| 713 | + parse: (body) => blockLines(body).andThen((lines) => parseGatedBullets(lines, 1)), |
| 714 | + }), |
| 715 | + ], |
| 716 | + }, |
| 717 | + "docs/operate/check-mode.md": { |
| 718 | + regions: [ |
| 719 | + GeneratedRegion.of<readonly GatedRead[]>({ |
| 720 | + name: "check-mode-gated-reads", |
| 721 | + placement: { |
| 722 | + kind: "under-heading", |
| 723 | + heading: "## Checking settings changes on pull requests", |
| 724 | + }, |
| 725 | + data: () => gatedReads(SECTIONS), |
| 726 | + render: block(checkModeGatedReads), |
| 727 | + parse: parseCheckModeGatedReads, |
| 728 | + }), |
| 729 | + ], |
| 730 | + }, |
729 | 731 | }; |
730 | 732 |
|
731 | | -export function regenerateText(path: string, text: string): string { |
732 | | - const regions = GENERATED_REGIONS[path]; |
733 | | - if (regions === undefined) { |
734 | | - throw new Error(`no generated regions are registered for ${path}`); |
735 | | - } |
736 | | - return regenerateRegions(text, regions, path); |
737 | | -} |
738 | | - |
739 | | -export function regenerateAll(): string[] { |
740 | | - const changed: string[] = []; |
741 | | - for (const path of Object.keys(GENERATED_REGIONS)) { |
742 | | - const file = join(ROOT, path); |
743 | | - const before = readFileSync(file, "utf8"); |
744 | | - const after = regenerateText(path, before); |
745 | | - if (after !== before) { |
746 | | - writeFileSync(file, after); |
747 | | - changed.push(path); |
748 | | - } |
749 | | - } |
750 | | - return changed; |
751 | | -} |
752 | | - |
753 | 733 | if (import.meta.main) { |
754 | | - const changed = regenerateAll(); |
755 | | - console.log( |
756 | | - changed.length === 0 |
757 | | - ? "generated regions already up to date" |
758 | | - : `regenerated ${changed.join(", ")}`, |
759 | | - ); |
| 734 | + regenerateFiles("gen-action-docs", FILES, ROOT); |
760 | 735 | } |
0 commit comments