-
Notifications
You must be signed in to change notification settings - Fork 73
/
gulpfile.js
338 lines (298 loc) · 9.79 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
const gulp = require('gulp');
const through = require('through2');
const fs = require('fs');
const zip = require('gulp-zip');
const del = require('del');
const bump = require('gulp-update-version');
const pipeline = require('readable-stream').pipeline;
const mergeStream = require('merge-stream');
const rollup = require('gulp-rollup');
const ts = require('gulp-typescript');
/** Default mathjs configuration does not support BigNumbers */
//const math = require('mathjs');
/** MathJS configuration for BigNumbers **/
const {create, all} = require('mathjs');
const math = create(all);
math.config({
number: 'BigNumber',
precision: 256
});
/** Constants definitions **/
const datePlusConstant = 116444736000000000;
const dividedDatePlusConstant = datePlusConstant / 1000;
/** Global layout output variable **/
const layoutOutput = {content: []};
var packageSize = 0;
/** Directories configuration **/
/** Directories for layout and manifest **/
const directoriesToProcess = ['./**', '!*.*', '!*', '!./html_ui/Pages/VCockpit/Core/**', '!./DOCS/**', '!./build/**', '!./release/**', '!./node_modules/**', '!./src/**'];
const directoriesToProcessDEV = ['./**', '!*.*', '!*', '!./DOCS/**', '!./build/**', '!./release/**', '!./node_modules/**', '!./src/**'];
/** Directories for release**/
const directoriesToRelease = ['./**', '!*', 'LICENSE', 'thirdparty_licenses.txt', './manifest.json', './layout.json', '!./html_ui/Pages/VCockpit/Core/**', '!./DOCS/**', '!./build/**', '!./release/**', '!./node_modules/**', '!./src/**'];
/** Internal Transformers */
const _prepareLayoutFile = (data) => {
let newPath = data.relativePath.replace(/\\/g, '\/');
packageSize += data.fileSize;
layoutOutput.content.push({
'path': newPath,
'size': data.fileSize,
'date': data.fileDate
});
};
const TerminalColors = {
default: '\x1b[0m',
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m'
};
function log(message, color = TerminalColors.default) {
console.log(color + message + TerminalColors.default);
}
const _updateManifest = () => {
log('Updating manifest.json.', TerminalColors.yellow);
let originalManifest = fs.readFileSync('manifest.json').toString();
let manifestJson = JSON.parse(originalManifest);
manifestJson.total_package_size = String(packageSize).padStart(20, '0');
fs.writeFile('manifest.json', JSON.stringify(manifestJson, null, 4), () => {
});
log('manifest.json updated.', TerminalColors.green);
};
const copyPackageVersion = () => {
let originalManifest = fs.readFileSync('manifest.json').toString();
let manifestJson = JSON.parse(originalManifest);
let version = {};
version.package_version = manifestJson.package_version;
let versionChunks = manifestJson.package_version.replace(/\./g, '-').split('-');
let versionChunks2 = versionChunks;
if (versionChunks.length < 4) {
versionChunks.push('STA');
versionChunks2.push('BAK');
}
versionChunks[0] = String(versionChunks[0]).padStart(3, '0');
const versionString = versionChunks.join('-');
const version2String = versionChunks.join('-');
version.fms_man_version = 'HD-P' + versionString;
version.fms_bak_version = 'HD-C' + version2String;
fs.writeFile('./html_ui/b78xh/b78xh.json', JSON.stringify(version, null, 4), () => {
console.log('version copied successfully.');
});
};
function defaultTask(callback) {
callback();
}
function buildTask() {
/**
* This is important for resetting the array with watch (monitorSDKSource and so on)
* @type {number}
*/
layoutOutput.content.length = 0;
console.log(layoutOutput.content.length);
return gulp.src(directoriesToProcess, {nodir: true, cwd: './'})
.pipe(
through.obj(function (file, _, callback) {
const data = {
relativePath: file.relative,
fileSize: file.stat.size,
fileDate: math.chain(file.stat.mtimeMs).multiply(10000.0).add(datePlusConstant).done()
};
this.push(data);
callback();
})
).on('data', function (data) {
_prepareLayoutFile(data);
}).on('end', function () {
log('Creating layout.json', TerminalColors.yellow);
fs.writeFile('layout.json', JSON.stringify(layoutOutput, null, 4), _updateManifest);
log('layout.json created.', TerminalColors.green);
});
}
function buildDEVTask() {
/**
* This is important for resetting the array with watch (monitorSDKSource and so on)
* @type {number}
*/
layoutOutput.content.length = 0;
console.log(layoutOutput.content.length);
return gulp.src(directoriesToProcessDEV, {nodir: true, cwd: './'})
.pipe(
through.obj(function (file, _, callback) {
const data = {
relativePath: file.relative,
fileSize: file.stat.size,
fileDate: math.chain(file.stat.mtimeMs).multiply(10000.0).add(datePlusConstant).done()
};
this.push(data);
callback();
})
).on('data', function (data) {
_prepareLayoutFile(data);
}).on('end', function () {
log('Creating layout.json', TerminalColors.yellow);
fs.writeFile('layout.json', JSON.stringify(layoutOutput, null, 4), _updateManifest);
log('layout.json created.', TerminalColors.green);
});
}
function releaseTask(callback) {
return gulp.src('release/cache/**')
.pipe(zip('release.zip'))
.pipe(gulp.dest('release'))
.on('finish', function () {
console.log('Release done.');
callback();
});
}
function copyFilesForReleaseToCache(callback) {
return gulp.src(directoriesToRelease)
.pipe(gulp.dest('release/cache/B78XH/'))
.on('finish', function () {
console.log('Files for release copied.');
callback();
});
}
function deleteReleaseCache(callback) {
del('release/cache');
console.log('Release cache deleted.');
callback();
}
function bumpTask(callback) {
return gulp.src('./manifest.json')
.pipe(bump())
.pipe(gulp.dest('./')).on('end', function () {
copyPackageVersion();
});
}
function preBumpTask(callback) {
return gulp.src('./manifest.json')
.pipe(bump({type: 'prerelease'}))
.pipe(gulp.dest('./')).on('finish', function () {
console.log('Release done.');
callback();
}).on('end', function () {
copyPackageVersion();
});
}
const HDSDKProject = ts.createProject('./src/hdsdk/tsconfig.json');
function buildHDSDKTask() {
let res = gulp.src('src/hdsdk/**/*.ts').pipe(HDSDKProject());
return pipeline(
res.dts,
gulp.dest('src/hdsdk/types'),
res.js,
gulp.dest('build/cache/hdsdk')
);
}
const HDLoggerProject = ts.createProject('./src/hdlogger/tsconfig.json');
function buildHDLoggerTask() {
let res = gulp.src('src/hdlogger/**/*.ts').pipe(HDLoggerProject());
return pipeline(
res.dts,
gulp.dest('src/hdlogger/types'),
res.js,
gulp.dest('build/cache/hdlogger')
);
}
const InstrumentsProject = ts.createProject('./src/instruments/tsconfig.json');
function buildInstrumentsTask() {
let res = gulp.src('src/instruments/**/*.ts').pipe(InstrumentsProject());
return pipeline(
res.dts,
gulp.dest('src/instruments/types'),
res.js,
gulp.dest('build/cache/instruments')
);
}
function rollupHDSDKTask() {
return pipeline(
gulp.src('build/cache/hdsdk/**/*.js'),
rollup({
input: 'build/cache/hdsdk/hdsdk.js',
output: {
format: 'umd',
sourcemap: false,
extend: true,
name: 'window'
}
}),
gulp.dest('build/cache/rollups')
);
}
function rollupHDLoggerTask() {
return pipeline(
gulp.src('build/cache/hdlogger/**/*.js'),
rollup({
input: 'build/cache/hdlogger/hdlogger.js',
output: {
format: 'umd',
sourcemap: false,
extend: true,
name: 'window'
}
}),
gulp.dest('build/cache/rollups')
);
}
function copyHDSDKTask() {
return pipeline(
gulp.src('build/cache/rollups/hdsdk.js'),
gulp.dest('html_ui/Heavy/libs')
);
}
function copyHDLoggerTask() {
return pipeline(
gulp.src('build/cache/rollups/hdlogger.js'),
gulp.dest('html_ui/Heavy/libs')
);
}
function copyInstrumentsTask() {
return pipeline(
gulp.src('build/cache/instruments/**/*'),
gulp.dest('html_ui/Pages/VCockpit/Instruments')
);
}
function cleanBuildCache(callback) {
del('build/**/*');
callback();
}
function cleanBuildCacheSDK(callback) {
del('build/cache/hdsdk/**/*');
callback();
}
function cleanBuildCacheLogger(callback) {
del('build/cache/hdlogger/**/*');
callback();
}
function cleanBuildCacheInstruments(callback) {
del('build/cache/instruments/**/*');
callback();
}
function monitorHDSDKSourceDirectory() {
log('Monitoring build folder.\n', TerminalColors.blue);
gulp.watch(['src/hdsdk/**/*', '!src/hdsdk/tsconfig.json'], {ignoreInitial: true}, gulp.series(cleanBuildCache, buildHDSDKTask, rollupHDSDKTask, copyHDSDKTask, cleanBuildCache, buildTask)).on('change', function (path, stats) {
log('Source files were changed. Starting build process...', TerminalColors.red);
});
}
function monitorHDLoggerSourceDirectory() {
log('Monitoring build folder.\n', TerminalColors.blue);
gulp.watch(['src/hdlogger/**/*', '!src/hdlogger/tsconfig.json'], {ignoreInitial: true}, gulp.series(cleanBuildCacheLogger, buildHDLoggerTask, rollupHDLoggerTask, copyHDLoggerTask, cleanBuildCacheLogger, buildTask)).on('change', function (path, stats) {
log('Source files were changed. Starting build process...', TerminalColors.red);
});
}
exports.release = gulp.series(deleteReleaseCache, buildTask, copyFilesForReleaseToCache, releaseTask, deleteReleaseCache);
exports.default = buildTask;
exports.build = buildTask;
exports.buildDEV = buildDEVTask;
exports.bump = bumpTask;
exports.prebump = preBumpTask;
exports.buildSDK = gulp.series(cleanBuildCache, buildHDSDKTask, rollupHDSDKTask, copyHDSDKTask, cleanBuildCacheSDK, buildTask);
exports.buildLogger = gulp.series(cleanBuildCacheLogger, buildHDLoggerTask, rollupHDLoggerTask, copyHDLoggerTask, cleanBuildCacheLogger, buildTask);
exports.buildInstruments = gulp.series(cleanBuildCacheInstruments, buildInstrumentsTask, copyInstrumentsTask, cleanBuildCacheInstruments, buildTask);
/**
* Monitoring
*/
exports.monitorSDKSource = monitorHDSDKSourceDirectory;
exports.monitorHDLoggerSource = monitorHDLoggerSourceDirectory;