-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-to-file.ts
65 lines (63 loc) · 1.44 KB
/
render-to-file.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 chalk from "chalk";
import { emitEffects } from "./compile-pipeline";
import {
combineHtml,
write,
replaceUrlPlaceholders,
downloadFonts,
} from "./compile-pipeline/operators";
import {
Config,
TempsealDocument,
SideEffects,
ComponentMap,
SideEffect,
} from "./";
export function renderToFile(
components: ComponentMap,
config: Config.Config,
base_url: string,
html_url: string,
public_dir: string,
document: TempsealDocument,
emitted_hashes: Map<string, SideEffect> = new Map()
) {
const start = Date.now();
return new Promise((resolve, reject) => {
emitEffects(components, config, document, emitted_hashes)
.pipe(
replaceUrlPlaceholders(base_url, emitted_hashes, html_url),
downloadFonts(public_dir, base_url),
combineHtml((content, subscriber) => {
const file_effect = new SideEffects.HtmlFile(
html_url,
content
);
file_effect.getHash();
subscriber.next(file_effect);
}),
write(public_dir)
)
.subscribe(
(e) => {
if (e.type == "skipped") {
console.log(chalk.gray(`Skipped ${e.file_name}`));
} else {
console.log(chalk.green(`Wrote ${e.file_name}`));
}
},
(er) =>
reject(
`Error: <code><pre>${
er.formatted ? er.formatted : er
}\n${er.stack?.replace(/</g, "<")}</pre></code>`
),
() => {
console.log(
`Rendered ${html_url} in ${Date.now() - start}ms`
);
resolve();
}
);
});
}