Last active
October 21, 2023 22:15
-
-
Save raineorshine/25439cb47dc725b891d0afbe9f33bbac to your computer and use it in GitHub Desktop.
Simple script to inject a file into a README idempotently.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
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