-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.js
98 lines (91 loc) · 3.16 KB
/
tasks.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
const { existsSync, copyFileSync, readFileSync, statSync, writeFileSync } = require('node:fs');
const { deleteFoldersRecursive, copyFiles, npmInstall, buildReact, patchHtmlFile } = require('@iobroker/build-tools');
function clean() {
deleteFoldersRecursive(`${__dirname}/admin`, ['matter.png']);
deleteFoldersRecursive(`${__dirname}/src-admin/build`);
}
function copyAllFiles() {
copyFiles(['src-admin/build/**/*', '!src-admin/build/index.html'], 'admin/');
}
function copyI18n() {
copyFiles(['src/lib/i18n/**/*'], 'build/lib/i18n');
}
function sync2files(src, dst) {
const srcTxt = readFileSync(src).toString('utf8');
const destTxt = readFileSync(dst).toString('utf8');
if (srcTxt !== destTxt) {
const srcs = statSync(src);
const dest = statSync(dst);
if (srcs.mtime > dest.mtime) {
writeFileSync(dst, srcTxt);
} else {
writeFileSync(src, destTxt);
}
}
}
async function patch() {
await patchHtmlFile(`${__dirname}/src-admin/build/index.html`, '../..');
if (!existsSync(`${__dirname}/src-admin/build/index.html`)) {
console.error('Index.html not found!');
process.exit(2);
}
copyFileSync(`${__dirname}/src-admin/build/index.html`, `${__dirname}/admin/index_m.html`);
copyFileSync(`${__dirname}/src-admin/build/index.html`, `${__dirname}/admin/tab_m.html`);
}
if (process.argv.includes('--0-clean')) {
clean();
} else if (process.argv.includes('--1-npm')) {
npmInstall(`${__dirname}/src-admin`).catch(e => {
console.error(`Cannot install npm: ${e}`);
process.exit(1);
});
} else if (process.argv.includes('--2-build')) {
buildReact(`${__dirname}/src-admin/`, {
rootDir: __dirname,
vite: true,
}).catch(e => {
console.error(`Cannot build react: ${e}`);
process.exit(1);
});
} else if (process.argv.includes('--3-copy')) {
copyAllFiles();
} else if (process.argv.includes('--4-patch')) {
patch().catch(e => {
console.error(`Cannot patch: ${e}`);
process.exit(1);
});
} else if (process.argv.includes('--build')) {
clean();
sync2files(`${__dirname}/src/lib/vendorIDs.ts`, `${__dirname}/src-admin/src/utils/vendorIDs.ts`);
npmInstall(`${__dirname}/src-admin`)
.then(() =>
buildReact(`${__dirname}/src-admin/`, {
rootDir: __dirname,
vite: true,
}),
)
.then(() => copyAllFiles())
.then(() => patch())
.catch(e => {
console.error(`Cannot build: ${e}`);
process.exit(1);
});
} else if (process.argv.includes('--copy-i18n')) {
copyI18n();
} else {
clean();
sync2files(`${__dirname}/src/lib/vendorIDs.ts`, `${__dirname}/src-admin/src/utils/vendorIDs.ts`);
npmInstall(`${__dirname}/src-admin`)
.then(() =>
buildReact(`${__dirname}/src-admin/`, {
rootDir: __dirname,
vite: true,
}),
)
.then(() => copyAllFiles())
.then(() => patch())
.catch(e => {
console.error(`Cannot build admin controls: ${e}`);
process.exit(1);
});
}