forked from lokalise/node-service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateEnvVarDoc.ts
More file actions
52 lines (44 loc) · 1.74 KB
/
validateEnvVarDoc.ts
File metadata and controls
52 lines (44 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { readFile } from 'node:fs/promises'
import { resolve } from 'node:path'
import { consoleLog } from './utils/loggingUtils.ts'
import { getRootDirectory } from './utils/pathUtils.ts'
const codeBasePath = resolve(getRootDirectory(), 'src/infrastructure/config.ts')
const docPath = resolve(getRootDirectory(), 'docs/environment-variables.md')
function extractEnvVarsFromCode(content: string): Set<string> {
const regex = /configScope\.\w+\(\s*['"`]([A-Z0-9_]+)['"`]/g
const matches = content.matchAll(regex)
const vars = new Set<string>()
for (const match of matches) {
const varName = match[1]
if (varName) vars.add(varName)
}
return vars
}
function extractEnvVarsFromDocs(content: string): Set<string> {
const regex = /^-\s*(\(OPTIONAL\)\s*)?`([A-Z0-9_]+)`/gm
const matches = content.matchAll(regex)
const vars = new Set<string>()
for (const match of matches) {
const varName = match[2]
if (varName) vars.add(varName)
}
return vars
}
async function run() {
const content = await readFile(codeBasePath, 'utf-8')
const varsInFile = extractEnvVarsFromCode(content)
const docContent = await readFile(docPath, 'utf-8')
const documentedVars = extractEnvVarsFromDocs(docContent)
const missing = [...varsInFile].filter((v) => !documentedVars.has(v))
if (missing.length > 0) {
// biome-ignore lint/suspicious/noConsole: <biomev2 migration>
console.error('❌ Missing documentation for the following environment variables:')
for (const v of missing) {
// biome-ignore lint/suspicious/noConsole: <biomev2 migration>
console.error(`- ${v}`)
}
throw new Error('Environment variable documentation validation failed')
}
consoleLog('✅ All environment variables are documented!')
}
void run()