-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathgulpfile.js
172 lines (144 loc) · 5.64 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
/**
* Copyright 2018-2023 bluefox <[email protected]>
*
* MIT License
*
**/
'use strict';
const gulp = require('gulp');
const fs = require('fs');
const rename = require('gulp-rename');
const cp = require('child_process');
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);
}
}
}
}
gulp.task('clean', done => {
deleteFoldersRecursive(`${__dirname}/admin`);
deleteFoldersRecursive(`${__dirname}/src/build`);
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 installation npm packet will be deleted too, but some files must be loaded even during the install 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 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();
} else {
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/`,
env: {
SKIP_PREFLIGHT_CHECK: 'true'
}
};
// copy langModel.js from lib to src/public
fs.writeFileSync(`${__dirname}/src/public/langModel.js`, fs.readFileSync(`${__dirname}/lib/langModel.js`));
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', '3-build'));
function copyFiles() {
deleteFoldersRecursive(`${__dirname}/admin`);
return Promise.all([
gulp.src([
'src/build/**/*',
'!src/build/index.html',
'!src/build/static/js/main.*.chunk.js',
'!src/build/i18n/**/*',
'!src/build/i18n',
'admin-config/*'
])
.pipe(gulp.dest('admin/')),
gulp.src([
'src/build/index.html',
])
.pipe(rename('tab_m.html'))
.pipe(gulp.dest('admin/')),
gulp.src([
'src/build/static/js/main.*.chunk.js',
])
.pipe(gulp.dest('admin/static/js/')),
]);
}
gulp.task('5-copy', () => copyFiles());
gulp.task('5-copy-dep', gulp.series('3-build-dep', '5-copy'));
gulp.task('6-patch', () => new Promise(resolve => {
if (fs.existsSync(`${__dirname}/admin/tab.html`)) {
let code = fs.readFileSync(`${__dirname}/admin/tab.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/tab.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')); //gulp.series('6-patch-dep')