-
Notifications
You must be signed in to change notification settings - Fork 6
/
init.ts
71 lines (67 loc) · 2.38 KB
/
init.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
import { ensureDir } from "./deps.ts";
import { basename, dirname, join } from "./deps.ts";
import { stringify } from "./deps.ts";
import { writableStreamFromWriter } from "./deps.ts";
import { TerConfig } from "./config.ts";
const MOD_URL = new URL("https://deno.land/x/ter");
const requiredViews = ["base.eta", "feed.xml.eta"];
const requiredAssets = ["ter.css", "hljs.css"];
async function initializeFile(filePath: string, url: URL) {
const fileResponse = await fetch(url.toString()).catch((err) => {
console.log(`Can't fetch file: ${url}, Error: ${err}`);
Deno.exit(1);
});
if (fileResponse.ok && fileResponse.body) {
await ensureDir(dirname(filePath));
const file = await Deno.open(filePath, {
write: true,
create: true,
});
const writableStream = writableStreamFromWriter(file);
await fileResponse.body.pipeTo(writableStream);
} else {
console.error(`Fetch response error`);
Deno.exit(1);
}
}
export async function init(config: TerConfig) {
console.log("%cInitializing site config:", "font-weight: bold");
try {
await Deno.stat(join(Deno.cwd(), config.siteConfigPath));
console.log(` File exists, skipping:\t${config.siteConfigPath}`);
} catch {
const yaml = stringify(
config.site as unknown as Record<string, unknown>,
);
await ensureDir(dirname(config.siteConfigPath));
await Deno.writeTextFile(config.siteConfigPath, yaml);
console.log(` ${config.siteConfigPath}`);
}
console.log("%cInitializing views and assets:", "font-weight: bold");
for await (const view of requiredViews) {
const viewPath = join(config.viewsPath, view);
try {
await Deno.stat(viewPath);
console.log(` File exists, skipping:\t${viewPath}`);
} catch {
const url = new URL(
join(MOD_URL.href, basename(config.viewsPath), view),
);
await initializeFile(join(config.viewsPath, view), url);
console.log(` Initialized:\t${viewPath}`);
}
}
for await (const asset of requiredAssets) {
const assetPath = join(config.assetsPath, asset);
try {
await Deno.stat(assetPath);
console.log(" File exists, skipping:\t", assetPath);
} catch {
const url = new URL(
join(MOD_URL.href, basename(config.assetsPath), asset),
);
await initializeFile(join(config.assetsPath, asset), url);
console.log(` Initialized:\t${assetPath}`);
}
}
}