forked from cakephp/app
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgulpfile.js
326 lines (303 loc) · 8.4 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
const prefix = require('autoprefixer');
const beep = require('beepbeep');
const minify = require('cssnano');
const del = require('del');
const fancylog = require('fancy-log');
const gulp = require('gulp');
const cleanCss = require('gulp-clean-css');
const concat = require('gulp-concat');
const expectFile = require('gulp-expect-file');
const flatmap = require('gulp-flatmap');
const jshint = require('gulp-jshint');
const sass = require('gulp-sass')(require('sass'));
const plumber = require('gulp-plumber');
const postcss = require('gulp-postcss');
const postcssImport = require('postcss-import');
const rename = require('gulp-rename');
const replace = require('gulp-replace');
const sourcemaps = require('gulp-sourcemaps');
const uglify = require('gulp-uglify');
const wait = require('gulp-wait');
const mergeStream = require('merge-stream');
const path = require('path');
const streamSwitch = require('stream-switch');
const through = require('through2');
/**
* The font files to install
* @type array
*/
const fontFilesInstall = [
{
src: [
'./node_modules/@fortawesome/fontawesome-free/webfonts/*.*'
],
dest: './webroot/font'
},
];
/**
* Fonts to clean
* @type array
*/
const fontsToClean = [
];
/**
* Styles to process
* @type Object
*/
const stylesToProcess = {
app: [
'./node_modules/select2/dist/css/select2.min.css',
'./node_modules/select2/dist/css/select2.css',
'./node_modules/select2-bootstrap4-theme/dist/select2-bootstrap4.min.css',
'./node_modules/tempusdominus-bootstrap-4/build/css/tempusdominus-bootstrap-4.css',
'./assets/app/scss/main.scss'
]
};
/**
* Scripts to process
* @type Object
*/
const scriptsToProcess = {
app: [
'./node_modules/jquery/dist/jquery.js',
'./node_modules/popper.js/dist/umd/popper.js',
'./node_modules/bootstrap/dist/js/bootstrap.js',
'./node_modules/moment/min/moment.min.js',
'./node_modules/moment-timezone/builds/moment-timezone-with-data.min.js',
'./node_modules/jsrender/jsrender.js',
'./node_modules/tempusdominus-core/build/js/tempusdominus-core.js',
'./node_modules/tempusdominus-bootstrap-4/build/js/tempusdominus-bootstrap-4.js',
'./node_modules/select2/dist/js/select2.full.js',
'./node_modules/inputmask/dist/jquery.inputmask.js',
'./node_modules/jquery-countdown/dist/jquery.countdown.min.js',
'./node_modules/@fortawesome/fontawesome-free/js/all.min.js',
'./assets/app/js/*.js'
]
};
/**
* Script to Lint
* @type Object
*/
const scriptsToLint = {
app: [
'./assets/app/js/*.js'
]
};
/**
* CacheBuster Presets
* @type Object
*/
const cb = {
js: false,
css: false
};
/**
* OnError
* @param object error The error
*/
function onError(error) {
handleError.call(this, 'error', error);
}
/**
* OnWarning
* @param object warning The warning
*/
function onWarning(warning) {
handleError.call(this, 'warning', warning);
}
/**
* handleError
* @param string level The error level error|warning
* @param object error The error
*/
function handleError(level, error) {
beep(1);
fancylog.error(error.toString);
if (level === 'error') {
process.exit(1);
}
this.emit('end');
}
/**
* Clean Fonts - Remove the fonts
* @param Function done callback to signal completion
*/
function cleanFonts(done) {
del.sync(fontsToClean);
done();
}
/**
* Install the specified fonts
* @return stream The stream
*/
function installFonts() {
return mergeStream(fontFilesInstall.map(function(config) {
return gulp.src(config.src)
.pipe(wait(1000))
.pipe(plumber(onError))
.pipe(gulp.dest(config.dest));
}));
}
/**
* Build Styles
* @param Function done callback to signal completion
* @return stream The stream
*/
function buildStyles(done) {
var streams = [];
for (var name in stylesToProcess) {
streams.push(buildStyle(name, stylesToProcess[name]));
}
if (!streams) {
done();
}
return mergeStream(streams);
}
/**
* Build Style File
* @param string name The name of the file
* @param array files The files to process
* @return stream The stream
*/
function buildStyle(name, files) {
return gulp.src(files)
.pipe(expectFile.real({verbose: true}, files.map(path.normalize)))
.pipe(flatmap(function(stream, file) {
if (file.extname === '.css') {
return stream;
}
return stream
.pipe(streamSwitch(function(file){
return file.extname;
}, {
'.scss': sass()
}))
.pipe(plumber(onError));
}))
.pipe(concat(name + '.css'))
.pipe(postcss([
postcssImport(),
prefix({
cascade: true,
remove: true
})
]))
.pipe(gulp.dest('./webroot/css'))
.pipe(rename({suffix: '.min'}))
.pipe(cleanCss({advanced: false}))
.pipe(postcss([
minify({
discardComments: {
removeAll: true
}
})
]))
.pipe(gulp.dest('./webroot/css'))
.pipe(through.obj(function(file, enc, callback) {
cb.css = true;
callback(null, file);
}));
}
/**
* Build Scripts
* @param Function done callback to signal completion
* @return stream The stream
*/
function buildScripts(done) {
var streams = [];
for (var name in scriptsToProcess) {
streams.push(buildScript(name, scriptsToProcess[name]));
}
if (!streams) {
done();
}
return mergeStream(streams);
}
/**
* Build Script File
* @param string name The name of the file
* @param array files The files to process
* @return stream The stream
*/
function buildScript(name, files) {
return gulp.src(files)
.pipe(expectFile.real({verbose: true}, files.map(path.normalize)))
.pipe(concat(name + '.js'))
.pipe(gulp.dest('./webroot/js'))
.pipe(rename({suffix: '.min'}))
.pipe(uglify({compress: false}))
.pipe(plumber(onError))
.pipe(gulp.dest('./webroot/js'))
.pipe(through.obj(function(file, enc, callback) {
cb.js = true;
callback(null, file);
}));
}
/**
* Lint the scripts
* @param Function done callback to signal completion
* @return stream The stream
*/
function lintScripts(done) {
var streams = [];
for (var name in scriptsToLint) {
streams.push(lintFiles(name, scriptsToLint[name]));
}
if (!streams) {
done();
}
return mergeStream(streams);
}
/**
* Lint Script File
* @param string name The name of the file
* @param array files The files to lint
* @return stream The stream
*/
function lintFiles(name, files) {
return gulp.src(files)
.pipe(expectFile.real({verbose: true}, files.map(path.normalize)))
.pipe(jshint())
.pipe(jshint.reporter('jshint-stylish'));
}
/**
* CacheBuster
* @param Function done callback to signal completion
*/
function cacheBuster(done) {
if (cb.js || cb.css) {
var config = gulp.src(['./config/app.php']);
var time = Math.round(+new Date()/1000) + 60;
if (cb.js) {
config.pipe(replace(/'jsCB' => '(.*)',?\n/, "'jsCB' => '" + time + "',\n"));
}
if (cb.css) {
config.pipe(replace(/'cssCB' => '(.*)',?\n/, "'cssCB' => '" + time + "',\n"))
}
config.pipe(gulp.dest('./config/'));
}
cb.js = false;
cb.css = false;
done();
}
/**
* Watch the less
* @param Function done callback to signal completion
*/
function watchSass(done) {
gulp.watch('./assets/**/*.scss', { usePolling: true }, gulp.series(exports.styles, cacheBuster));
done();
}
/**
* Watch the scripts
* @param Function done callback to signal completion
*/
function watchScript(done) {
gulp.watch('./assets/**/*.js', { usePolling: true }, gulp.series(exports.scripts, cacheBuster));
done();
}
exports.default = gulp.series(lintScripts, gulp.parallel(gulp.series(cleanFonts, installFonts), buildScripts, buildStyles), cacheBuster);
exports.styles = gulp.series(buildStyles, cacheBuster);
exports.scripts = gulp.series(buildScripts, cacheBuster);
exports.fonts = gulp.series(cleanFonts, installFonts);
exports.watch = gulp.series(exports.default, gulp.parallel(watchSass, watchScript));