-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgulpfile.js
196 lines (165 loc) · 7.24 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
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
/*!
* ioBroker gulpfile
* Date: 2022-07-08
*/
'use strict';
const gulp = require('gulp');
const fs = require('fs');
const cp = require('child_process');
const adapterName = require('./package.json').name.replace('iobroker.', '');
const SRC = 'src-widgets/';
const src = `${__dirname}/${SRC}`;
function deleteFoldersRecursive(path, exceptions) {
if (fs.existsSync(path)) {
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);
}
}
}
}
function npmInstall() {
return new Promise((resolve, reject) => {
// Install node modules
const cwd = src.replace(/\\/g, '/');
//const cmd = `npm install -f`;
const cmd = `yarn`;
console.log(`"${cmd} in ${cwd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
const exec = cp.exec;
const child = exec(cmd, {cwd});
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is 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();
}
});
});
}
function startDevServerWatch() {
return new Promise((resolve, reject) => {
//const cmd = `npm install -f`;
const cwd='./'
try {
const isDir=fs.statSync('./.dev-server/').isDirectory();
} catch (e) {
reject('cannot start dev-server since it was not found');
}
const cmd = `dev-server watch`;
console.log(`"${cmd} in ${cwd}`);
// System call used for update of js-controller itself,
// because during installation npm packet will be deleted too, but some files must be loaded even during the install process.
const exec = cp.exec;
const child = exec(cmd, {cwd});
child.stderr.pipe(process.stderr);
child.stdout.pipe(process.stdout);
child.on('exit', (code /* , signal */) => {
// code 1 is strange error that cannot be explained. Everything is installed but error :(
if (code && code !== 1) {
reject(`Cannot start dev-server: ${code}`);
} else {
console.log(`"${cmd} in ${cwd} finished.`);
// command succeeded
resolve();
}
});
});
}
function buildRules() {
const version = JSON.parse(fs.readFileSync(`${__dirname}/package.json`).toString('utf8')).version;
const data = JSON.parse(fs.readFileSync(`${src}package.json`).toString('utf8'));
data.version = version;
fs.writeFileSync(`${src}package.json`, JSON.stringify(data, null, 4));
// we have bug, that federation requires version number in @mui/material/styles, so we have to change it
// read version of @mui/material and write it to @mui/material/styles
const muiStyleVersion = require(`${src}node_modules/@mui/material/styles/package.json`);
if (!muiStyleVersion.version) {
const muiVersion = require(`${src}node_modules/@mui/material/package.json`);
muiStyleVersion.version = muiVersion.version;
fs.writeFileSync(`${src}node_modules/@mui/material/styles/package.json`, JSON.stringify(muiStyleVersion, null, 2));
}
return new Promise((resolve, reject) => {
const options = {
stdio: 'pipe',
cwd: src
};
console.log(options.cwd);
let script = `${src}node_modules/@craco/craco/dist/bin/craco.js`;
if (!fs.existsSync(script)) {
script = `${__dirname}/node_modules/@craco/craco/dist/bin/craco.js`;
}
if (!fs.existsSync(script)) {
console.error(`Cannot find execution file: ${script}`);
reject(`Cannot find execution file: ${script}`);
} else {
const child = cp.fork(script, ['build'], 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('widget-0-clean', done => {
deleteFoldersRecursive(`${src}build`);
deleteFoldersRecursive(`widgets`);
done();
});
gulp.task('widget-1-npm', async () => npmInstall());
gulp.task('widget-2-compile', async () => buildRules());
gulp.task('widget-3-createdirectories', () => Promise.all([
new Promise(resolve =>
setTimeout(() => {
if (fs.existsSync(`widgets/${adapterName}/static/js`) &&
!fs.readdirSync(`widgets/${adapterName}/static/js`).length
) {
fs.rmdirSync(`widgets/${adapterName}/static/js`)
}
resolve();
}, 500)
),
new Promise(resolve =>
setTimeout(() => {
if (fs.existsSync(`widgets/${adapterName}/static/media`) &&
!fs.readdirSync(`widgets/${adapterName}/static/media`).length
) {
fs.rmdirSync(`widgets/${adapterName}/static/media`)
}
resolve();
}, 500)
)
]));
gulp.task('widget-4-copy', () => Promise.all([
gulp.src([`${SRC}build/*.js`]).pipe(gulp.dest(`widgets/${adapterName}`)),
gulp.src([`${SRC}build/img/*`]).pipe(gulp.dest(`widgets/${adapterName}/img`)),
gulp.src([`${SRC}build/*.map`]).pipe(gulp.dest(`widgets/${adapterName}`)),
gulp.src([`${SRC}build/static/js/node_modules*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/js`)),
gulp.src([`${SRC}build/static/js/vendors-node_modules*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/js`)),
gulp.src([`${SRC}build/static/js/main*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/js`)),
gulp.src([`${SRC}build/static/js/src_bootstrap*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/js`)),
gulp.src([`${SRC}build/static/js/src_*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/js`)),
gulp.src([`${SRC}build/static/media/TrashIcon*.*`]).pipe(gulp.dest(`widgets/${adapterName}/static/media`)),
gulp.src([`${SRC}src/i18n/*.json`]).pipe(gulp.dest(`widgets/${adapterName}/i18n`))
]));
gulp.task('widget-5-start-dev-server', async () => startDevServerWatch());
gulp.task('widget-build', gulp.series(['widget-0-clean', 'widget-1-npm', 'widget-2-compile', 'widget-3-createdirectories', 'widget-4-copy']));
//gulp.task('widget-build', gulp.series(['widget-3-createdirectories', 'widget-4-copy']));
gulp.task('default', gulp.series('widget-build'));