-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtasksChart.js
133 lines (115 loc) · 4.78 KB
/
tasksChart.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
/**
* Copyright 2018-2025 bluefox <[email protected]>
*
* MIT License
*
**/
'use strict';
const { readFileSync, existsSync, writeFileSync } = require('node:fs');
const {
deleteFoldersRecursive,
npmInstall,
copyFolderRecursiveSync,
buildReact,
copyFiles,
} = require('@iobroker/build-tools');
function copyAllFiles() {
deleteFoldersRecursive(`${__dirname}/admin/chart`);
copyReactFilesToBackEnd();
copyFiles(['src-chart/build/**/*'], 'admin/chart/');
}
function checkChart(resolve, reject, start) {
if (!resolve) {
return new Promise((resolve, reject) => checkChart(resolve, reject, Date.now()));
}
console.log('Check src-chart/build/service-worker.js');
if (existsSync(`${__dirname}/src-chart/build/index.html`)) {
console.log('Exists src-chart/build/index.html');
setTimeout(() => resolve(), 500);
} else {
if (Date.now() - start > 30000) {
reject('timeout');
} else {
console.log('Wait for src-chart/build/index.html');
setTimeout(() => checkChart(resolve, reject, start), 500);
}
}
}
function copyFilesToWWW() {
deleteFoldersRecursive(`${__dirname}/www`);
return checkChart()
.then(() => copyFolderRecursiveSync(`${__dirname}/admin/chart/`, `${__dirname}/www`, ['.svg', '.txt']))
.then(
() =>
new Promise(resolve => {
if (existsSync(`${__dirname}/www/index.html`)) {
let code = readFileSync(`${__dirname}/www/index.html`).toString('utf8');
code = code.replace(
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/,
`<script type="text/javascript" src="./../lib/js/socket.io.js"></script>`,
);
code = code.replace(
'<script type="text/javascript" src="./../../lib/js/socket.io.js"></script>',
`<script type="text/javascript" src="./../lib/js/socket.io.js"></script>`,
);
if (!code.includes('_socket/info.js')) {
code = code.replace(
'<script type="text/javascript" src="./../lib/js/socket.io.js"></script>',
'<script type="text/javascript" src="./../lib/js/socket.io.js"></script><script type="text/javascript" src="_socket/info.js"></script>',
);
}
writeFileSync(`${__dirname}/www/index.html`, code);
}
resolve();
}),
);
}
deleteFoldersRecursive(`${__dirname}/admin/chart`);
deleteFoldersRecursive(`${__dirname}/src-chart/build`);
function copyReactFilesToBackEnd() {
let chartOptions = readFileSync(`${__dirname}/src-chart/src/Components/ChartOption.ts`).toString('utf8');
let chartModel = readFileSync(`${__dirname}/src-chart/src/Components/ChartModel.ts`).toString('utf8');
chartModel = chartModel
.replace(
"'@iobroker/adapter-react-v5';",
"'../types';",
)
.replace(
"'../../../src/types';",
"'../types';",
);
chartOptions = chartOptions.replace(
"'../../../src/types';",
"'../types';",
);
writeFileSync(`${__dirname}/src/lib/ChartOption.ts`, chartOptions);
writeFileSync(`${__dirname}/src/lib/ChartModel.ts`, chartModel);
}
let installPromise;
if (!existsSync(`${__dirname}/src-chart/node_modules`)) {
installPromise = npmInstall(`${__dirname.replace(/\\/g, '/')}/src-chart/`);
} else {
installPromise = Promise.resolve();
}
installPromise
.then(() => buildReact(`${__dirname}/src-chart/`, { vite: true, tsc: true }))
.then(() => copyAllFiles())
.then(() => {
if (existsSync(`${__dirname}/admin/chart/index.html`)) {
let code = readFileSync(`${__dirname}/admin/chart/index.html`).toString('utf8');
code = code.replace(
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/,
`<script type="text/javascript" src="./../../lib/js/socket.io.js"></script>`,
);
writeFileSync(`${__dirname}/admin/chart/index.html`, code);
}
if (existsSync(`${__dirname}/src-chart/build/index.html`)) {
let code = readFileSync(`${__dirname}/src-chart/build/index.html`).toString('utf8');
code = code.replace(
/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/,
`<script type="text/javascript" src="./../../lib/js/socket.io.js"></script>`,
);
writeFileSync(`${__dirname}/src-chart/build/index.html`, code);
}
})
.then(() => copyFilesToWWW());