-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
146 lines (126 loc) · 3.66 KB
/
build.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const { cwd } = require("process");
const ts = require("typescript");
const {
cp,
readdirSync,
exists,
existsSync,
readFileSync,
writeFile,
writeFileSync,
mkdirSync,
realpathSync,
} = require("node:fs");
const nodesSourceDir = cwd() + "/src/nodes";
const nodesTargetDir = cwd() + "/dist/nodes";
const buildTsConfig = JSON.parse(readFileSync("build.tsconfig.json").toString());
function makeDir(dir) {
if (!existsSync(dir)) {
console.log(dir);
const parentDir = realpathSync(`${dir}../`);
console.log(parentDir);
if (!existsSync(parentDir)) {
makeDir(parentDir);
}
mkdirSync(dir);
}
}
/**
* @param node String
* @param sourcePath String
* @param targetPath String
*/
function buildForm(node, sourcePath, targetPath) {
/**
* @type {string[]}
*/
const html = [];
const form = readFileSync(`${sourcePath}/form.html`).toString();
const docs = existsSync(`${sourcePath}/docs.html`) ? readFileSync(`${sourcePath}/docs.html`).toString() : undefined;
const formLines = form.split("\n");
html.push(`<script type="text/html" data-template-name="${node}">`);
formLines.forEach((line) => {
if (line.length > 0) {
html.push(` ${line}`);
}
});
html.push("</script>");
if (docs) {
const docsLines = docs.split("\n");
html.push(`<script type="text/html" data-help-name="${node}">`);
docsLines.forEach((line) => {
if (line.length > 0) {
html.push(` ${line}`);
}
});
html.push("</script>");
}
const initTS = readFileSync(`${sourcePath}/init.ts`).toString();
let initJS = ts
.transpileModule(initTS, buildTsConfig)
.outputText.replace(/export \{(?:[^\}]+)?\};/gim, "")
.replace(/RED\.nodes\.registerType\("([^\"]+)"/i, `RED.nodes.registerType("${node}"`);
const initJSLines = initJS.split("\n");
html.push('<script type="text/javascript">');
initJSLines.forEach((line) => {
if (line.length > 0) {
html.push(` ${line}`);
}
});
html.push("</script>");
html.push("");
mkdirSync(`${targetPath}/`, {
recursive: true,
});
writeFileSync(`${targetPath}/${node}.html`, html.join("\n"));
}
/**
* @param node String
* @param sourcePath String
* @param targetPath String
*/
function copyLocales(node, sourcePath, targetPath) {
if (!existsSync(`${sourcePath}/locales`)) {
return;
}
readdirSync(`${sourcePath}/locales`, {
recursive: false,
})
.filter((language) => {
return language.match(/^[a-z]{2,2}(?:-[A-Z]{2,2})?\.json$/);
})
.forEach((language) => {
const languageCode = language.match(/^([a-z]{2,2}(?:-[A-Z]{2,2})?)\.json$/i)[1];
const content = readFileSync(`${sourcePath}/locales/${language}`).toString();
/**
* @type {string[]}
*/
const json = [];
const contentLines = content.split("\n");
const lastElement = json.push("{");
json.push(` "${node}": ${contentLines[0]}`);
contentLines.splice(1).forEach((line) => {
if (line.length > 0) {
json.push(` ${line}`);
}
});
json.push("}");
mkdirSync(`${targetPath}/locales/${languageCode}`, {
recursive: true,
});
writeFileSync(`${targetPath}/locales/${languageCode}/${node}.json`, json.join("\n"));
});
}
readdirSync(nodesSourceDir, {
recursive: false,
})
.filter((node) => {
const path = `${nodesSourceDir}/${node}`;
return existsSync(`${path}/form.html`) && existsSync(`${path}/${node}.ts`);
})
.forEach((node) => {
const sourcePath = `${nodesSourceDir}/${node}`;
const targetPath = `${nodesTargetDir}/${node}`;
buildForm(node, sourcePath, targetPath);
copyLocales(node, sourcePath, targetPath);
});