-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
567 lines (529 loc) · 21.2 KB
/
index.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
import fs, {
existsSync,
readFileSync,
writeFileSync,
copyFileSync,
readdirSync,
statSync,
rmdirSync,
unlinkSync,
mkdirSync,
} from 'node:fs';
import { globSync } from 'glob';
import { type ChildProcess, exec, fork, type IOType, type CommonSpawnOptions, execFile } from 'node:child_process';
import { dirname, join } from 'node:path';
/**
* Delete all folders recursive (sync function)
*/
export function deleteFoldersRecursive(
/** Path to the folder */
path: string,
/** List of exceptions */
exceptions?: string[],
): void {
if (existsSync(path)) {
const files = readdirSync(path);
for (const file of files) {
const curPath = join(path, file);
if (exceptions?.find(e => curPath.endsWith(e))) {
continue;
}
const stat = statSync(curPath);
if (stat.isDirectory()) {
deleteFoldersRecursive(curPath);
try {
rmdirSync(curPath);
} catch (e) {
console.warn(`[${new Date().toISOString()}] Cannot delete "${curPath}: ${e}`);
}
} else {
try {
unlinkSync(curPath);
} catch (e) {
console.warn(`[${new Date().toISOString()}] Cannot delete "${curPath}: ${e}`);
}
}
}
}
}
/**
* Copies folder recursive (sync function)
*/
export function copyFolderRecursiveSync(
/** Source folder */
src: string,
/** Destination folder */
dest: string,
/** List of files to exclude */
exclude?: string[],
): void {
const stats = existsSync(src) ? statSync(src) : null;
if (stats?.isDirectory()) {
!fs.existsSync(dest) && fs.mkdirSync(dest);
fs.readdirSync(src).forEach(childItemName => {
copyFolderRecursiveSync(join(src, childItemName), join(dest, childItemName));
});
} else if (!exclude || !exclude.find(ext => src.endsWith(ext))) {
copyFileSync(src, dest);
}
}
// Read all files in directory and subdirectories as one list (sync function)
export function readDirRecursive(path: string, _list?: string[]): string[] {
_list = _list || [];
if (existsSync(path)) {
const files = readdirSync(path);
files.forEach((file: string) => {
const fullPath = join(path, file).replace(/\\/g, '/');
if (statSync(fullPath).isDirectory()) {
readDirRecursive(fullPath, _list);
} else {
_list.push(fullPath);
}
});
}
return _list;
}
// Collect files by mask (sync function)
export function collectFiles(patterns: string[] | string): { name: string; base: string }[] {
const _patterns = typeof patterns === 'string' ? [patterns] : patterns;
const result = [];
for (let i = 0; i < _patterns.length; i++) {
let add = true;
if (_patterns[i].startsWith('!')) {
_patterns[i] = _patterns[i].substring(1);
add = false;
}
_patterns[i] = _patterns[i].replace(/\\/g, '/');
let folder = _patterns[i].split('*')[0];
if (folder[folder.length - 1] === '/') {
folder = folder.substring(0, folder.length - 1);
} else {
const folderParts = folder.split('/');
folderParts.pop();
folder = folderParts.join('/');
}
const files: string[] = globSync(_patterns[i]);
for (let f = 0; f < files.length; f++) {
files[f] = files[f].replace(/\\/g, '/');
if (folder && files[f].startsWith(folder)) {
files[f] = files[f].substring(folder.length + 1);
}
if (add) {
// ignore folders
const isDirectory = folder
? statSync(join(folder, files[f])).isDirectory()
: statSync(files[f]).isDirectory();
if (isDirectory) {
continue;
}
result.push({ name: files[f], base: folder });
} else {
const pos = result.findIndex(it => it.name === files[f]);
if (pos !== -1) {
result.splice(pos, 1);
}
}
}
}
return result;
}
// Copy files by pattern to destination (sync function)
export function copyFiles(
patterns: string[] | string,
dest: string,
options?: {
process?: (fileData: Buffer | string, fileName: string) => string | null | undefined | false;
replace?: { find: string | RegExp; text: string }[];
},
): void {
const files = collectFiles(patterns);
for (let f = 0; f < files.length; f++) {
const destName = join(dest, files[f].name);
const folder = dirname(destName);
if (!existsSync(folder)) {
mkdirSync(folder, { recursive: true });
}
console.log(`[${new Date().toISOString()}] Copy "${files[f].base}/${files[f].name}" to "${destName}"`);
if (options) {
let data: Buffer | string = readFileSync(
files[f].base ? `${files[f].base}/${files[f].name}` : files[f].name,
);
if (options.replace) {
data = data.toString('utf8');
for (let r = 0; r < options.replace.length; r++) {
data = data.replace(options.replace[r].find, options.replace[r].text);
}
}
if (options.process) {
const newData = options.process(
data,
files[f].base ? `${files[f].base}/${files[f].name}` : files[f].name,
);
// if null, skip this fila
if (newData === null || newData === false) {
continue;
} else if (newData !== undefined) {
data = newData;
}
}
writeFileSync(destName, data);
} else {
copyFileSync(files[f].base ? `${files[f].base}/${files[f].name}` : files[f].name, destName);
}
}
}
// run npm install in directory (async function)
export function npmInstall(
/** Path to the folder where npm must be executed */
src: string,
/** Options */
options?: {
/** Set to false if you want to execute without `--force` flag */
force?: boolean;
/** Execute npm install with "ci" */
clean?: boolean;
omitDev?: boolean;
},
): Promise<void> {
if (src.endsWith('/')) {
src = src.substring(0, src.length - 1);
}
return new Promise((resolve, reject) => {
// Install node modules
const cwd = src.replace(/\\/g, '/');
const start = Date.now();
const cmd = `npm ${options?.clean ? 'ci' : 'install'}${options?.force !== false ? ' --force' : ''}${options?.omitDev ? ' --omit=dev' : ''}`;
// System call used for update of js-controller itself,
// because during an installation the npm packet will be deleted too, but some files must be loaded even during the install process.
console.log(`[${new Date().toISOString()}] executing: "${cmd}" in "${cwd}"`);
const child = exec(cmd, { cwd });
child?.stderr?.pipe(process.stderr);
child?.stdout?.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is a strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(new Error(`Cannot install: ${code}`));
} else {
console.log(`[${new Date().toISOString()}] "${cmd}" in "${cwd}" finished in ${Date.now() - start}ms.`);
// command succeeded
resolve();
}
});
});
}
export function tsc(
/** React directory to build */
src: string,
options?: {
/** Root directory to copy the version from */
rootDir?: string;
},
): Promise<void> {
if (src.endsWith('/')) {
src = src.substring(0, src.length - 1);
}
let rootDir: string | undefined;
if (options?.rootDir) {
rootDir = options.rootDir;
if (rootDir.endsWith('/')) {
rootDir = rootDir.substring(0, options.rootDir.length - 1);
}
}
return new Promise((resolve, reject) => {
const cpOptions: CommonSpawnOptions = {
stdio: 'pipe' as IOType,
cwd: src,
};
const start = Date.now();
let script;
script = `${src}/node_modules/typescript/bin/tsc`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/typescript/bin/tsc`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/typescript/bin/tsc`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/typescript/bin/tsc`;
}
}
}
if (!existsSync(script)) {
console.error(`[${new Date().toISOString()}] Cannot find execution file: ${script}`);
reject(new Error(`Cannot find execution file: ${script}`));
} else {
const child: ChildProcess = fork(script, [], cpOptions);
child?.stdout?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child?.stderr?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child.on('close', code => {
console.log(
`[${new Date().toISOString()}] child process exited with code ${code} after ${Date.now() - start}ms.`,
);
code ? reject(new Error(`Exit code: ${code}`)) : resolve();
});
}
});
}
export function buildReact(
/** React directory to build */
src: string,
/** Options */
options?: {
/** use craco instead of react-scripts */
craco?: boolean;
/** Root directory to copy the version from */
rootDir?: string;
/** Use exec and not fork */
exec?: boolean;
/** Max memory size for exec */
ramSize?: number;
/** Use vite for build */
vite?: boolean;
/** execute tsc before building ReactJS */
tsc?: boolean;
/** ignore return code 1 as error */
ignoreCode1?: boolean;
/** Normally process.exit will be called. With this flag only promise will be rejected */
ignoreErrors?: boolean;
},
): Promise<void> {
if (src.endsWith('/')) {
src = src.substring(0, src.length - 1);
}
let rootDir: string | undefined;
const start = Date.now();
// Copy version number from root directory to src directory
if (options?.rootDir) {
rootDir = options.rootDir;
if (rootDir.endsWith('/')) {
rootDir = rootDir.substring(0, options.rootDir.length - 1);
}
const version = JSON.parse(readFileSync(`${rootDir}/package.json`).toString('utf8')).version;
const data = JSON.parse(readFileSync(`${src}/package.json`).toString('utf8'));
if (data.version !== version) {
console.log(`[${new Date().toISOString()}] updated version in "${src}/package.json to "${version}"`);
data.version = version;
writeFileSync(`${src}/package.json`, JSON.stringify(data, null, 4));
}
}
const reactPromise: Promise<void> = new Promise((resolve, reject) => {
const cpOptions: CommonSpawnOptions = {
stdio: 'pipe' as IOType,
cwd: src,
};
// cpOptions.env = {
// DANGEROUSLY_DISABLE_HOST_CHECK: 'true',
// };
let script;
if (options?.craco) {
script = `${src}/node_modules/@craco/craco/dist/bin/craco.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/@craco/craco/dist/bin/craco.js`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/@craco/craco/dist/bin/craco.js`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/@craco/craco/dist/bin/craco.js`;
}
}
}
} else if (options?.vite) {
script = `${src}/node_modules/vite/bin/vite.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/vite/bin/vite.js`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/vite/bin/vite.js`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/vite/bin/vite.js`;
}
}
}
} else {
script = `${src}/node_modules/react-scripts/scripts/build.js`;
if (rootDir && !existsSync(script)) {
script = `${rootDir}/node_modules/react-scripts/scripts/build.js`;
if (!existsSync(script)) {
// admin could have another structure
script = `${rootDir}/../node_modules/react-scripts/scripts/build.js`;
if (!existsSync(script)) {
script = `${rootDir}/../../node_modules/react-scripts/scripts/build.js`;
}
}
}
}
if (!existsSync(script)) {
console.error(`[${new Date().toISOString()}] Cannot find execution file: ${script}`);
reject(new Error(`Cannot find execution file: ${script}`));
} else {
cpOptions.cwd = src;
let child: ChildProcess;
if (options?.ramSize || options?.exec || options?.craco) {
delete cpOptions.stdio;
const cmd = 'node';
const args = [script, options.ramSize ? `--max-old-space-size=${options.ramSize}` : '', 'build'].filter(
a => a,
);
const child = execFile(cmd, args, cpOptions);
console.log(
`[${new Date().toISOString()}] Execute: "${cmd} ${args.join(' ')}" ${JSON.stringify(cpOptions)}`,
);
child.stderr?.pipe(process.stderr);
child.stdout?.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is a strange error that cannot be explained. Everything is done but error :(
if (code) {
if (code === 1 && options.ignoreCode1) {
console.log(`"${cmd} in ${src} finished.`);
// command succeeded
resolve();
} else {
if (options.ignoreErrors) {
reject(new Error(`Cannot build: ${code}`));
} else {
console.error(`Cannot build: ${code}`);
process.exit(2);
}
}
} else {
console.log(`"${cmd} in ${src} finished.`);
// command succeeded
resolve();
}
});
} else {
console.log(`[${new Date().toISOString()}] fork: "${script} build" ${JSON.stringify(cpOptions)}`);
child = fork(script, ['build'], cpOptions);
child?.stdout?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child?.stderr?.on('data', data => console.log(`[${new Date().toISOString()}] ${data.toString()}`));
child.on('close', code => {
console.log(
`[${new Date().toISOString()}] child process exited with code ${code} after ${Date.now() - start}ms.`,
);
if (code) {
if (code === 1 && options?.ignoreCode1) {
resolve();
} else {
if (options?.ignoreErrors) {
reject(new Error(`Cannot build: ${code}`));
} else {
console.error(`Cannot build: ${code}`);
process.exit(2);
}
}
} else {
// command succeeded
resolve();
}
});
}
}
});
if (options?.tsc) {
return tsc(src, options).then(() => reactPromise);
}
return reactPromise;
}
/** @deprecated use buildReact with the craco flag */
export function buildCraco(
/** React directory to build */
src: string,
/** Options */
options?: {
/** Root directory to copy the version from */
rootDir?: string;
/** Use exec and not fork */
exec?: boolean;
/** Max memory size for exec */
ramSize?: number;
},
): Promise<void> {
console.warn(`[${new Date().toISOString()}] buildCraco deprecated: Please use buildReact with craco option`);
return buildReact(src, { craco: true, ...options });
}
function _patchHtmlFile(fileName: string, rootDir?: string): boolean {
let changed = false;
if (fs.existsSync(fileName)) {
let code = fs.readFileSync(fileName).toString('utf8');
// replace code
if (code.match(/<script>\n?\s*const script\s?=\s?document[^<]+<\/script>/)) {
code = code.replace(
/<script>\n?\s*const script\s?=\s?document[^<]+<\/script>/,
`<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="${rootDir || '.'}/lib/js/socket.io.js"></script>`,
);
changed = true;
}
if (code.match(/<script>\n?\s*var script\s?=\s?document[^<]+<\/script>/)) {
code = code.replace(
/<script>\n?\s*var script\s?=\s?document[^<]+<\/script>/,
`<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="${rootDir || '.'}/lib/js/socket.io.js"></script>`,
);
changed = true;
}
if (changed) {
fs.writeFileSync(fileName, code);
}
}
return changed;
}
export function ignoreWidgetFiles(src: string, doNotIgnoreMap?: boolean): string[] {
src = src || './src-widgets/';
if (!src.endsWith('/')) {
src += '/';
}
let list = [
`!${src}build/static/js/node_modules*.*`,
`!${src}build/static/js/vendors-node_modules*.*`,
`!${src}build/static/js/main*.*`,
`!${src}build/static/js/src_bootstrap*.*`,
];
if (!doNotIgnoreMap) {
list = list.concat([`!${src}build/static/*.map`, `!${src}build/static/**/*.map`]);
}
return list;
}
export function copyWidgetsFiles(src: string): string[] {
src = src || './src-widgets/';
if (!src.endsWith('/')) {
src += '/';
}
return [
`${src}build/static/js/*fast-xml*.*`,
`${src}build/static/js/*react-swipeable*.*`,
`${src}build/static/js/*moment_*.*`,
`${src}build/static/js/*react-beautiful-dnd*.*`,
`${src}build/static/js/*vis-2-widgets-react-dev_index_jsx*.*`,
`${src}build/static/js/*vis-2-widgets-react-dev_node_modules_babel_runtime_helpers*.*`,
`${src}build/static/js/*runtime_helpers_asyncToGenerator*.*`,
`${src}build/static/js/*modules_color*.*`,
`${src}build/static/js/*echarts-for-react_lib_core_js-node_modules_echarts_core_js-*.chunk.*`,
`${src}build/static/js/*echarts_lib*.*`,
`${src}build/static/js/*vis-2-widgets-react-dev_node_modules_babel_runtime_helpers*.*`,
`${src}build/static/js/*leaflet*.*`,
`${src}build/static/js/*react-circular*.*`,
`${src}build/static/js/*d3-array_src_index_js-node_modules_d3-collection_src_index_js-*.*`,
`${src}build/static/js/*d3-dispatch_*.*`,
`${src}build/static/js/*lodash_*.*`,
`${src}build/static/js/*react-battery-gauge_dist_react-battery-gauge*.*`,
`${src}build/static/js/*react-gauge-chart*.*`,
`${src}build/static/js/*react-liquid-gauge*.*`,
`${src}build/static/js/*helpers_esm_asyncToGener*.*`,
`${src}build/static/js/*emotion_styled_dist*.*`,
`${src}build/static/js/*mui_system_colorManipulator*.*`,
];
}
/**
* Patch an HTML file (async function)
*
* @param fileName File name to be patched. Normally `${__dirname}/src-admin/build/index.html` or `${__dirname}/src/build/index.html`
* @param rootDir for admin. Admin has '.' and all other adapters '../..'
*/
export function patchHtmlFile(fileName: string, rootDir?: string): Promise<boolean> {
return new Promise(resolve => {
if (fs.existsSync(fileName)) {
resolve(_patchHtmlFile(fileName, rootDir));
} else {
// wait till finished
setTimeout(() => resolve(_patchHtmlFile(fileName, rootDir)), 2000);
}
});
}