-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgulpfile.js
More file actions
546 lines (480 loc) · 17 KB
/
gulpfile.js
File metadata and controls
546 lines (480 loc) · 17 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/* Command line arguments */var argv = require('yargs').argv;
var g = this; /* Global variables */
g.ugly = argv.ugly || argv.u;
g.production = argv.production || argv.p;
g.blank = argv.test || argv.blank;
/*------------------------------------------------------------
* =Modules
*------------------------------------------------------------ */
/*============= Misc ============= */
var fs = require('fs');
var browserSyncModule = require('browser-sync');
var browserSync = browserSyncModule.create();
var merge = require('merge-stream');
//var es = require('event-stream');
var child_process = require('child_process');
var path = require('path');
/*============= Gulp operations ============= */
var gulp = require('gulp');
var gutil = require('gulp-util');
var gulpif = require('gulp-if');
var combiner = require('stream-combiner2');
var preprocess = require('gulp-preprocess');
/*============= Content operations ============= */
/* General */
var sourcemaps = require('gulp-sourcemaps'); /* sourcemaps */
var concat = require('gulp-concat'); /* sourcemaps */
//var insert = require('gulp-insert');
var replace = require('gulp-replace');
/* js */
var uglify = require('gulp-uglify'); /* sourcemaps */
/* scss */
var sass = require('gulp-sass'); /* sourcemaps */
/* css */
var stripCssComments = require('gulp-strip-css-comments');
var minifyCss = require('gulp-minify-css'); /* sourcemaps */
/* jade */
var jade = require('gulp-jade');
/* Images */
var imagemin = require('gulp-imagemin');
var debug = require('gulp-debug'); /* DEBUG */
/*-------------------------------------------------------------
* =Variables
*------------------------------------------------------------*/
/*============= Setting files ============= */
function getBackEndPath() {
/* Possible paths */
var posPaths = [
argv.be || argv.backend,
gulpSettings && gulpSettings.backEndPath,
'../be/',
'../LynxChan/src/be/'
];
var fileContent;
for (var i = 0; fileContent === undefined && i < posPaths.length; ++i) {
if (tryReadFileSync(posPaths[i]+'/settings/general.json')) {
return path.resolve(posPaths[i]);
}
}
/* Not found */
console.log('[getBackEndPath] Back-end not found, edit gulpSettings.json to either specify "backEndPath" or specify both "generalSettingsPath" and "languagePack.backEndJson" (the back-end\'s path can also be specified with the option --backend)');
}
/* gulpSettings.json keys:
* generalSettingsPath,
* languagePackPath: {
* frontEnd,
* backEnd
* },
* outputFolder,
* startCommand,
* reloadCommand */
var gulpSettings = JSON.parse(
tryReadFileSync('gulpSettings.json', {log: true}) ||
"{}");
var backEndPath = ((gulpSettings.languagePack &&
gulpSettings.languagePack.backEndJson) &&
gulpSettings.generalSettingsPath) ?
undefined : getBackEndPath();
var settings = JSON.parse(tryReadFileSync(
gulpSettings.generalSettingsPath ||
backEndPath+'/settings/general.json', {log: true}) ||
"{}");
var jadeLocals = JSON.parse(
tryReadFileSync('./src/templates/jadeLocals.json', {log: true}) ||
"{}");
var templateSettings = JSON.parse(
tryReadFileSync('./templateSettings.json', {log: true}));
var package = require('./package.json');
//==========================
var CWD = process.cwd(); /* Project's root (same place as gulpfile.js) */
var siteTitle = settings.siteTitle || "Undefined site title";
var url = {
protocol: settings.sll ? 'https' : 'http',
domain: !settings.address || settings.address === '127.0.0.1' ?
'localhost' : settings.address,
port: settings.port ? settings.port : '8080',
};
url.base = url.protocol+'://'+url.domain+':'+url.port;
url.baseStatic = url.protocol+'://'+'static.'+url.domain+':'+url.port;
//==========================
var themeFolder = argv.theme || argv.t || gulpSettings.themeFolder;
themeFolder = typeof(themeFolder) === 'string' ?
path.resolve(themeFolder) :
undefined;
console.info('[i]\tUsing '+(!themeFolder ? 'default theme.' :
'theme: '+path.basename(themeFolder)));
themeFolder = themeFolder || './src/static/scss/sass/dummyThemeFolder/';
//==========================
var languagePacks = {
fe: path.resolve((gulpSettings.languagePack &&
gulpSettings.languagePack.frontEndFolder) ||
'./src/res/languagePacks/default-en_US'), /* Default language pack */
be: path.resolve((gulpSettings.languagePack &&
gulpSettings.languagePack.backEndJson) ||
backEndPath+'/defaultLanguagePack.json')
};
console.info('[i]\tUsing language packs:\n\tBE:\t'+
languagePacks.be+'\n\tFE:\t'+languagePacks.fe+'/strings.json');
var lang = {
fe: require(languagePacks.fe+'/strings.json'),
be: require(languagePacks.be)
};
//==========================
/* Variables forwarded to jade by gulp */
jadeLocals = concatObjects(jadeLocals, {
siteTitle: siteTitle,
siteLicense: settings.siteLicense,
baseUrl: url.base,
baseStaticUrl: url.baseStatic,
lang: lang.fe,
l: lang.fe /* shorthand */
});
//==========================
var basePaths = {
source: 'src/',
build: argv.output || argv.o ? (argv.output || argv.o)+'/' : (
gulpSettings.outputFolder ? gulpSettings.outputFolder+'/' :
'./'),
};
var outPaths = {
js: basePaths.build+'static/js/',
css: basePaths.build+'static/css/',
html: basePaths.build+'templates/',
htmlStatic: basePaths.build+'static/',
png: basePaths.build+'templates/cmp/images/',
};
var paths = {
js: basePaths.source+'static/js/',
scss: basePaths.source+'static/scss/',
/* For watching */
scssExtras: basePaths.source+'static/scss/{cmp/,pages/,sass/,vendor/}',
css: basePaths.source+'static/css/',
scssThemeFolder: themeFolder, /* For watching */
html: basePaths.source+'templates/',
jade: basePaths.source+'templates/{cmp/,pages/}',
jadeExtras: basePaths.source+'templates/jade/', /* For watching */
jadeStatic: basePaths.source+'static/jade/',
png: basePaths.source+'templates/cmp/images/',
sourcemaps: {}, /* Filled below */
};
Object.keys(paths).forEach(function(path) {
paths.sourcemaps[path] = '../sourcemaps/';
});
paths.sourcemaps.js = '../'+paths.sourcemaps.js;
var filesRecur = {}; /* { js: src/static/** /*.js, ... } */
Object.keys(paths).forEach(function(ft) {
var ext = keepFirstWordFromCamelCase(ft);
filesRecur[ft] = paths[ft]+'**/*.'+ext;
});
var files = {}; /* { js: src/static/*.js, ... } */
Object.keys(paths).forEach(function(ft) {
var ext = keepFirstWordFromCamelCase(ft);
files[ft] = paths[ft]+'*.'+ext;
});
/*-------------------------------------------------------------
* =Tasks
*-------------------------------------------------------------*/
gulp.task('default', ['build']);
/* =Build
========================== */
gulp.task('build', ['js', 'html', 'css', 'otherFiles'], function() {
if (basePaths.build !== './') {
console.log('Built project to: '+basePaths.build);
}
});
gulp.task('otherFiles', function() {
var excludeTypes = [
'js', 'json', 'html', 'css', 'scss', 'jade', 'md',
].map(function(ft) { return '!'+basePaths.source+'**/*.'+ft; });
return gulp.src([basePaths.source+'**/*.*'].concat(excludeTypes))
.pipe(debug())
.pipe(gulp.dest(basePaths.build));
});
/* =JS =Javascript
------------------------------*/
var jsOutNames = [ 'pre', 'post', 'ext' ];
/* Allows settings concat order when needed */
var jsPaths = {
pre: [
paths.js+'pre/settings.js',
paths.js+'pre/head.js',
paths.js+'pre/boardI.js',
paths.js+'pre/threadI.js',
paths.js+'pre/postingI.js',
paths.js+'pre/*I.js',
paths.js+'pre/tail.js'
],
post: [
paths.js+'post/api.js',
paths.js+'post/*.js'
],
ext: [ paths.js+'ext/*.js' ]
};
function templateToEscapedString(template) {
var unescaped = tryReadFileSync(
basePaths.build+'templates/'+templateSettings[template], { log: true });
if(unescaped[0] === '\n') { /* Strip leading \n */
unescaped = unescaped.slice(1, unescaped.length);
}
/* Escape newlines for 'preprocess' */
return unescaped.replace(/\n/g, '\\n');
}
/* Requires 'jade' to be able to pass postCell/uploadCell html */
gulp.task('js', ['jade'], function() {
/* Remove moderation elements */
var postCellTemplate = templateToEscapedString('postCell')
.replace(/<(\w+) class="panelRange"[\s\S]*?<\/span>.*<\/\1>/, '')
.replace(/<(\w+) class="linkEdit"[\s\S]*?<\/\1>/, '');
var uploadCellTemplate = templateToEscapedString('uploadCell')
.replace(/<(\w+) class="divHash"[\s\S]*?<\/\1>/, '');
var jsPreprocessContext = {
DEBUG: !!g.production || undefined,
postCellTemplate: postCellTemplate,
uploadCellTemplate: uploadCellTemplate,
l: lang
};
var combined = [];
jsOutNames.forEach(function(name, i) {
combined[i] =
gulp.src(jsPaths[name])
.pipe(preprocess({context: jsPreprocessContext}))
.pipe(gulpif(!g.production, sourcemaps.init()))
.pipe(concat('../'+name+'.js'))
.pipe(gulpif(g.ugly, uglify()))
.pipe(gulpif(!g.production, sourcemaps.write(paths.sourcemaps.js)))
.pipe(gulp.dest(outPaths.js+name));
combined[i].on('error', console.error.bind(console));
});
return merge.apply(this, combined);
});
/*
------------------------------*/
gulp.task('moveHtml', function() {
gulp.src(filesRecur.html)
.pipe(gulp.dest(outPaths.html));
});
/* =Jade
------------------------------*/
var relativeRootDir = '../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../..';
var jadeRegex = {
languagePackInclude: {
match: /((^|\n)(\s+)?\binclude(:.*\b)? )(\$LANGUAGE_PACK)/g,
replace: '$1'+relativeRootDir+languagePacks.fe
}
};
gulp.task('jade', ['moveHtml', 'jadeStatic'], function() {
var combined = combiner.obj([
gulp.src(filesRecur.jade)
.pipe(replace(jadeRegex.languagePackInclude.match,
jadeRegex.languagePackInclude.replace))
.pipe(jade(
{ pretty: !g.ugly, data: jadeLocals }))
.pipe(gulp.dest(outPaths.html))
]);
combined.on('error', console.error.bind(console));
return combined;
});
gulp.task('jadeStatic', function() {
var combined = combiner.obj([
gulp.src(filesRecur.jadeStatic)
.pipe(replace(jadeRegex.languagePackInclude.match,
jadeRegex.languagePackInclude.replace))
.pipe(jade(
{ pretty: !g.ugly, data: jadeLocals }))
.pipe(gulp.dest(outPaths.htmlStatic))
]);
combined.on('error', console.error.bind(console));
return combined;
});
gulp.task('html', ['moveHtml', 'jade']);
/* =CSS =SCSS =SASS
------------------------------*/
/* eg.: the string '__thumbSize' will be replaced by '128' */
var sassContext = {};
Object.keys(settings).forEach(function(setting) {
sassContext['__'+setting] = settings[setting];
});
Object.keys(lang).forEach(function(str) {
sassContext['l.'+str] = settings[str];
});
var scssPreprocessedFile = paths.scss+'preprocessed.scss';
var scssMainFile = paths.scss+'main.scss';
gulp.task('css', function() {
var combined = combiner.obj([
merge(gulp.src(scssMainFile)
.pipe(preprocess({context: sassContext })),
gulp.src(['!'+paths.scssExtras+'*', '!'+scssPreprocessedFile,
'!'+scssMainFile, files.scss, files.css]))
.pipe(gulpif(!g.production, sourcemaps.init()))
.pipe(sass({
includePaths: [themeFolder],
style: (g.ugly ? 'compressed' : 'nested')})
.on('error', sass.logError))
.pipe(gulpif(g.production, stripCssComments()))
.pipe(gulpif(g.ugly, minifyCss()))
.pipe(gulpif(!g.production, sourcemaps.write(paths.sourcemaps.css)))
.pipe(gulp.dest(outPaths.css))
.pipe(browserSync.stream())
]);
combined.on('error', console.error.bind(console));
return combined;
});
/* =Images
------------------------------*/
gulp.task('images', function() {
var combined = combiner.obj([
gulp.src(files.png)
.pipe(gulpif(g.production), imagemin())
.pipe(gulp.dest(outPaths.png))
]);
combined.on('error', console.error.bind(console));
return combined;
});
/*============ Utility ============== */
/* =Watching, =Sync, =Browser-Sync
========================== */
gulp.task('sync', ['browser-sync']);
gulp.task('browser-sync', ['build'], function() {
var browserSyncPort = argv.syncport || 3000;
url.base = url.protocol+'://'+url.domain+':'+browserSyncPort;
if (gulpSettings.startCommand) {
child_process.spawnSync();
child_process.execSync(gulpSettings.startCommand);
}
browserSync.init({
proxy: {
target: url.domain+':'+url.port,
port: browserSyncPort
}
});
gulp.watch(
[filesRecur.scss, filesRecur.scssExtras, filesRecur.css,
filesRecur.scssThemeFolder],
['css'])
.on('change', serverRefresh);
gulp.watch(
[filesRecur.js],
['js', 'browserReloadJs']);
gulp.watch(
[filesRecur.jade, filesRecur.jadeExtras, filesRecur.jadeStatic],
['html', 'jade', 'jadeStatic', 'browserReload']);
gulp.watch(
files.html,
['html', 'browserReload']);
});
function serverRefresh() {
if (gulpSettings.reloadCommand) {
child_process.spawnSync();
child_process.execSync(gulpSettings.reloadCommand);
console.log("Reload: \""+gulpSettings.reloadCommand+"\"");
browserSync.reload();
}
}
gulp.task('browserReload', ['html'], function() {
serverRefresh();
});
gulp.task('browserReloadJs', ['js'], function() {
serverRefresh();
});
/* =Remove built files
========================== */
gulp.task('clean', ['clear']);
gulp.task('clear', function() {
Object.keys(outPaths).forEach(function(outPath) {
deleteFolderRecursive(
/* Strip the trailing '/' */
(outPaths[outPath]).slice(0, outPaths[outPath].length - 1),
{log: true});
});
});
/* =Help
========================== */
gulp.task('help', function() {
console.log([
package.name+'\'s gulp tasks and options',
'--------------------------------------------------',
'[all]',
'\t--production\tBuild for deployment as opposed to development version (no sourcemaps, ...)',
'\t--ugly\t\tMinify code',
'\t--output, -o\tFolder in which to output the built files, overrides the value configured in gulpSettings.json',
'\t--backend, --be\tBack-end\'s path, overrides the value configured in gulpSettings.json',
'[default, build]\tProcess all necessarily files',
'[css, scss]',
'\t--theme, -t\tPath to a folder containing the theme.scss file to use instead of the default one, overrides the value configured in gulpSettings.json',
'[sync, browser-sync]\tWatch files and reload browser automatically with browser-sync, default port is 3000',
'\t--syncport\tBrowser-sync port',
'[clear, clean]\tDelete built files',
'\t--blank\t\tDon\'t delete, only print name of files that would be deleted'
].join('\n'));
});
/*-------------------------------------------------------------
* =Helper functions
*------------------------------------------------------------*/
function commentString(string, fileType) {
var tags = {
html: ['<!--', '-->'],
css: ['/*', '*/'],
js: ['/*', '*/']
};
return tags[fileType][0]+' '+string+' '+tags[fileType][1];
}
function concatObjects(obj1, obj2) {
var obj = {};
[obj1, obj2].forEach(function(object) {
Object.keys(object || {}).forEach(function(key) {
obj[key] = object[key];
});
});
return obj;
}
function keepFirstWordFromCamelCase(camel) {
function isLowerCase(ch) {
return ch.toLowerCase() === ch;
}
var firstWord = '';
for (var i = 0; i < camel.length && isLowerCase(camel[i]); ++i) {
firstWord = firstWord+camel[i];
}
return firstWord;
}
/*============= File operations ============= */
/* options: log (false), encoding ('utf-8') */
function tryReadFileSync(fileName, options) {
options = options || {};
if (options.encoding === undefined) { options.encoding = 'utf-8'; }
try {
return fileName && fs.readFileSync(fileName, 'utf8');
} catch(e) {
if (e instanceof Error && e.code === "ENOENT") { /* File not found */
if (options.log) {
console.log(
"[tryReadFileSync] No '"+fileName+"' file found.");
}
return undefined;
} else { throw e; }
}
}
function deleteFolderRecursive (path, options) {
options = options || {};
var files = [];
if( fs.existsSync(path) ) {
files = fs.readdirSync(path);
files.forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath, options);
} else { // delete file
if (!g.blank) {
fs.unlinkSync(curPath);
}
if (options.log) {
console.log("[rm -r "+path+"] Removed "+"\""+
curPath+"\"");
}
}
});
if (!g.blank) {
fs.rmdirSync(path);
}
}
}