-
Notifications
You must be signed in to change notification settings - Fork 1
/
gulpfile.js
executable file
·265 lines (218 loc) · 5.13 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
'use strict';
require('sugar');
require('colors');
const
fs = require('fs'),
gulp = require('gulp'),
$C = require('collection.js').$C,
program = require('commander'),
semver = require('semver');
/**
* Environment types
*
* @enum {string}
*/
const environments = {
development: 'development',
dev: 'development',
production: 'production',
prod: 'production'
};
/**
* Output the message
*/
function log() {
if (!program.silent) {
console.log.apply(console, arguments);
}
}
/**
* Output the message, help and exit program
*
* @param {string} message
* @param {Command} [cmd = program]
*/
function die(message, cmd) {
cmd = cmd || program;
log(' Fatal error'.red + ' in ', cmd.name().yellow + ':', message);
cmd.help();
}
function determineVersion() {
return JSON.parse(fs.readFileSync('./package.json')).version;
}
/**
* Set NODE_ENV
*
* @param {environments} [value]
*/
function setEnvironment(value) {
let env = process.env.NODE_ENV || 'development';
if (value) {
env = environments[value];
}
if (!$C(environments).indexOf(env)) {
die(`invalid environment level value: ${value.yellow}`);
}
process.env.NODE_ENV = env;
program.env = env;
return env;
}
program
.version(determineVersion(), '-V')
.option('--no-color', 'No colors in output')
.option('--silent', 'Disable output')
.option(
'-e, --env <environment>',
'Set environment: (prod[uction]|dev[elopment])',
(value) => setEnvironment(value),
process.env.NODE_ENV || 'development'
)
.allowUnknownOption(true);
/*---------- BUMP COMMAND ----------*/
const cBump = program
.command('bump [type]')
.description(`Bump version (${
['major', 'minor', 'patch']
.map((s) => s.yellow)
.join('|')
}) [${'patch'.yellow}]`)
.action((type) => {
if (type && ['major', 'minor', 'patch'].indexOf(type) === -1) {
die(`invalid type "${type.yellow}"`, cBump);
}
cBump.type = type || 'patch';
gulp.task(cBump.type, [cBump.name()]);
})
.option('-x, --exactly <version>', 'specify the exact version', (value) => {
if (!semver.valid(value)) {
die(`${value.yellow} is not valid semantic version`, cBump);
}
return value.replace(/^[^0-9]*/, '');
});
/*---------- BUMP END ----------*/
/*---------- BUILD COMMAND ----------*/
const cBuild = program
.command('build')
.description('Build project, if environment is "development" runs watcher')
.option(
'-w, --watch [status]',
'exactly enable or disable running of watcher [on|off] (on)',
(value) => {
if (!/^(on|off)$/.test(value)) {
die(`Invalid value for --watcher option: ${value.yellow}`, cBuild);
}
return value === 'on';
},
undefined
)
.action(() => {
cBuild.running = true;
});
/*---------- BUILD END ----------*/
program.parse(process.argv);
setEnvironment();
log(`Environment: ${program.env.yellow}`);
/*---------- BUMP TASK ----------*/
const
bump = require('gulp-bump');
gulp.task('bump', (ignore) => {
log(`Current version is ${program.version().yellow}`);
const config = {};
if (cBump.exactly) {
config.version = cBump.exactly;
log(`Bump to v${config.version.yellow}`);
} else {
config.type = cBump.type;
log(`Bump ${config.type.yellow} version`);
}
gulp.src('./*.json')
.pipe(bump(config))
.pipe(gulp.dest('./'))
.on('end', () => {
log(`Done, new version is ${determineVersion().yellow}`);
});
});
/*---------- BUMP END ----------*/
/*---------- BUILD TASK ----------*/
if (cBuild.running) {
if (cBuild.watch === undefined) {
cBuild.watch = program.env === 'development';
}
log(`Watcher ${(cBuild.watch ? 'enabled' : 'disabled').yellow}`);
}
const
path = require('path'),
config = require('config'),
del = require('del'),
gutil = require('gulp-util'),
watch = require('gulp-watch'),
plumber = require('gulp-plumber'),
webpack = require('webpack');
const
dest = config.buildDir;
gulp.task('build:clean', () => del([dest]));
gulp.task('build:copy', ['build:clean'], (cb) => {
const srcGlobs = [
'./manifest.json'
];
gulp
.src(srcGlobs)
.pipe(watch(srcGlobs, {
events: ['add', 'change', 'unlink', 'ready'],
name: 'build:copy',
verbose: true
}))
.on('ready', function () {
if (cBuild.watch) {
cb();
} else {
setTimeout(() => this.close(), 10000); // TODO: починить вот этот ад
}
})
.pipe(gulp.dest(dest))
.on('end', () => {
if (!cBuild.watch) {
cb();
}
});
});
gulp.task('build:webpack', ['build:clean'], (cb) => {
const
compiler = webpack(require('./webpack.config')),
isWatch = cBuild.watch;
function listener(err, stats) {
if (err) {
if (isWatch) {
gutil.log(err);
gutil.log(err.stack);
} else {
throw err;
}
}
gutil.log(`[${'build:webpack'.cyan}]\n`, stats.toString({
colors: true,
hash: true,
timings: true,
version: true,
reasons: true,
errorDetails: true,
chunks: true,
assets: true,
source: true,
chunkOrigins: false,
cached: false,
modules: false,
chunkModules: false
}));
if (!isWatch) {
cb();
}
}
if (isWatch) {
compiler.watch({}, listener);
} else {
compiler.run(listener);
}
});
gulp.task('build', ['build:clean', 'build:copy', 'build:webpack']);
/*---------- BUILD END ----------*/