File size: 1,046 Bytes
eb3f11e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { existsSync } from "node:fs";
import fs from "node:fs/promises";
import { createPreUpdateConfigSnapshot } from "../../config/backup-rotation.js";
import { hashConfigRaw } from "../../config/io.read-helpers.js";
import { resolveConfigPath } from "../../config/paths.js";
import { hasNodeErrorCode } from "../../infra/path-guards.js";

export type UpdateConfigSnapshot = {
  path: string;
  raw: string | null;
  hash: string;
  doctorOwned?: boolean;
};

export async function readUpdateConfigSnapshot(path: string): Promise<UpdateConfigSnapshot> {
  const raw = await fs.readFile(path, "utf8").catch((error: unknown) => {
    if (!hasNodeErrorCode(error, "ENOENT")) {
      throw error;
    }
    return null;
  });
  return { path, raw, hash: hashConfigRaw(raw) };
}

export async function createUpdateConfigSnapshot(
  env: NodeJS.ProcessEnv = process.env,
): Promise<void> {
  await createPreUpdateConfigSnapshot({
    configPath: resolveConfigPath(env),
    fs: { writeFile: fs.writeFile, readFile: fs.readFile, existsSync },
  });
}