forked from ioBroker/ioBroker.modbus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
144 lines (120 loc) · 4.99 KB
/
gulpfile.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
134
135
136
137
138
139
140
141
142
143
144
/*!
* ioBroker gulpfile
* Date: 2019-01-28
*/
'use strict';
const gulp = require('gulp');
const fs = require('fs');
const cp = require('child_process');
function deleteFoldersRecursive(path, exceptions) {
if (fs.existsSync(path)) {
const stat = fs.statSync(path);
if (stat.isDirectory()) {
const files = fs.readdirSync(path);
for (const file of files) {
const curPath = `${path}/${file}`;
if (exceptions && exceptions.find(e => curPath.endsWith(e))) {
continue;
}
const stat = fs.statSync(curPath);
if (stat.isDirectory()) {
deleteFoldersRecursive(curPath);
fs.rmdirSync(curPath);
} else {
fs.unlinkSync(curPath);
}
}
} else {
fs.unlinkSync(path);
}
}
}
// TASKS
gulp.task('clean', done => {
deleteFoldersRecursive(`${__dirname}/admin`, ['modbus.png']);
done();
});
function npmInstall() {
return new Promise((resolve, reject) => {
// Install node modules
const cwd = __dirname.replace(/\\/g, '/') + '/src/';
const cmd = `npm install -f`;
console.log(`"${cmd} in ${cwd}`);
// 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 installation process.
const exec = require('child_process').exec;
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(`Cannot install: ${code}`);
} else {
console.log(`"${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
});
});
}
gulp.task('2-npm', () => {
if (fs.existsSync(`${__dirname}/src/node_modules`)) {
return Promise.resolve();
}
return npmInstall();
});
gulp.task('2-npm-dep', gulp.series('clean', '2-npm'));
function build() {
return new Promise((resolve, reject) => {
const options = {
stdio: 'pipe',
cwd: `${__dirname}/src/`
};
const version = JSON.parse(fs.readFileSync(`${__dirname}/package.json`).toString('utf8')).version;
const data = JSON.parse(fs.readFileSync(`${__dirname}/src/package.json`).toString('utf8'));
data.version = version;
fs.writeFileSync(`${__dirname}/src/package.json`, JSON.stringify(data, null, 4));
console.log(options.cwd);
let script = `${__dirname}/src/node_modules/react-scripts/scripts/build.js`;
if (!fs.existsSync(script)) {
script = `${__dirname}/node_modules/react-scripts/scripts/build.js`;
}
if (!fs.existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child = cp.fork(script, [], options);
child.stdout.on('data', data => console.log(data.toString()));
child.stderr.on('data', data => console.log(data.toString()));
child.on('close', code => {
console.log(`child process exited with code ${code}`);
code ? reject(`Exit code: ${code}`) : resolve();
});
}
});
}
gulp.task('3-build', () => build());
gulp.task('3-build-dep', gulp.series('2-npm-dep', '3-build'));
gulp.task('5-copy', () =>
gulp.src(['src/build/*/**', 'src/build/*', '!src/build/static/media/*.svg', '!src/build/static/media/*.txt'])
.pipe(gulp.dest('admin/')));
gulp.task('5-copy-dep', gulp.series('3-build-dep', '5-copy'));
gulp.task('6-patch', () => new Promise(resolve => {
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.unlinkSync(`${__dirname}/admin/index.html`);
fs.writeFileSync(`${__dirname}/admin/index_m.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(`${__dirname}/src/build/index.html`, code);
}
resolve();
}));
gulp.task('6-patch-dep', gulp.series('5-copy-dep', '6-patch'));
gulp.task('default', gulp.series('6-patch-dep'));