Skip to content

Commit 9cc2907

Browse files
committed
better dictionary build error handling
1 parent afee1fe commit 9cc2907

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

build/dev.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ async function watchDictionary(): Promise<number> {
5151
}
5252
if (import.meta.main) {
5353
if (!await exists(DICTIONARY)) {
54-
await Deno.create(DICTIONARY);
54+
const Dictionary = await import("../dictionary/build.ts");
55+
if (!await Dictionary.build()) {
56+
await Dictionary.buildWithDictionary(new Map());
57+
// deno-lint-ignore no-console
58+
console.error(
59+
"Dictionary failed to build. Empty dictionary is used instead. Please fix it.",
60+
);
61+
}
5562
}
5663
const statusCodePromise = watchDictionary();
5764
await using _ = await watchMain();

dictionary/build.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,23 @@ import { Dictionary } from "./type.ts";
1010
const SOURCE = new URL("./dictionary", import.meta.url);
1111
const DESTINATION = new URL("./dictionary.ts", import.meta.url);
1212

13+
export async function buildWithDictionary(
14+
dictionary: Dictionary,
15+
): Promise<void> {
16+
const json = JSON.stringify(
17+
Object.fromEntries(dictionary),
18+
undefined,
19+
2,
20+
);
21+
const code = `\
22+
// This code is autogenerated
23+
24+
import { Dictionary } from "./type.ts";
25+
26+
export const dictionary: Dictionary = new Map(Object.entries(${json}));
27+
`;
28+
await Deno.writeTextFile(DESTINATION, code);
29+
}
1330
export async function build(): Promise<boolean> {
1431
console.log("Building dictionary...");
1532
const start = performance.now();
@@ -24,19 +41,7 @@ export async function build(): Promise<boolean> {
2441
displayError(text, result.errors);
2542
return false;
2643
}
27-
const json = JSON.stringify(
28-
Object.fromEntries(dictionary),
29-
undefined,
30-
2,
31-
);
32-
const code = `\
33-
// This code is autogenerated
34-
35-
import { Dictionary } from "./type.ts";
36-
37-
export const dictionary: Dictionary = new Map(Object.entries(${json}));
38-
`;
39-
await Deno.writeTextFile(DESTINATION, code);
44+
await buildWithDictionary(dictionary);
4045
const end = performance.now();
4146
const total = Math.floor(end - start);
4247
const parsing = Math.floor(endDictionary - startDictionary);

0 commit comments

Comments
 (0)