-
Notifications
You must be signed in to change notification settings - Fork 7
/
tasksExample.js
91 lines (78 loc) · 2.77 KB
/
tasksExample.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
/**
* Copyright 2024 bluefox <dogafox@gmail.com>
*
* MIT License
*
*/
'use strict';
const fs = require('node:fs');
const { deleteFoldersRecursive, npmInstall, buildReact, copyFiles } = require('@iobroker/build-tools');
const SRC = 'src';
function copyAllFiles() {
deleteFoldersRecursive('admin', ['.png', '.json', 'i18n']);
copyFiles(
[
`${SRC}/build/*`,
`!${SRC}/build/index.html`,
`!${SRC}/build/static/js/main.*.chunk.js`,
`!${SRC}/build/static/media/*.svg`,
`!${SRC}/build/static/media/*.txt`,
`!${SRC}/build/i18n/*`,
`!${SRC}/build/i18n`,
],
'admin',
);
copyFiles(`${SRC}/build/index.html`, 'admin');
copyFiles(`${SRC}/build/static/js/main.*.chunk.js`, 'admin/static/js');
}
function clean() {
deleteFoldersRecursive('admin', ['.png', '.json', 'i18n']);
deleteFoldersRecursive(`${SRC}/build`);
}
function installNpmLocal() {
if (fs.existsSync(`${SRC}/node_modules`)) {
return Promise.resolve();
}
return npmInstall(`${__dirname.replace(/\\/g, '/')}/${SRC}/`);
}
function patchFiles() {
if (fs.existsSync(`${__dirname}/admin/index.html`)) {
let code = fs.readFileSync(`${__dirname}/admin/index.html`).toString('utf8');
code = code.replace(
/<script>var script=document\.createElement\("script"\).+?<\/script>/,
`<script type="text/javascript" src="./../../lib/js/socket.io.js"></script>`,
);
fs.writeFileSync(`${__dirname}/admin/index.html`, code);
}
if (fs.existsSync(`${__dirname}/${SRC}/build/index.html`)) {
let code = fs.readFileSync(`${__dirname}/${SRC}/build/index.html`).toString('utf8');
code = code.replace(
/<script>var script=document\.createElement\("script"\).+?<\/script>/,
`<script type="text/javascript" src="./../../lib/js/socket.io.js"></script>`,
);
fs.writeFileSync(`${SRC}/build/index.html`, code);
}
}
if (process.argv.find(arg => arg === '--0-clean')) {
clean();
} else if (process.argv.find(arg => arg === '--1-npm')) {
npmInstall(`${__dirname.replace(/\\/g, '/')}/${SRC}/`).catch(e => {
console.error(`Cannot install: ${e}`);
process.exit(1);
});
} else if (process.argv.find(arg => arg === '--2-build')) {
buildReact(SRC, { rootDir: __dirname }).catch(e => {
console.error(`Cannot build: ${e}`);
process.exit(1);
});
} else if (process.argv.find(arg => arg === '--3-copy')) {
copyAllFiles();
} else if (process.argv.find(arg => arg === '--4-patch')) {
patchFiles();
} else {
clean();
installNpmLocal()
.then(() => buildReact(SRC, { rootDir: __dirname }))
.then(() => copyAllFiles())
.then(() => patchFiles());
}