-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathadd-epilogue-final.js
More file actions
113 lines (97 loc) · 3.6 KB
/
add-epilogue-final.js
File metadata and controls
113 lines (97 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
// @ts-check
/**
* Final carefully tested script to add doc epilogue to all commands
*/
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import { commandMetadata } from './bin/commandMetadata.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const binDir = path.join(__dirname, 'bin')
/**
* Update a command file to add the doc epilogue
*/
function updateCommandFile(filePath, commandName) {
const metadata = commandMetadata[commandName]
if (!metadata) {
console.log(`⚠️ No metadata found for ${commandName}`)
return false
}
let content = fs.readFileSync(filePath, 'utf-8')
// Check if already has epilogue
if (content.includes('.epilog(buildDocEpilogue')) {
console.log(`✓ Already updated: ${commandName}`)
return false
}
const { category, relatedCommands = [] } = metadata
const epilogueCall = `.epilog(buildDocEpilogue('${commandName}', '${category}', [${relatedCommands.map(cmd => `'${cmd}'`).join(', ')}]))`
// Add import if not present
if (!content.includes('buildDocEpilogue')) {
// Find the last import statement (by looking for 'from' or 'import')
const lastImportMatch = content.match(/^import\s+.*from\s+['"][^'"]*['"]\s*\n/gm)
if (lastImportMatch && lastImportMatch.length > 0) {
const lastImport = lastImportMatch[lastImportMatch.length - 1]
const insertPos = content.indexOf(lastImport) + lastImport.length
content = content.slice(0, insertPos) +
`import { buildDocEpilogue } from '../utils/doc-linker.js'\n` +
content.slice(insertPos)
}
}
// Find and update the builder export with epilogue
const builderMatch = content.match(/export\s+const\s+builder\s*=/)
if (!builderMatch) {
return false
}
// Find the first .example( after the builder export
const exampleIdx = content.indexOf('.example(', builderMatch.index)
if (exampleIdx === -1) {
console.log(`⚠️ No .example() found in ${commandName}`)
return false
}
// Count parentheses carefully from the opening paren of .example(
let parenCount = 0
let foundStart = false
let idx = exampleIdx + '.example'.length
while (idx < content.length) {
const char = content[idx]
if (char === '(') {
parenCount++
foundStart = true
} else if (char === ')') {
if (foundStart) {
parenCount--
if (parenCount === 0) {
// Found the closing paren
content = content.slice(0, idx + 1) + epilogueCall + content.slice(idx + 1)
break
}
}
}
idx++
}
fs.writeFileSync(filePath, content)
console.log(`✅ Updated: ${commandName}`)
return true
}
// Main execution
const files = fs.readdirSync(binDir)
.filter(f => f.endsWith('.js') &&
f !== 'index.js' &&
f !== 'cli.js' &&
f !== 'commandMap.js' &&
f !== 'commandMetadata.js')
console.log(`Found ${files.length} command files to process...`)
let updated = 0
for (const file of files) {
const commandName = path.basename(file, '.js')
const filePath = path.join(binDir, file)
try {
if (updateCommandFile(filePath, commandName)) {
updated++
}
} catch (err) {
console.log(`❌ Error processing ${commandName}: ${err.message}`)
}
}
console.log(`\n📊 Summary: Successfully updated ${updated} files`)