-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
202 lines (177 loc) · 6.19 KB
/
build.js
File metadata and controls
202 lines (177 loc) · 6.19 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const { cwd } = require("node:process");
const ts = require("typescript");
const {
readdirSync,
existsSync,
readFileSync,
writeFileSync,
mkdirSync,
realpathSync,
copyFileSync,
} = require("node:fs");
// directory containing the sources for nodes; defaults to `src/nodes` within the current working directory
const nodesSourceDir = cwd() + "/src/nodes";
// directory the generated files should be saved to; defaults to `dist/nodes` within the current working directory
const nodesTargetDir = cwd() + "/dist/nodes";
// path to the typescript project configuration for building the nodes
const buildTsConfig = JSON.parse(readFileSync("build.tsconfig.json").toString());
/**
* Creates a directory if it does not exist
* @param {string} dir directory path to create
*/
function makeDir(dir) {
// bypass everything in case the directory already exists
if (!existsSync(dir)) {
const parentDir = realpathSync(`${dir}../`);
// check whether parent directory exists and create it recursively if not
if (!existsSync(parentDir)) {
makeDir(parentDir);
}
// finally create the directory
mkdirSync(dir);
}
}
/**
* Builds the form for editing a node using the Node-RED Editor.
*
* @param {string} node name of the node the form belongs to
* @param {string} sourcePath path of the directory containing the node's source files
* @param {string} targetPath path of the directory the node's generated files should be saved to
*/
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;
// parse `form.html` and wrap it to the corresponding script-tag
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>");
// parse `docs.html` and wrap it to the corresponding script-tag
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>");
}
// read and transpile the node's TypeScript
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}"`);
// inject the node's init logic into the node's HTML
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("");
// create node's target directory
mkdirSync(`${targetPath}/`, {
recursive: true,
});
// save the node's editor formular
writeFileSync(`${targetPath}/${node}.html`, html.join("\n"));
}
/**
* Copys the locale files of a node.
*
* @param {string} node name of the node the form belongs to
* @param {string} sourcePath path of the directory containing the node's source files
* @param {string} targetPath path of the directory the node's generated files should be saved to
*/
function copyLocales(node, sourcePath, targetPath) {
// skip in case directory `locales` does not exist within the nodes directory
if (!existsSync(`${sourcePath}/locales`)) {
return;
}
readdirSync(`${sourcePath}/locales`, {
recursive: false,
})
.filter((language) => {
// filter for files named as valid language codes
return language.match(/^[a-z]{2,2}(?:-[A-Z]{2,2})?\.json$/);
})
.forEach((language) => {
// read the language code and the JSON
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 = ["{"];
// wrap locales content within a tag names like the node
const contentLines = content.split("\n");
json.push(` "${node}": ${contentLines[0]}`);
contentLines.splice(1).forEach((line) => {
if (line.length > 0) {
json.push(` ${line}`);
}
});
json.push("}");
// create node locales target directory
mkdirSync(`${targetPath}/locales/${languageCode}`, {
recursive: true,
});
// save the node's locales
writeFileSync(`${targetPath}/locales/${languageCode}/${node}.json`, json.join("\n"));
});
}
/**
* Copy icons of a node.
*
@param {string} node name of the node the form belongs to
@param {string} sourcePath path of the directory containing the node's source files
@param {string} targetPath path of the directory the node's generated files should be saved to
*/
function copyIcons(node, sourcePath, targetPath) {
// skip in case directory `icons` does not exist within the nodes directory
if (!existsSync(`${sourcePath}/icons`)) {
return;
}
// create node icons target directory
mkdirSync(`${targetPath}/icons`, {
recursive: true,
});
// copy the node's icons
readdirSync(`${sourcePath}/icons`, {
recursive: false,
}).forEach((icon) => {
copyFileSync(`${sourcePath}/icons/${icon}`, `${targetPath}/icons/${icon}`);
});
}
/*
* Iterate over every directory
*/
readdirSync(nodesSourceDir, {
recursive: false,
})
.filter((node) => {
const path = `${nodesSourceDir}/${node}`;
// filter for directories containing at least a `form.html` and the nodes logic script `node.ts`
return existsSync(`${path}/form.html`) && existsSync(`${path}/${node}.ts`);
})
.forEach((node) => {
const sourcePath = `${nodesSourceDir}/${node}`;
const targetPath = `${nodesTargetDir}/${node}`;
// build the nodes' form for Node-RED Editor
buildForm(node, sourcePath, targetPath);
// copy/transfer optional assets
copyLocales(node, sourcePath, targetPath);
copyIcons(node, sourcePath, targetPath);
});