|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { scanCodebase } from '../services/scanCodebase.js'; |
| 4 | +import { parseEnvFile } from '../services/parseEnvFile.js'; |
| 5 | +import { findDuplicateKeys } from '../core/duplicates.js'; |
| 6 | +import type { ScanOptions } from '../config/types.js'; |
| 7 | +import { DEFAULT_ENV_FILE, DEFAULT_EXAMPLE_FILE } from '../config/constants.js'; |
| 8 | +import { printExplain, type ExplainResult } from '../ui/scan/printExplain.js'; |
| 9 | +import { skipCommentedUsages } from '../core/helpers/skipCommentedUsages.js'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Options forwarded from the CLI for the --explain command. |
| 13 | + */ |
| 14 | +export interface ExplainOptions extends ScanOptions { |
| 15 | + key: string; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Implements `dotenv-diff --explain <KEY>`. |
| 20 | + * |
| 21 | + * Reports where the key is defined in env files, where it is used in the |
| 22 | + * codebase, and its overall status (defined / used / duplicated / ignored). |
| 23 | + * @param opts Explain options from CLI |
| 24 | + * @returns void |
| 25 | + */ |
| 26 | +export async function explainKey(opts: ExplainOptions): Promise<void> { |
| 27 | + const { key, cwd, ignore, ignoreRegex } = opts; |
| 28 | + |
| 29 | + // Find env files that contain the key |
| 30 | + const envFiles = discoverEnvFilesSync(cwd); |
| 31 | + const definedIn: string[] = []; |
| 32 | + const isDuplicated = envFiles.some((filePath) => { |
| 33 | + const dups = findDuplicateKeys(filePath); |
| 34 | + return dups.some((d) => d.key === key); |
| 35 | + }); |
| 36 | + |
| 37 | + for (const filePath of envFiles) { |
| 38 | + const parsed = parseEnvFile(filePath); |
| 39 | + if (Object.prototype.hasOwnProperty.call(parsed, key)) { |
| 40 | + definedIn.push(path.relative(cwd, filePath)); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // Scan codebase for usages |
| 45 | + const scanResult = await scanCodebase(opts); |
| 46 | + |
| 47 | + // Filter out commented usages |
| 48 | + const filteredUsages = skipCommentedUsages(scanResult.used); |
| 49 | + const usages = filteredUsages.filter((u) => u.variable === key); |
| 50 | + |
| 51 | + // Check ignore status |
| 52 | + const isIgnored = |
| 53 | + ignore.includes(key) || ignoreRegex.some((rx) => rx.test(key)); |
| 54 | + |
| 55 | + // Print result |
| 56 | + const result: ExplainResult = { |
| 57 | + key, |
| 58 | + definedIn, |
| 59 | + usages, |
| 60 | + isDuplicated, |
| 61 | + isIgnored, |
| 62 | + }; |
| 63 | + |
| 64 | + if (opts.json) { |
| 65 | + console.log(JSON.stringify(result, null, 2)); |
| 66 | + } else { |
| 67 | + printExplain(result); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Returns absolute paths to all .env* files in the cwd |
| 73 | + * (both env files and example files). |
| 74 | + * @param cwd Directory to search in |
| 75 | + * @returns Array of absolute file paths |
| 76 | + */ |
| 77 | +function discoverEnvFilesSync(cwd: string): string[] { |
| 78 | + let entries: string[] = []; |
| 79 | + try { |
| 80 | + entries = fs.readdirSync(cwd); |
| 81 | + } catch { |
| 82 | + return []; |
| 83 | + } |
| 84 | + |
| 85 | + return entries |
| 86 | + .filter( |
| 87 | + (f) => |
| 88 | + f.startsWith(DEFAULT_ENV_FILE) || f.startsWith(DEFAULT_EXAMPLE_FILE), |
| 89 | + ) |
| 90 | + .map((f) => path.resolve(cwd, f)); |
| 91 | +} |
0 commit comments