|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * Updates the Rollbar snippet in js and html files under a specified base |
| 5 | + * directory from the project root. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * update-snippets <baseDir> |
| 9 | + * |
| 10 | + * If <baseDir> is omitted, it defaults to '.' (current directory). |
| 11 | + * |
| 12 | + * Replaces content between "// Rollbar Snippet" and "// End Rollbar Snippet" markers. |
| 13 | + */ |
| 14 | + |
| 15 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 16 | +import { fileURLToPath } from 'node:url'; |
| 17 | +import path from 'node:path'; |
| 18 | +import glob from 'glob'; |
| 19 | + |
| 20 | +import { findUp } from './util.js'; |
| 21 | + |
| 22 | +const verbose = ['--verbose', '-v'].some((f) => process.argv.includes(f)); |
| 23 | +const baseDir = process.argv.at(verbose ? 3 : 2); |
| 24 | + |
| 25 | +function replaceSnippet({ content, snippet, regex, file, root }) { |
| 26 | + let didMatch = false; |
| 27 | + let wasReplaced = false; |
| 28 | + |
| 29 | + const updatedContent = content.replace(regex, (...args) => { |
| 30 | + const { indent, start, currentSnippet, end } = args.at(-1); // groups |
| 31 | + didMatch = true; |
| 32 | + wasReplaced = currentSnippet.trim() !== snippet; |
| 33 | + console.log(` ${wasReplaced ? '✓' : '-'} ${path.relative(root, file)}`); |
| 34 | + return `${indent}${start}${indent}${snippet}${end}`; |
| 35 | + }); |
| 36 | + |
| 37 | + if (verbose && !didMatch) { |
| 38 | + console.log(` ✗ ${path.relative(root, file)}`); |
| 39 | + } |
| 40 | + |
| 41 | + return { updatedContent, wasReplaced }; |
| 42 | +} |
| 43 | + |
| 44 | +async function updateSnippets({ dir }) { |
| 45 | + if (!dir) { |
| 46 | + throw new Error('baseDir is required.'); |
| 47 | + } |
| 48 | + |
| 49 | + const cwd = path.dirname(fileURLToPath(import.meta.url)); |
| 50 | + const root = await findUp({ fileName: 'package.json', dir: cwd }); |
| 51 | + |
| 52 | + console.log(`Updating Rollbar snippets in ${path.relative(root, cwd)}...`); |
| 53 | + |
| 54 | + if (verbose) { |
| 55 | + console.log(`Found project root at ${root}`); |
| 56 | + } |
| 57 | + |
| 58 | + const snippetPath = path.join(root, 'dist/rollbar.snippet.js'); |
| 59 | + const snippet = await readFile(snippetPath, 'utf8'); |
| 60 | + |
| 61 | + const regex = new RegExp( |
| 62 | + '^' + |
| 63 | + '(?<indent>[ \t]*)' + |
| 64 | + '(?<start>// Rollbar Snippet\n)' + |
| 65 | + '(?<currentSnippet>.*?)' + |
| 66 | + '(?<end>\n[ \t]*// End Rollbar Snippet)' + |
| 67 | + '$', |
| 68 | + 'ms', |
| 69 | + ); |
| 70 | + |
| 71 | + const files = glob.sync(`${dir}/**/*.{html,js}`, { |
| 72 | + cwd: root, |
| 73 | + absolute: true, |
| 74 | + ignore: ['**/.git/**', '**/node_modules/**'], |
| 75 | + }); |
| 76 | + |
| 77 | + if (files.length === 0) { |
| 78 | + throw new Error(`No files found in ${path.relative(root, dir)} to update.`); |
| 79 | + } |
| 80 | + |
| 81 | + let snippetsUpdated = 0; |
| 82 | + |
| 83 | + for (const file of files) { |
| 84 | + const content = await readFile(file, 'utf8'); |
| 85 | + |
| 86 | + const { updatedContent, wasReplaced } = replaceSnippet({ |
| 87 | + content, |
| 88 | + snippet, |
| 89 | + regex, |
| 90 | + file, |
| 91 | + root, |
| 92 | + }); |
| 93 | + |
| 94 | + if (wasReplaced) { |
| 95 | + await writeFile(file, updatedContent); |
| 96 | + snippetsUpdated++; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + console.log(`${snippetsUpdated} snippets updated.`); |
| 101 | +} |
| 102 | + |
| 103 | +updateSnippets({ dir: baseDir }).catch((err) => { |
| 104 | + console.error('Error updating snippets:', err); |
| 105 | + console.error('\nUsage: update-snippets [--verbose|-v] <baseDir>'); |
| 106 | + console.error('Example:'); |
| 107 | + console.error(' update-snippets examples'); |
| 108 | + process.exit(1); |
| 109 | +}); |
0 commit comments