-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
130 lines (117 loc) · 3.07 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
'use strict';
const gulp = require('gulp');
const uglify = require('gulp-uglify-es').default;
const clean = require('gulp-clean');
const pump = require('pump');
const run = require('gulp-run-command').default;
const fs = require('fs');
const moment = require('moment');
let platform = 'windows-x86';
let version;
/**
* Prepare build
*/
gulp.task('prepare-build', function (callback) {
// get platform and type from CLI
process.argv.forEach(function (arg, index) {
if (arg === '--platform') {
platform = process.argv[index + 1];
}
if (arg === '--version') {
version = process.argv[index + 1];
}
});
// keep a list of errors
const errors = [];
// validate platform
const supportedPlatforms = ['windows-x86', 'windows-x64', 'mac_osx-x64', 'linux_debian-x86', 'linux_debian-x64', 'linux_rhel-x86', 'linux_rhel-x64'];
if (!supportedPlatforms.includes(platform)) {
errors.push(`Invalid platform: ${platform}. Supported platforms: ${supportedPlatforms.join(', ')}`);
}
// check if there are errors
if (errors.length) {
return callback(errors);
}
// output build information
process.stdout.write(`\nBuilding instance for ${platform}.\nYou can specify other types and platforms by sending arguments. E.g.: npm run build -- --version 1.1.1 --platform windows-x86\n\n`);
callback();
});
/**
* Clean up build directory
*/
gulp.task('clean', gulp.series('prepare-build', function (callback) {
pump(
[
gulp.src(
[
'build'
],
{
allowEmpty: true
}
),
clean()
],
callback
);
}));
/**
* Copy source files to build directory
*/
gulp.task('copy', gulp.series('clean', function (callback) {
pump(
[
gulp.src(
[
'**',
'!build',
'!build/**',
'!node_modules',
'!node_modules/**',
'!gulpfile.js'
],
{buffer: false}
),
gulp.dest('build')
],
callback
);
}));
/**
* Compress source
*/
gulp.task('compress', gulp.series('copy', function (callback) {
pump(
[
gulp.src(['build/**/*.js']),
uglify(),
gulp.dest('build')
],
callback
);
}));
/**
* Install dependencies
*/
gulp.task('install-dependencies', gulp.series('compress', async () => run('npm install --production', {
cwd: `${__dirname}/build`
})()));
/**
* Update build information
*/
gulp.task('update-build-info', gulp.series('install-dependencies', function (callback) {
const packageJson = require('./build/package');
if (!packageJson.build) {
packageJson.build = {};
}
// set build information
packageJson.build.platform = platform;
packageJson.build.version = version || packageJson.version;
packageJson.build.build = moment().format('YYMMDDHHmm');
packageJson.build.arch = packageJson.build.arch || 'x64';
// remove unneeded information
delete packageJson.devDependencies;
fs.writeFile(`${__dirname}/build/package.json`, JSON.stringify(packageJson, null, 2), callback);
}));
// run build process (task)
gulp.task('build', gulp.series('update-build-info'));