| | | 1 | | /** One `KEY=value` assignment of an env file, in file order. */ |
| | | 2 | | export type EnvFileEntry = { |
| | | 3 | | readonly key: string; |
| | | 4 | | readonly value: string; |
| | | 5 | | }; |
| | | 6 | | |
| | | 7 | | /** A key assigned more than once in the env file. */ |
| | | 8 | | export type EnvFileDuplicate = { |
| | | 9 | | readonly key: string; |
| | | 10 | | readonly count: number; |
| | | 11 | | }; |
| | | 12 | | |
| | | 13 | | /** |
| | | 14 | | * How the keys of an env file line up with the variables a map file declares: |
| | | 15 | | * |
| | | 16 | | * - `present`: declared in the map file and in the env file. |
| | | 17 | | * - `missing`: declared in the map file, absent from the env file. Fails. |
| | | 18 | | * - `undeclared`: only in the env file. A warning; it never fails. |
| | | 19 | | * - `duplicates`: assigned more than once in the env file. Fails. |
| | | 20 | | * |
| | | 21 | | * `KEY=` counts as present. Keys compare case-sensitively. No value is kept. |
| | | 22 | | */ |
| | | 23 | | export type EnvFileKeyComparison = { |
| | | 24 | | readonly present: readonly string[]; |
| | | 25 | | readonly missing: readonly string[]; |
| | | 26 | | readonly undeclared: readonly string[]; |
| | | 27 | | readonly duplicates: readonly EnvFileDuplicate[]; |
| | | 28 | | }; |
| | | 29 | | |
| | | 30 | | /** |
| | | 31 | | * The outcome of comparing one declared variable's local value with its |
| | | 32 | | * remote value. `unreadable` means the remote value could not be read, with |
| | | 33 | | * the provider's `reason`; it never carries either value. |
| | | 34 | | */ |
| | | 35 | | export type EnvValueStatus = 'matches' | 'differs' | 'unreadable'; |
| | | 36 | | |
| | | 37 | | export type EnvValueCheck = { |
| | | 38 | | readonly variable: string; |
| | | 39 | | readonly path: string; |
| | | 40 | | readonly status: EnvValueStatus; |
| | | 41 | | readonly reason?: string; |
| | | 42 | | }; |
| | | 43 | | |
| | | 44 | | export function compareEnvFileKeys( |
| | | 45 | | declared: readonly string[], |
| | | 46 | | entries: readonly EnvFileEntry[], |
| | | 47 | | ): EnvFileKeyComparison { |
| | 21 | 48 | | const counts = new Map<string, number>(); |
| | 21 | 49 | | for (const { key } of entries) { |
| | 44 | 50 | | counts.set(key, (counts.get(key) ?? 0) + 1); |
| | | 51 | | } |
| | 21 | 52 | | const declaredKeys = new Set(declared); |
| | | 53 | | |
| | 21 | 54 | | return { |
| | 33 | 55 | | present: declared.filter((key) => counts.has(key)), |
| | 33 | 56 | | missing: declared.filter((key) => !counts.has(key)), |
| | 39 | 57 | | undeclared: [...counts.keys()].filter((key) => !declaredKeys.has(key)), |
| | | 58 | | duplicates: [...counts] |
| | 39 | 59 | | .filter(([, count]) => count > 1) |
| | 4 | 60 | | .map(([key, count]) => ({ key, count })), |
| | | 61 | | }; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | /** |
| | | 65 | | * Counts what makes the env file fail, for example `1 missing from .env, 1 |
| | | 66 | | * duplicated in .env`. Empty when only warnings remain. |
| | | 67 | | */ |
| | | 68 | | export function summarizeEnvFileFailures( |
| | | 69 | | comparison: EnvFileKeyComparison, |
| | | 70 | | envFileName: string, |
| | | 71 | | ): string { |
| | 18 | 72 | | const { missing, duplicates } = comparison; |
| | 18 | 73 | | return [ |
| | | 74 | | missing.length > 0 ? `${missing.length} missing from ${envFileName}` : '', |
| | | 75 | | duplicates.length > 0 |
| | | 76 | | ? `${duplicates.length} duplicated in ${envFileName}` |
| | | 77 | | : '', |
| | | 78 | | ] |
| | 36 | 79 | | .filter((part) => part !== '') |
| | | 80 | | .join(', '); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /** |
| | | 84 | | * Counts the values that fail, for example `2 differ, 1 unreadable`. Empty |
| | | 85 | | * when every compared value matches. |
| | | 86 | | */ |
| | | 87 | | export function summarizeValueFailures( |
| | | 88 | | checks: readonly EnvValueCheck[], |
| | | 89 | | ): string { |
| | 28 | 90 | | const differs = checks.filter((check) => check.status === 'differs').length; |
| | 19 | 91 | | const unreadable = checks.filter( |
| | 28 | 92 | | (check) => check.status === 'unreadable', |
| | | 93 | | ).length; |
| | 19 | 94 | | return [ |
| | | 95 | | differs > 0 ? `${differs} ${differs === 1 ? 'differs' : 'differ'}` : '', |
| | | 96 | | unreadable > 0 ? `${unreadable} unreadable` : '', |
| | | 97 | | ] |
| | 38 | 98 | | .filter((part) => part !== '') |
| | | 99 | | .join(', '); |
| | | 100 | | } |