49 lines
1.4 KiB
JavaScript
49 lines
1.4 KiB
JavaScript
import fs from 'node:fs'
|
|
|
|
/**
|
|
* Idempotently append an `- insert:` patch block to a DSH cordis.patch.yml
|
|
* file unless a row with the same full package name already exists.
|
|
*
|
|
* Usage: node patch-cordis.mjs --file <path> --name <full pkg name> [--id <id>]
|
|
*/
|
|
function parseArg(key) {
|
|
const i = process.argv.indexOf(key)
|
|
return i >= 0 ? process.argv[i + 1] : undefined
|
|
}
|
|
|
|
const file = parseArg('--file')
|
|
const name = parseArg('--name')
|
|
const id = parseArg('--id') || (name ? name.split('/').pop() : undefined)
|
|
|
|
if (!file || !name || !id) {
|
|
console.error('usage: node patch-cordis.mjs --file <path> --name <full-pkg-name> [--id <id>]')
|
|
process.exit(2)
|
|
}
|
|
|
|
let text
|
|
try {
|
|
text = fs.readFileSync(file, 'utf8')
|
|
} catch (err) {
|
|
console.error(`cannot read ${file}: ${err.message}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
// Idempotency: if a row with this name already exists, nothing to do.
|
|
if (text.includes(`name: '${name}'`) || text.includes(`name: "${name}"`)) {
|
|
console.log(`already present: ${name} — no change`)
|
|
process.exit(0)
|
|
}
|
|
|
|
const block = [
|
|
'',
|
|
'- insert:',
|
|
' - id: ' + id,
|
|
` name: '${name}'`,
|
|
' config: {}',
|
|
'',
|
|
].join('\n')
|
|
|
|
// Append preserving a single trailing newline.
|
|
const trimmed = text.endsWith('\n') ? text.slice(0, -1) : text
|
|
fs.writeFileSync(file, trimmed + '\n' + block + '\n')
|
|
console.log(`appended insert for ${name} (id=${id}) to ${file}`) |