-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
56 lines (51 loc) · 1.57 KB
/
builder.js
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
const Koa = require("koa");
const bodyParser = require("koa-bodyparser");
const ejs = require("ejs");
const fs = require("fs");
const execa = require("execa");
const { preRender } = require("./preRender");
const app = new Koa();
const buildApp = async (widgets) => {
const template = fs.readFileSync("./builderApp.ejs", {
encoding: "utf-8",
});
const getWidgetNames = (widgets, defaultNames = []) => {
return widgets.reduce((names, widget) => {
if (names.includes(widget.name) && widget.name !== "wLayout")
return names;
if (widget.name === "wLayout") {
if (!names.includes(widget.name)) names.push(widget.name);
const { children } = widget;
names = getWidgetNames(children, names);
} else {
names.push(widget.name);
}
return names;
}, defaultNames);
};
const widgetNames = getWidgetNames(widgets, []);
const code = ejs.render(template, { widgetNames, widgets });
fs.writeFileSync("./src/builderApp.tsx", code);
const commands = ["run", "build"];
const subprocess = execa("npm", commands);
subprocess.stdout.pipe(process.stdout);
const { stdout } = await subprocess;
console.log(stdout);
};
app.use(bodyParser());
app.use(async (ctx) => {
const { widgets, usePreRender } = ctx.request.body;
try {
await buildApp(widgets);
if (usePreRender) {
console.log("----preRenderBegin----");
await preRender();
console.log("----preRenderDone----");
}
ctx.body = "ok hahaha";
} catch (error) {
console.log(error);
ctx.body = "error";
}
});
app.listen(9001);