Skip to content

Instantly share code, notes, and snippets.

@raineorshine
Last active October 21, 2023 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raineorshine/25439cb47dc725b891d0afbe9f33bbac to your computer and use it in GitHub Desktop.
Save raineorshine/25439cb47dc725b891d0afbe9f33bbac to your computer and use it in GitHub Desktop.
Simple script to inject a file into a README idempotently.
/**
Usage:
1. Add <!-- TEMPLATE: myfile.txt --> to your README.md.
2. Run `node inject.js README.md`.
*/
import fs from 'fs'
import path from 'path'
const inputPath = process.argv[2]
if (!inputPath) {
console.error('Please specify a path.')
process.exit(1)
}
const input = fs.readFileSync(inputPath, 'utf-8')
const replaced = input.replace(
/<!-- TEMPLATE( START)?: (.+?) -->(?:.+?<!-- TEMPLATE END: .+? -->)?/gms,
(match, init, injectedPath) => {
const ext = path.extname(injectedPath)
const content = fs.readFileSync(injectedPath, 'utf-8').trim()
return `<!-- TEMPLATE START: ${injectedPath} -->
\`\`\`${ext.slice(1)}
${content}
\`\`\`
<!-- TEMPLATE END: ${injectedPath} -->`
},
)
fs.writeFileSync(inputPath, replaced)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment