-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
run.ts
56 lines (45 loc) 路 1.15 KB
/
run.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
import lume from "../mod.ts";
import { toFileUrl } from "../deps/path.ts";
import { dim } from "../deps/colors.ts";
import { getConfigFile, isUrl } from "../core/utils.ts";
import type { Site } from "../core.ts";
interface Options {
config?: string;
}
export default function ({ config }: Options, scripts: string[]) {
return run(config, scripts);
}
/** Run one or more custom scripts */
export async function run(
config: string | undefined,
scripts: string[],
) {
const site = await createSite(config);
console.log();
for (const script of scripts) {
const success = await site.run(script);
if (!success) {
addEventListener("unload", () => Deno.exit(1));
break;
}
}
}
/** Create a site instance */
export async function createSite(config?: string): Promise<Site> {
let url: string | undefined;
if (config && isUrl(config)) {
url = config;
} else {
const path = await getConfigFile(config);
if (path) {
url = toFileUrl(path).href;
}
}
if (url) {
console.log(`Loading config file ${dim(url)}`);
console.log();
const mod = await import(url);
return mod.default;
}
return lume();
}