forked from Heavy-Division/B78XH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
103 lines (92 loc) · 3.01 KB
/
gulpfile.js
File metadata and controls
103 lines (92 loc) · 3.01 KB
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
const gulp = require('gulp');
const through = require('through2');
const fs = require('fs');
const zip = require('gulp-zip');
const del = require('del');
/** 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 = ['./**', '!*.*', '!*', '!./DOCS/**', '!./build/**', '!./release/**', '!./node_modules/**'];
/** Directories for release**/
const directoriesToRelease = ['./**', '!*', 'LICENSE', './manifest.json', './layout.json', '!./DOCS/**', '!./build/**', '!./release/**', '!./node_modules/**'];
/** 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 _updateManifest = () => {
console.log('Updating manifest.json')
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), () => {
});
console.log('manifest.json updated.')
};
function defaultTask(callback) {
callback();
}
function buildTask() {
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 () {
console.log('Creating layout.json')
fs.writeFile('layout.json', JSON.stringify(layoutOutput, null, 4), _updateManifest);
console.log('layout.json created.')
});
}
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()
}
exports.release = gulp.series(deleteReleaseCache, buildTask, copyFilesForReleaseToCache, releaseTask, deleteReleaseCache);
exports.default = buildTask;
exports.build = buildTask;