-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.utils.ts
65 lines (52 loc) · 1.32 KB
/
render.utils.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
import { render } from "https://deno.land/x/mustache/mod.ts";
import { dasherize } from "./string.utils.ts";
import { TemplateHelpers } from "./template.helpers.ts";
const encoder = new TextEncoder();
/**
* Fetch and generate from url
*/
export async function generateFromUrl(
downloadUrl: string,
outputFileName: string,
name: string,
) {
try {
const body = await fetch(downloadUrl).then((r) => r.text());
const fileOutputName = outputFileName || `${dasherize(name)}.ts`;
await renderAndWrite(fileOutputName, body, name);
} catch (e) {
throw e;
}
}
/**
* Render and write file
* default renderer Mustach
*/
export async function renderAndWrite(
fileOutputName: string,
body: string,
name: string,
) {
const fileContent = await render(body, {
...TemplateHelpers,
name,
});
const path = dasherize(name) + fileOutputName;
await createDirs(path);
const f = await Deno.create(path);
await f.write(encoder.encode(fileContent));
await f.close();
console.log(path);
}
const createdDirs = new Set();
async function createDirs(path: string) {
const dirs = path.split("/");
let needDir = "";
for (let i = 0; i < dirs.length - 1; i++) {
needDir += dirs[i] + "/";
if (!createdDirs.has(needDir)) {
await Deno.mkdir(needDir);
}
createdDirs.add(needDir);
}
}