< Summary - Envilder CLI

Information
Class: src/envilder/core/domain/EnvFileComparison.ts
Assembly: Default
File(s): src/envilder/core/domain/EnvFileComparison.ts
Tag: 605_44106794472
Line coverage
100%
Covered lines: 18
Uncovered lines: 0
Coverable lines: 18
Total lines: 100
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

File(s)

src/envilder/core/domain/EnvFileComparison.ts

#LineLine coverage
 1/** One `KEY=value` assignment of an env file, in file order. */
 2export type EnvFileEntry = {
 3  readonly key: string;
 4  readonly value: string;
 5};
 6
 7/** A key assigned more than once in the env file. */
 8export 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 */
 23export 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 */
 35export type EnvValueStatus = 'matches' | 'differs' | 'unreadable';
 36
 37export type EnvValueCheck = {
 38  readonly variable: string;
 39  readonly path: string;
 40  readonly status: EnvValueStatus;
 41  readonly reason?: string;
 42};
 43
 44export function compareEnvFileKeys(
 45  declared: readonly string[],
 46  entries: readonly EnvFileEntry[],
 47): EnvFileKeyComparison {
 2148  const counts = new Map<string, number>();
 2149  for (const { key } of entries) {
 4450    counts.set(key, (counts.get(key) ?? 0) + 1);
 51  }
 2152  const declaredKeys = new Set(declared);
 53
 2154  return {
 3355    present: declared.filter((key) => counts.has(key)),
 3356    missing: declared.filter((key) => !counts.has(key)),
 3957    undeclared: [...counts.keys()].filter((key) => !declaredKeys.has(key)),
 58    duplicates: [...counts]
 3959      .filter(([, count]) => count > 1)
 460      .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 */
 68export function summarizeEnvFileFailures(
 69  comparison: EnvFileKeyComparison,
 70  envFileName: string,
 71): string {
 1872  const { missing, duplicates } = comparison;
 1873  return [
 74    missing.length > 0 ? `${missing.length} missing from ${envFileName}` : '',
 75    duplicates.length > 0
 76      ? `${duplicates.length} duplicated in ${envFileName}`
 77      : '',
 78  ]
 3679    .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 */
 87export function summarizeValueFailures(
 88  checks: readonly EnvValueCheck[],
 89): string {
 2890  const differs = checks.filter((check) => check.status === 'differs').length;
 1991  const unreadable = checks.filter(
 2892    (check) => check.status === 'unreadable',
 93  ).length;
 1994  return [
 95    differs > 0 ? `${differs} ${differs === 1 ? 'differs' : 'differ'}` : '',
 96    unreadable > 0 ? `${unreadable} unreadable` : '',
 97  ]
 3898    .filter((part) => part !== '')
 99    .join(', ');
 100}