UNPKG

env-toolkit

Version:

This is a package that can generate or compare env files for your project.

22 lines (21 loc) 817 B
import { statSync, readFileSync } from 'fs'; import { globSync } from 'glob'; export function findEnvVars(directory, excludeFolders, envPatterns) { const envVars = new Set(); const excludedFolders = excludeFolders.map((folder) => `${directory}/${folder}/**`); const filesToSearch = `${directory}/**/*.{js,ts}`; const foundFiles = globSync(filesToSearch, { ignore: excludedFolders }); for (const file of foundFiles) { const stats = statSync(file); if (stats.isFile()) { const content = readFileSync(file, 'utf8'); for (const pattern of envPatterns) { let match; while ((match = pattern.exec(content)) !== null) { envVars.add(match[1]); } } } } return envVars; }