scripts/insert-in-readme.js
Rama main · réplica de lectura del repositorio original.
#!/usr/bin/env node
import { readFile, writeFile } from 'fs/promises';
async function main(content, key, code) {
// The script piped in failed: keep the README as it is
if (!content.trim()) throw new Error(`Nothing to insert for ${key}`)
console.log(content)
// Read README.md
const readme = await readFile('README.md', 'utf-8')
const keyText = `<!-- Auto generated ${key} -->`
// Replace content between keys
const lines = readme.split('\n')
const keyIndexStart = lines.findIndex((line) => line.trim() === keyText)
const keyIndexEnd = lines.findIndex((line, index) => index > keyIndexStart && line.trim() === keyText)
if(keyIndexStart < 0 || keyIndexEnd < 0) throw new Error(`Key not found in readme: ${key}`)
if(code) {
lines.splice(keyIndexStart + 1, keyIndexEnd - keyIndexStart - 1, `
\`\`\`
${content}
\`\`\`
`)
} else {
lines.splice(keyIndexStart + 1, keyIndexEnd - keyIndexStart - 1, content)
}
// Write README.md
await writeFile('README.md', lines.join('\n'))
}
// Read piped content
let content = ''
process.stdin.on('data', (chunk) => {
content += chunk
})
process.stdin.on('end', () => {
main(content, process.argv[2], process.argv[3] === '--code')
})