Add testing for telemetry in local dist assets

This commit is contained in:
Benjamin Lu
2026-01-28 15:23:25 -08:00
parent 074ec623f0
commit bca2c6203f
3 changed files with 94 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
import { readFile, stat } from 'node:fs/promises'
import { extname, resolve } from 'node:path'
import { glob } from 'glob'
type Pattern = {
label: string
regex: RegExp
}
const distDir = resolve('dist')
const ignoredExtensions = new Set(['.map', '.svg'])
const telemetryPatterns: Pattern[] = [
{ label: 'GTM container', regex: /GTM-[A-Z0-9]+/i },
{ label: 'GTM script', regex: /gtm\.js/i },
{ label: 'Google Tag Manager', regex: /googletagmanager/i },
{ label: 'dataLayer', regex: /\bdataLayer\b/ }
]
const distStats = await stat(distDir).catch(() => null)
if (!distStats?.isDirectory()) {
console.error('dist directory not found. Run pnpm build first.')
process.exit(1)
}
const files = await glob('dist/**/*', { nodir: true })
const violations: Array<{
file: string
hits: Array<{ label: string; match: string }>
}> = []
for (const file of files) {
const extension = extname(file).toLowerCase()
if (ignoredExtensions.has(extension)) continue
const content = (await readFile(file)).toString('utf8')
const hits = telemetryPatterns
.map((pattern) => {
const match = content.match(pattern.regex)
return match ? { label: pattern.label, match: match[0] } : null
})
.filter((hit): hit is { label: string; match: string } => hit !== null)
if (hits.length > 0) {
violations.push({ file, hits })
}
}
if (violations.length > 0) {
console.error('Telemetry references found in dist assets:')
for (const violation of violations) {
const formattedHits = violation.hits
.map((hit) => `${hit.label} (${hit.match})`)
.join(', ')
console.error(`- ${violation.file}: ${formattedHits}`)
}
process.exit(1)
}