@@ -3,13 +3,112 @@ import fs from "node:fs";
33import path from "node:path" ;
44import { buildSecretRefCredentialMatrix } from "../src/secrets/credential-matrix.js" ;
55
6- const outputPath = path . join (
7- process . cwd ( ) ,
8- "docs" ,
9- "reference" ,
10- "secretref-user-supplied-credentials-matrix.json" ,
6+ const CHECK = process . argv . includes ( "--check" ) ;
7+ const WRITE = process . argv . includes ( "--write" ) || ! CHECK ; // Default to write mode if --check is not specified
8+
9+ const ROOT = path . resolve ( import . meta. dirname , ".." ) ;
10+
11+ const matrixPath = path . join ( ROOT , "docs" , "reference" , "secretref-user-supplied-credentials-matrix.json" ) ;
12+ const surfacePath = path . join ( ROOT , "docs" , "reference" , "secretref-credential-surface.md" ) ;
13+
14+ const matrixData = buildSecretRefCredentialMatrix ( ) ;
15+ const matrixJson = JSON . stringify ( matrixData , null , 2 ) + "\n" ;
16+
17+ // Load original markdown file
18+ const originalSurface = fs . readFileSync ( surfacePath , "utf8" ) ;
19+
20+ // Parse current lines to preserve comments/descriptions
21+ const lineMap = new Map < string , string > ( ) ;
22+ for ( const line of originalSurface . split ( / \r ? \n / ) ) {
23+ const match = line . match ( / ^ - ` ( [ ^ ` ] + ) ` / ) ;
24+ if ( match ) {
25+ lineMap . set ( match [ 1 ] , line ) ;
26+ }
27+ }
28+
29+ // Separate keys by config file
30+ const openclawJsonKeys = matrixData . entries
31+ . filter ( ( entry ) => entry . configFile === "openclaw.json" )
32+ . map ( ( entry ) => entry . path ) ;
33+
34+ const authProfilesJsonKeys = matrixData . entries
35+ . filter ( ( entry ) => entry . configFile === "auth-profiles.json" )
36+ . map ( ( entry ) => entry . refPath ?? entry . path ) ;
37+
38+ // Format openclaw.json targets (sorted, descriptions preserved)
39+ const openclawLines = [ ...new Set ( openclawJsonKeys ) ] . toSorted ( ) . map ( ( key ) => {
40+ return lineMap . get ( key ) ?? `- \`${ key } \`` ;
41+ } ) ;
42+
43+ // Format auth-profiles.json targets (sorted, descriptions preserved)
44+ const authProfilesLines = [ ...new Set ( authProfilesJsonKeys ) ] . toSorted ( ) . map ( ( key ) => {
45+ return lineMap . get ( key ) ?? `- \`${ key } \`` ;
46+ } ) ;
47+
48+ // Combine with headers and distinct structures
49+ const newSupportedLines = [
50+ ...openclawLines ,
51+ "" ,
52+ "### `auth-profiles.json` targets (`secrets configure` + `secrets apply` + `secrets audit`)" ,
53+ "" ,
54+ ...authProfilesLines ,
55+ ] ;
56+
57+ const unsupportedSet = new Set ( matrixData . excludedMutableOrRuntimeManaged ) ;
58+ const newUnsupportedLines = [ ...unsupportedSet ] . toSorted ( ) . map ( ( key ) => {
59+ return lineMap . get ( key ) ?? `- \`${ key } \`` ;
60+ } ) ;
61+
62+ // Helper to replace block in markdown
63+ function replaceBlock ( content : string , startMarker : string , endMarker : string , newLines : string [ ] ) : string {
64+ const startIdx = content . indexOf ( startMarker ) ;
65+ const endIdx = content . indexOf ( endMarker ) ;
66+ if ( startIdx === - 1 || endIdx === - 1 || endIdx < startIdx ) {
67+ throw new Error ( `Markers not found: ${ startMarker } or ${ endMarker } ` ) ;
68+ }
69+ const before = content . slice ( 0 , startIdx + startMarker . length ) ;
70+ const after = content . slice ( endIdx ) ;
71+ return `${ before } \n\n${ newLines . join ( "\n" ) } \n\n${ after } ` ;
72+ }
73+
74+ let newSurface = originalSurface ;
75+ newSurface = replaceBlock (
76+ newSurface ,
77+ '[//]: # "secretref-supported-list-start"' ,
78+ '[//]: # "secretref-supported-list-end"' ,
79+ newSupportedLines
80+ ) ;
81+ newSurface = replaceBlock (
82+ newSurface ,
83+ '[//]: # "secretref-unsupported-list-start"' ,
84+ '[//]: # "secretref-unsupported-list-end"' ,
85+ newUnsupportedLines
1186) ;
1287
13- const matrix = buildSecretRefCredentialMatrix ( ) ;
14- fs . writeFileSync ( outputPath , `${ JSON . stringify ( matrix , null , 2 ) } \n` , "utf8" ) ;
15- console . log ( `Wrote ${ outputPath } ` ) ;
88+ // Normalize line endings to LF
89+ newSurface = newSurface . replace ( / \r \n / g, "\n" ) ;
90+
91+ if ( CHECK ) {
92+ const currentMatrix = fs . readFileSync ( matrixPath , "utf8" ) ;
93+ const currentSurface = fs . readFileSync ( surfacePath , "utf8" ) . replace ( / \r \n / g, "\n" ) ;
94+
95+ let drift = false ;
96+ if ( currentMatrix !== matrixJson ) {
97+ console . error ( "Drift detected in secretref-user-supplied-credentials-matrix.json" ) ;
98+ drift = true ;
99+ }
100+ if ( currentSurface !== newSurface ) {
101+ console . error ( "Drift detected in secretref-credential-surface.md" ) ;
102+ drift = true ;
103+ }
104+ if ( drift ) {
105+ console . error ( "Please run: pnpm secretref:docs:gen" ) ;
106+ process . exit ( 1 ) ;
107+ } else {
108+ console . log ( "SecretRef reference docs are up to date." ) ;
109+ }
110+ } else if ( WRITE ) {
111+ fs . writeFileSync ( matrixPath , matrixJson , "utf8" ) ;
112+ fs . writeFileSync ( surfacePath , newSurface , "utf8" ) ;
113+ console . log ( "SecretRef reference docs and matrix generated successfully." ) ;
114+ }
0 commit comments