-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtempseal-adapter.ts
executable file
·98 lines (92 loc) · 2.27 KB
/
tempseal-adapter.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import * as path from "path";
const Adapter = require("@frctl/fractal").Adapter;
import { ComponentMap } from "@sealcode/tempseal";
import { emitEffects } from "@sealcode/tempseal/compile-pipeline";
import {
combineHtml,
write,
replaceUrlPlaceholders,
downloadFonts,
} from "@sealcode/tempseal/compile-pipeline/operators";
class TempsealAdapter extends Adapter {
constructor(source: any, app: any) {
super({}, source);
this._app = app;
}
async render(
input_file_path: string,
_str: string,
context: any,
_meta: any
) {
if (Array.isArray(context)) {
if (context[context.length - 1] === null) {
context = context.slice(0, context.length - 1);
}
}
for (let module_path of [
input_file_path,
require.resolve("./config.js"),
]) {
if (require.cache[module_path]) delete require.cache[module_path];
}
const { config } = require("./config.js");
try {
const start = Date.now();
if (!input_file_path) {
return "";
}
const component = require(input_file_path).default;
const output_path = path.resolve(__dirname, "public");
let resolve_html_body: Function;
let resolve_write: Function;
let reject: Function;
const promises = [
new Promise((resolve) => {
resolve_html_body = resolve;
}),
new Promise((resolve, _reject) => {
resolve_write = resolve;
reject = _reject;
}),
];
emitEffects(new ComponentMap([component]), config, {
language: "en",
segments: [
{
component_name: component.identifier,
props: context,
},
],
})
.pipe(
replaceUrlPlaceholders("/"),
downloadFonts(output_path),
combineHtml((content) => resolve_html_body(content)),
write(output_path)
)
.subscribe(
(e) => console.log(e.file_name),
(er) =>
reject(
`Error: <code><pre>${
er.formatted ? er.formatted : er
}\n${er.stack?.replace(/</g, "<")}</pre></code>`
),
() => {
console.log("RESOLVING WRITE");
resolve_write();
}
);
const [html_body] = await Promise.all(promises);
console.log(`Done in ${Date.now() - start}ms`);
return html_body;
} catch (e) {
console.error(e);
throw e;
}
}
}
export default {
register: (source: any, app: any) => new TempsealAdapter(source, app),
};