Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import { dirname, join } from "node:path";
import { out } from "../shared/adapters/io.ts";
import type { Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { examplePaths, readOptional, readText, writeEnv } from "./adapters/files.ts";
import { parseEnv, render } from "./domain/envFile.ts";
import { describeSync, lostData, syncEnv } from "./domain/sync.ts";
type Outcome = "unchanged" | "stale" | "written" | "refused";
const syncOne = (example: string, check: boolean): Outcome => {
const env = join(dirname(example), ".env");
const refuse = (why: string): Outcome => {
out.error(`${env} left untouched: ${why}.`);
return "refused";
};
const parsedExample = parseEnv(readText(example));
if (parsedExample.problem !== undefined) {
return refuse(`${example} can't be read, ${parsedExample.problem}`);
}
const before = readOptional(env);
const parsedEnv = before === null ? { lines: [] } : parseEnv(before);
if (parsedEnv.problem !== undefined) return refuse(`it can't be read, ${parsedEnv.problem}`);
const synced = syncEnv(parsedExample.lines, parsedEnv.lines);
const after = render(synced.lines);
if (after === before) return "unchanged";
const lost = lostData(parsedEnv.lines, parsedExample.lines, after);
if (lost.length > 0) return refuse(`syncing would lose data (${lost.join("; ")})`);
const summary = before === null ? `created from ${example}` : describeSync(synced);
if (check) {
out.warn(`${env} is out of sync: ${summary}.`);
return "stale";
}
const backup = writeEnv(env, after);
const backupNote = backup ? ` Backup at ${backup}.` : "";
out.success(`${env}: ${summary}.${backupNote}`);
return "written";
};
export const main = ({ flags }: Args): void => {
const check = flags.has("check");
const examples = examplePaths();
const outcomes = examples.map((example) => syncOne(example, check));
const refused = outcomes.filter((outcome) => outcome === "refused").length;
if (refused > 0) {
throw new ExpectedError(
`${refused} env file${refused === 1 ? "" : "s"} left untouched — see above.`,
);
}
if (outcomes.includes("stale")) {
throw new ExpectedError("Run `pnpm env:sync` to bring them in line.");
}
if (!outcomes.includes("written")) {
out.success(`${examples.length} .env files match their .env.example.`);
}
};
|