-
Notifications
You must be signed in to change notification settings - Fork 616
/
dev_command.ts
78 lines (69 loc) · 2.45 KB
/
dev_command.ts
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
68
69
70
71
72
73
74
75
76
77
78
import { updateCheck } from "./update_check.ts";
import { DAY, dirname, fromFileUrl, join, toFileUrl } from "./deps.ts";
import { FreshConfig, Manifest as ServerManifest } from "../server/mod.ts";
import { build } from "./build.ts";
import { collect, ensureMinDenoVersion, generate, Manifest } from "./mod.ts";
import { startServer } from "../server/boot.ts";
import { getInternalFreshState } from "../server/config.ts";
import { getServerContext } from "../server/context.ts";
export async function dev(
base: string,
entrypoint: string,
config?: FreshConfig,
) {
ensureMinDenoVersion();
// Run update check in background
updateCheck(DAY).catch(() => {});
const dir = dirname(fromFileUrl(base));
let currentManifest: Manifest;
const prevManifest = Deno.env.get("FRSH_DEV_PREVIOUS_MANIFEST");
if (prevManifest) {
currentManifest = JSON.parse(prevManifest);
} else {
currentManifest = { islands: [], routes: [] };
}
const newManifest = await collect(dir, config?.router?.ignoreFilePattern);
Deno.env.set("FRSH_DEV_PREVIOUS_MANIFEST", JSON.stringify(newManifest));
const manifestChanged =
!arraysEqual(newManifest.routes, currentManifest.routes) ||
!arraysEqual(newManifest.islands, currentManifest.islands);
if (manifestChanged) await generate(dir, newManifest);
const manifest = (await import(toFileUrl(join(dir, "fresh.gen.ts")).href))
.default as ServerManifest;
if (Deno.args.includes("build")) {
const state = await getInternalFreshState(
manifest,
config ?? {},
);
state.config.dev = false;
state.loadSnapshot = false;
state.build = true;
await build(state);
} else if (config) {
const state = await getInternalFreshState(
manifest,
config,
);
state.config.dev = true;
state.loadSnapshot = false;
const ctx = await getServerContext(state);
await startServer(ctx.handler(), {
...state.config.server,
basePath: state.config.basePath,
});
} else {
// Legacy entry point: Back then `dev.ts` would call `main.ts` but
// this causes duplicate plugin instantiation if both `dev.ts` and
// `main.ts` instantiate plugins.
Deno.env.set("__FRSH_LEGACY_DEV", "true");
entrypoint = new URL(entrypoint, base).href;
await import(entrypoint);
}
}
function arraysEqual<T>(a: T[], b: T[]): boolean {
if (a.length !== b.length) return false;
for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}