From bb35debb776f8186bdda05e7600c1bdb9066d27e Mon Sep 17 00:00:00 2001 From: Bart Tadych Date: Sun, 14 Jan 2024 12:54:37 +0100 Subject: [PATCH] 0.3.0. (#4) --- scripts/append-ga.cjs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 scripts/append-ga.cjs diff --git a/scripts/append-ga.cjs b/scripts/append-ga.cjs new file mode 100644 index 0000000..05760a3 --- /dev/null +++ b/scripts/append-ga.cjs @@ -0,0 +1,43 @@ +const fs = require('fs'); +const path = require('path'); + +const SCRIPT = +` +`; + +function appendGa(path) { + let html = fs.readFileSync(path, 'utf8'); + if (html.includes(SCRIPT)) { + console.log(`👌 ${path} already has tracking`); + return; + } + const pos = html.lastIndexOf(''); + if (pos < 0) { + throw new Error('Could not find tag'); + } + html = html.substring(0, pos) + SCRIPT + html.substring(pos); + fs.writeFileSync(path, html, 'utf8'); + console.log(`✅ ${path} updated`); +} + +const directory = process.argv[2]; +if (!directory) { + throw new Error('Please specify a directory'); +} + +const dirPath = path.join(__dirname, '..', directory); +fs.readdir(dirPath, (_, files) => { + files.forEach(file => { + if (file.endsWith('.html')) { + const filePath = path.join(dirPath, file); + appendGa(filePath); + } + }); +});