|
1 | 1 | #!/usr/bin/env node |
2 | | -const fs = require( 'fs' ); |
3 | | -const path = require( 'path' ); |
4 | | -const matter = require( 'gray-matter' ); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const matter = require('gray-matter'); |
5 | 5 |
|
6 | | -const instructionsDir = path.resolve( __dirname, '../.github/instructions' ); |
| 6 | +const instructionsDir = path.resolve(__dirname, '../.github/instructions'); |
7 | 7 | const allowedReferences = { |
8 | | - '_index.instructions.md': [ '../custom-instructions.md' ], |
| 8 | + // '_index.instructions.md': [ '../custom-instructions.md' ], |
9 | 9 | }; |
10 | 10 |
|
11 | | -function getAllowedReferences( fileName ) { |
12 | | - if ( allowedReferences[ fileName ] ) { |
13 | | - return allowedReferences[ fileName ]; |
14 | | - } |
15 | | - |
16 | | - return [ '../custom-instructions.md', './_index.instructions.md' ]; |
| 11 | +function getAllowedReferences(fileName) { |
| 12 | + return ['../custom-instructions.md']; |
17 | 13 | } |
18 | 14 |
|
19 | | -function removeSection( content, heading ) { |
20 | | - const lines = content.split( /\r?\n/ ); |
21 | | - const headingPattern = new RegExp( `^#{1,6}\\s+${ heading }\\s*$`, 'i' ); |
| 15 | +function removeSection(content, heading) { |
| 16 | + const lines = content.split(/\r?\n/); |
| 17 | + const headingPattern = new RegExp(`^#{1,6}\\s+${heading}\\s*$`, 'i'); |
22 | 18 | const result = []; |
23 | 19 | let skipping = false; |
24 | 20 |
|
25 | | - for ( let i = 0; i < lines.length; i++ ) { |
| 21 | + for (let i = 0; i < lines.length; i++) { |
26 | 22 | const line = lines[i]; |
27 | 23 | const trimmed = line.trim(); |
28 | 24 |
|
29 | | - if ( ! skipping && headingPattern.test( trimmed ) ) { |
| 25 | + if (!skipping && headingPattern.test(trimmed)) { |
30 | 26 | skipping = true; |
31 | 27 | continue; |
32 | 28 | } |
33 | 29 |
|
34 | | - if ( skipping && /^#{1,6}\s+/.test( trimmed ) ) { |
| 30 | + if (skipping && /^#{1,6}\s+/.test(trimmed)) { |
35 | 31 | skipping = false; |
36 | 32 | } |
37 | 33 |
|
38 | | - if ( skipping ) { |
| 34 | + if (skipping) { |
39 | 35 | continue; |
40 | 36 | } |
41 | 37 |
|
42 | | - result.push( line ); |
| 38 | + result.push(line); |
43 | 39 | } |
44 | 40 |
|
45 | | - return result.join( '\n' ); |
| 41 | + return result.join('\n'); |
46 | 42 | } |
47 | 43 |
|
48 | | -function extractReferences( content ) { |
49 | | - const lines = content.split( /\r?\n/ ); |
| 44 | +function extractReferences(content) { |
| 45 | + const lines = content.split(/\r?\n/); |
50 | 46 | const headingPattern = /^#{1,6}\s+/; |
51 | | - const referenceIndex = lines.findIndex( ( line ) => |
52 | | - /^#{1,6}\s+References$/i.test( line.trim() ) |
| 47 | + const referenceIndex = lines.findIndex((line) => |
| 48 | + /^#{1,6}\s+References$/i.test(line.trim()) |
53 | 49 | ); |
54 | 50 |
|
55 | | - if ( referenceIndex === -1 ) { |
| 51 | + if (referenceIndex === -1) { |
56 | 52 | return { cleaned: content, references: [] }; |
57 | 53 | } |
58 | 54 |
|
59 | 55 | let endIndex = referenceIndex + 1; |
60 | | - while ( endIndex < lines.length && ! headingPattern.test( lines[ endIndex ].trim() ) ) { |
| 56 | + while ( |
| 57 | + endIndex < lines.length && |
| 58 | + !headingPattern.test(lines[endIndex].trim()) |
| 59 | + ) { |
61 | 60 | endIndex++; |
62 | 61 | } |
63 | 62 |
|
64 | 63 | const entries = lines |
65 | | - .slice( referenceIndex + 1, endIndex ) |
66 | | - .map( ( line ) => line.trim() ) |
67 | | - .filter( ( line ) => line.startsWith( '- ' ) ) |
68 | | - .map( ( line ) => line.slice( 2 ).trim() ) |
69 | | - .filter( Boolean ); |
| 64 | + .slice(referenceIndex + 1, endIndex) |
| 65 | + .map((line) => line.trim()) |
| 66 | + .filter((line) => line.startsWith('- ')) |
| 67 | + .map((line) => line.slice(2).trim()) |
| 68 | + .filter(Boolean); |
70 | 69 |
|
71 | 70 | const cleanedLines = [ |
72 | | - ...lines.slice( 0, referenceIndex ), |
73 | | - ...lines.slice( endIndex ), |
| 71 | + ...lines.slice(0, referenceIndex), |
| 72 | + ...lines.slice(endIndex), |
74 | 73 | ]; |
75 | 74 |
|
76 | | - return { cleaned: cleanedLines.join( '\n' ), references: entries }; |
| 75 | + return { cleaned: cleanedLines.join('\n'), references: entries }; |
77 | 76 | } |
78 | 77 |
|
79 | | -function ensureTrailingNewline( text ) { |
80 | | - if ( text.endsWith( '\n' ) ) { |
| 78 | +function ensureTrailingNewline(text) { |
| 79 | + if (text.endsWith('\n')) { |
81 | 80 | return text; |
82 | 81 | } |
83 | 82 |
|
84 | | - return `${ text }\n`; |
| 83 | + return `${text}\n`; |
85 | 84 | } |
86 | 85 |
|
87 | 86 | (async function main() { |
88 | 87 | const files = fs |
89 | | - .readdirSync( instructionsDir ) |
90 | | - .filter( ( file ) => file.endsWith( '.instructions.md' ) ) |
91 | | - .map( ( file ) => path.join( instructionsDir, file ) ); |
92 | | - |
93 | | - for ( const filePath of files ) { |
94 | | - const fileName = path.basename( filePath ); |
95 | | - const raw = fs.readFileSync( filePath, 'utf8' ); |
96 | | - const parsed = matter( raw ); |
| 88 | + .readdirSync(instructionsDir) |
| 89 | + .filter((file) => file.endsWith('.instructions.md')) |
| 90 | + .map((file) => path.join(instructionsDir, file)); |
| 91 | + |
| 92 | + for (const filePath of files) { |
| 93 | + const fileName = path.basename(filePath); |
| 94 | + const raw = fs.readFileSync(filePath, 'utf8'); |
| 95 | + const parsed = matter(raw); |
97 | 96 | const data = parsed.data || {}; |
98 | 97 | const content = parsed.content; |
99 | 98 |
|
100 | | - data.references = getAllowedReferences( fileName ); |
| 99 | + data.references = getAllowedReferences(fileName); |
101 | 100 |
|
102 | | - let body = removeSection( content, 'See Also' ); |
103 | | - const extracted = extractReferences( body ); |
| 101 | + let body = removeSection(content, 'See Also'); |
| 102 | + const extracted = extractReferences(body); |
104 | 103 | body = extracted.cleaned; |
105 | 104 |
|
106 | | - const seeAlsoEntries = Array.from( new Set( extracted.references ) ); |
| 105 | + const seeAlsoEntries = Array.from(new Set(extracted.references)); |
107 | 106 |
|
108 | | - if ( seeAlsoEntries.length ) { |
| 107 | + if (seeAlsoEntries.length) { |
109 | 108 | body = body.trimEnd() + '\n\n## See Also\n\n'; |
110 | | - body += seeAlsoEntries.map( ( entry ) => `- ${ entry }` ).join( '\n' ) + '\n'; |
| 109 | + body += |
| 110 | + seeAlsoEntries.map((entry) => `- ${entry}`).join('\n') + '\n'; |
111 | 111 | } else { |
112 | | - body = ensureTrailingNewline( body.trimEnd() ); |
| 112 | + body = ensureTrailingNewline(body.trimEnd()); |
113 | 113 | } |
114 | 114 |
|
115 | | - const output = matter.stringify( body, data ); |
116 | | - fs.writeFileSync( filePath, output, 'utf8' ); |
| 115 | + const output = matter.stringify(body, data); |
| 116 | + fs.writeFileSync(filePath, output, 'utf8'); |
117 | 117 |
|
118 | 118 | console.log( |
119 | | - `Updated ${ fileName }: ${ seeAlsoEntries.length } See Also entr${ seeAlsoEntries.length === 1 ? 'y' : 'ies' }.` |
| 119 | + `Updated ${fileName}: ${seeAlsoEntries.length} See Also entr${seeAlsoEntries.length === 1 ? 'y' : 'ies'}.` |
120 | 120 | ); |
121 | 121 | } |
122 | 122 |
|
123 | | - console.log( '\n✅ Instruction references cleaned. Run the audit if desired.' ); |
124 | | -} )(); |
| 123 | + console.log( |
| 124 | + '\n✅ Instruction references cleaned. Run the audit if desired.' |
| 125 | + ); |
| 126 | +})(); |
0 commit comments