-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgulpfile.js
197 lines (165 loc) · 6.9 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
194
195
196
'use strict';
const fs = require('fs');
const cp = require('child_process');
const gulp = require('gulp');
const del = require('del');
const replace = require('gulp-replace');
const rename = require('gulp-rename');
const srcRx = 'src/';
const src = __dirname + '/' + srcRx;
const dest = 'admin/';
function npmInstall() {
return new Promise((resolve, reject) => {
// Install node modules
const cwd = src.replace(/\\/g, '/');
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 child = cp.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 build() {
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));
return new Promise((resolve, reject) => {
const options = {
stdio: 'pipe',
cwd: src
};
console.log(options.cwd);
let script = src + 'node_modules/@craco/craco/bin/craco.js';
if (!fs.existsSync(script)) {
script = __dirname + '/node_modules/@craco/craco/bin/craco.js';
}
if (!fs.existsSync(script)) {
console.error('Cannot find execution file: ' + script);
reject('Cannot find execution file: ' + script);
} else {
const cmd = `node --max-old-space-size=8192 ${script} build`;
const child = cp.exec(cmd, { cwd: src });
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 ${src} finished.`);
// command succeeded
resolve();
}
});
// const child = cp.fork(script, ['--max-old-space-size=8192', '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();
});
*/
}
});
}
function copyFiles() {
return del([
dest + '**/*',
'!admin/admin.d.ts',
'!admin/ham.png',
'!admin/index_m.html',
'!admin/tsconfig.json',
'!admin/words.js',
])
.then(() => Promise.all([
gulp.src([
srcRx + 'build/**/*',
`!${srcRx}build/index.html`,
`!${srcRx}build/static/js/*.js`,
`!${srcRx}build/i18n/**/*`,
`!${srcRx}build/i18n`
])
.pipe(gulp.dest(dest)),
gulp.src([
`${srcRx}build/index.html`,
])
.pipe(replace('href="/', 'href="'))
.pipe(replace('src="/', 'src="'))
.pipe(rename('tab_m.html'))
.pipe(gulp.dest(dest)),
gulp.src([
`${srcRx}build/static/js/*.js`,
])
.pipe(replace('s.p+"static/media', '"./static/media'))
.pipe(gulp.dest(dest + 'static/js/')),
]));
}
function patchIndex() {
return new Promise(resolve => {
if (fs.existsSync(dest + '/tab_m.html')) {
let code = fs.readFileSync(dest + '/tab_m.html').toString('utf8');
// replace code
code = code.replace(/<script>const script=document[^<]+<\/script>/, `<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="../../lib/js/socket.io.js"></script>`);
code = code.replace(/<script>var script=document[^<]+<\/script>/, `<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="../../lib/js/socket.io.js"></script>`);
fs.writeFileSync(dest + '/tab_m.html', code);
resolve();
} else {
// wait till finished
setTimeout(() => {
if (fs.existsSync(dest + '/tab_m.html')) {
let code = fs.readFileSync(dest + '/tab_m.html').toString('utf8');
// replace code
code = code.replace(/<script>const script=document[^<]+<\/script>/, `<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="../../lib/js/socket.io.js"></script>`);
code = code.replace(/<script>var script=document[^<]+<\/script>/, `<script type="text/javascript" onerror="setTimeout(function(){window.location.reload()}, 5000)" src="../../lib/js/socket.io.js"></script>`);
fs.writeFileSync(dest + '/tab_m.html', code);
}
resolve();
}, 2000);
}
});
}
gulp.task('react-1-clean', () => {
return del([
dest + '**/*',
dest + '/*',
'!admin/admin.d.ts',
'!admin/ham.png',
'!admin/index_m.html',
'!admin/tsconfig.json',
'!admin/words.js',
srcRx + 'build/**/*'
])
.then(del([
'src/build',
]));
});
gulp.task('react-2-npm', () => {
if (fs.existsSync(src + 'node_modules')) {
return Promise.resolve();
} else {
return npmInstall();
}
});
gulp.task('react-2-npm-dep', gulp.series('react-1-clean', 'react-2-npm'));
gulp.task('react-3-build', () => build());
gulp.task('react-3-build-dep', gulp.series('react-2-npm-dep', 'react-3-build'));
gulp.task('react-5-copy', () => copyFiles());
gulp.task('react-5-copy-dep', gulp.series('react-3-build-dep', 'react-5-copy'));
gulp.task('react-6-patch', () => patchIndex());
gulp.task('react-6-patch-dep', gulp.series('react-5-copy-dep', 'react-6-patch'));
gulp.task('react-build', gulp.series('react-6-patch-dep'));
gulp.task('default', gulp.series('react-build'));