chore: extract fs extractor code (#1972)

This commit is contained in:
Marvin Hagemeister
2023-10-23 12:41:45 +02:00
committed by GitHub
parent cd4a3344a9
commit 20fdd7e16c
8 changed files with 681 additions and 702 deletions
+41 -1
View File
@@ -1,4 +1,7 @@
import type { BuildSnapshot } from "./mod.ts";
import { ResolvedFreshConfig } from "../server/types.ts";
import { colors, join } from "../server/deps.ts";
import type { BuildSnapshot, BuildSnapshotJson } from "./mod.ts";
import { setBuildId } from "../server/build_id.ts";
export class AotSnapshot implements BuildSnapshot {
#files: Map<string, string>;
@@ -35,3 +38,40 @@ export class AotSnapshot implements BuildSnapshot {
return this.#dependencies.get(path) ?? [];
}
}
export async function loadAotSnapshot(
config: ResolvedFreshConfig,
): Promise<AotSnapshot | null> {
const snapshotDirPath = config.build.outDir;
try {
if ((await Deno.stat(snapshotDirPath)).isDirectory) {
console.log(
`Using snapshot found at ${colors.cyan(snapshotDirPath)}`,
);
const snapshotPath = join(snapshotDirPath, "snapshot.json");
const json = JSON.parse(
await Deno.readTextFile(snapshotPath),
) as BuildSnapshotJson;
setBuildId(json.build_id);
const dependencies = new Map<string, string[]>(
Object.entries(json.files),
);
const files = new Map<string, string>();
Object.keys(json.files).forEach((name) => {
const filePath = join(snapshotDirPath, name);
files.set(name, filePath);
});
return new AotSnapshot(files, dependencies);
}
return null;
} catch (err) {
if (!(err instanceof Deno.errors.NotFound)) {
throw err;
}
return null;
}
}