forked from deco-cx/deco
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev.ts
executable file
·213 lines (185 loc) · 5.43 KB
/
dev.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import os from "https://deno.land/x/[email protected]/mod.ts";
import { setupGithooks } from "https://deno.land/x/[email protected]/githooks.ts";
import { dirname, fromFileUrl, join } from "std/path/mod.ts";
import { gte } from "std/semver/mod.ts";
import { ResolverMap } from "$live/engine/core/resolver.ts";
import {
ManifestBuilder,
newManifestBuilder,
} from "$live/engine/fresh/manifestBuilder.ts";
import { decoManifestBuilder } from "$live/engine/fresh/manifestGen.ts";
import { genSchemasFromManifest } from "$live/engine/schema/gen.ts";
import { context } from "$live/live.ts";
import { DecoManifest } from "$live/types.ts";
import { exists } from "$live/utils/filesystem.ts";
import { namespaceFromImportMap } from "$live/utils/namespace.ts";
import { SiteInfo } from "./types.ts";
const MIN_DENO_VERSION = "1.25.0";
export function ensureMinDenoVersion() {
// Check that the minimum supported Deno version is being used.
if (!gte(Deno.version.deno, MIN_DENO_VERSION)) {
let message =
`Deno version ${MIN_DENO_VERSION} or higher is required. Please update Deno.\n\n`;
if (Deno.execPath().includes("homebrew")) {
message +=
"You seem to have installed Deno via homebrew. To update, run: `brew upgrade deno`\n";
} else {
message += "To update, run: `deno upgrade`\n";
}
console.error(message);
}
}
const genSchemas = async (
base: string,
manifest: string,
directory: string,
) => {
manifest = new URL(manifest, base).href;
await Deno.writeTextFile(
join(directory, "schemas.gen.json"),
JSON.stringify(
await genSchemasFromManifest(
await import(manifest).then((mod) => mod.default),
),
null,
2,
),
);
};
const manifestFile = "./live.gen.ts";
export async function generate(
directory: string,
manifest: ManifestBuilder,
) {
const proc = Deno.run({
cmd: [Deno.execPath(), "fmt", "-"],
stdin: "piped",
stdout: "piped",
stderr: "null",
});
const raw = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(manifest.build()));
controller.close();
},
});
await raw.pipeTo(proc.stdin.writable);
const out = await proc.output();
await proc.status();
proc.close();
const manifestStr = new TextDecoder().decode(out);
const manifestPath = join(directory, manifestFile);
await Deno.writeTextFile(manifestPath, manifestStr);
console.log(
`%cThe manifest has been generated.`,
"color: blue; font-weight: bold",
);
}
export const siteJSON = "site.json";
const getAndUpdateNamespace = async (
dir: string,
): Promise<string | undefined> => {
const ns = await namespaceFromImportMap(dir);
if (!ns) {
return undefined;
}
const siteJSONPath = join(dir, siteJSON);
let siteInfo: SiteInfo | null = null;
if (await exists(siteJSONPath)) {
siteInfo = await Deno.readTextFile(siteJSONPath).then(
JSON.parse,
);
} else {
siteInfo = {
namespace: ns,
};
}
if (siteInfo?.namespace !== ns) {
await Deno.writeTextFile(
siteJSONPath,
JSON.stringify({ ...siteInfo, namespace: ns }, null, 2),
);
}
return ns;
};
export default async function dev(
base: string,
entrypoint: string,
{
imports = [],
onListen,
}: {
imports?:
| Array<
DecoManifest | (DecoManifest & Partial<Record<string, ResolverMap>>)
>
| Record<
string,
DecoManifest | (DecoManifest & Partial<Record<string, ResolverMap>>)
>;
onListen?: () => void;
} = {},
) {
const dir = dirname(fromFileUrl(base));
const ns = await getAndUpdateNamespace(dir) ?? base;
context.namespace = ns;
ensureMinDenoVersion();
entrypoint = new URL(entrypoint, base).href;
let currentManifest: ManifestBuilder;
const prevManifest = Deno.env.get("LIVE_DEV_PREVIOUS_MANIFEST");
if (prevManifest) {
currentManifest = newManifestBuilder(JSON.parse(prevManifest));
} else {
currentManifest = newManifestBuilder({
namespace: ns,
imports: {},
manifest: {},
exports: [],
});
}
let manifest = await decoManifestBuilder(dir, ns);
manifest = manifest.mergeWith(
typeof imports === "object" ? Object.values(imports) : imports,
);
Deno.env.set("LIVE_DEV_PREVIOUS_MANIFEST", manifest.toJSONString());
const manifestChanged = !currentManifest.equal(manifest);
if (manifestChanged) {
await generate(dir, manifest);
}
genSchemas(base, manifestFile, dir);
const shouldSetupGithooks = os.platform() !== "windows";
if (shouldSetupGithooks) {
await setupGithooks();
}
onListen?.();
await import(entrypoint);
}
export async function format(content: string) {
const proc = Deno.run({
cmd: [Deno.execPath(), "fmt", "-"],
stdin: "piped",
stdout: "piped",
stderr: "null",
});
const raw = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(content));
controller.close();
},
});
await raw.pipeTo(proc.stdin.writable);
const out = await proc.output();
await proc.status();
proc.close();
return new TextDecoder().decode(out);
}
// Generate live own manifest data so that other sites can import native functions and sections.
export const liveNs = "$live";
if (import.meta.main) {
context.namespace = liveNs;
const dir = Deno.cwd();
const newManifestData = await decoManifestBuilder(dir, liveNs);
await generate(dir, newManifestData).then(() =>
genSchemas(import.meta.url, manifestFile, dir)
);
}