// chezmoi:template:left-delimiter="/*{{" // chezmoi:template:right-delimiter="}}*/" /*{{- /* vim: set filetype=javascript: */ -}}*/ /*{{ if and .hellotech (lookPath "node") -}}*/ #!/usr/bin/env node const fs = require("fs"); const childProcess = require("child_process"); const { promisify } = require("util"); const exec = promisify(childProcess.exec); const readFile = promisify(fs.readFile); const writeFile = promisify(fs.writeFile); const [, , message, mode] = process.argv; async function main() { const file = await readFile(message, { encoding: "utf8" }); let result; try { result = await exec("git rev-parse --abbrev-ref HEAD"); } catch (err) { return } if (message.match(/^#/)) { return } const tags = result.stdout .split("\n") .map(branch => branch.match(/[A-Z]+-\d+/g)) .filter(x => !!x) .reduce((allIDs, matchedIDs) => [...allIDs, ...matchedIDs], []) .map(id => `[${id}]`); if (tags.length === 0) { return; } if (tags.find(tag => file.includes(tag))) { return; } const newFile = mode === 'message' ? file + '\n\n' + tags.join(" ") : `\n\n# ` + tags.join(" ") + "\n" + file; await writeFile(message, newFile); } main().catch(err => { console.error(err); process.exit(1); }); /*{{- end }}*/