-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
147 lines (125 loc) · 4.48 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
const { src, dest, parallel, series } = require('gulp');
const cleanCSS = require('gulp-clean-css');
const sourcemaps = require('gulp-sourcemaps');
const eslint = require('gulp-eslint');
const minify = require('gulp-minify');
const gulpIf = require('gulp-if');
const browserify = require('browserify');
const tap = require('gulp-tap');
const buffer = require('gulp-buffer');
const through = require('through2');
const esprima = require('esprima');
const PO = require('pofile');
const rename = require('gulp-rename');
const { findTranslationCalls, contract } = require('./localization-util');
const isDevEnvironment = process.env.NODE_ENV === 'dev';
if (isDevEnvironment) {
console.info('❗ Development environment detected.');
}
function html () {
return src('resources/public/*.html')
.pipe(dest('target/classes/public'));
}
function css () {
return src('resources/public/*.css')
.pipe(gulpIf(isDevEnvironment, sourcemaps.init()))
.pipe(cleanCSS())
.pipe(gulpIf(isDevEnvironment, sourcemaps.write()))
.pipe(dest('target/classes/public'));
}
function minJS () {
return src('resources/public/*.min.js')
.pipe(dest('target/classes/public'));
}
// NOTE ([ ]): pattern for all non-minified .js files
const SOURCE_FILES = [
'resources/public/**/*.js',
'!resources/public/**/*.min.js',
'!resources/public/**/*-min.js'
];
function lint() {
return src(SOURCE_FILES)
.pipe(eslint())
.pipe(eslint.result((e) => {
e.messages.forEach((m) => {
console.log('Message in', e.filePath, ':')
console.log(m)
});
}))
.pipe(eslint.failAfterError());
}
function translate(pofile) {
return through.obj(function(file, enc, callback) {
if (file.isBuffer()) {
PO.load(pofile, (error, pofile) => {
if (error) {
callback(error);
} else {
let contents = file.contents.toString();
const script = esprima.parseScript(contents, { range: true });
const translationCalls = script.body
.map(findTranslationCalls)
.flat()
.sort((a, b) => a.range.start - b.range.start);
let offset = 0;
for (const call of translationCalls) {
const [argument] = call.arguments;
const [start, end] = call.range;
const length = end - start;
const original = contract(contents.substring(...argument.range.map(p => p + offset)), 1);
const quote = contents[argument.range[0] + offset];
const item = pofile.items.find(i => i.msgid === original);
const replaceContent = (item && item.msgstr[0]) || original;
const replace = quote + replaceContent.replace(new RegExp(`([^\\\\])([${quote}])`, 'g'), '$1\\$2') + quote;
// just in case something goes wrong
if (contents.substring(start + offset, start + offset + 2) !== '__') {
callback(new Error('Translation offset drift'));
}
contents = contents.substring(0, offset + start) + replace + contents.substring(offset + end);
offset += replace.length - length;
}
file.contents = Buffer.from(contents);
callback(null, file);
}
});
} else {
callback(new Error('Expected buffer'));
}
});
}
function srcJS () {
return src(['po/*.po'])
.pipe(through.obj(function(file, enc, callback) {
const codeIndex = file.basename.lastIndexOf('_');
const extIndex = file.basename.lastIndexOf('.');
const langcode = codeIndex === -1
? ''
: file.basename.substring(codeIndex, extIndex === -1
? file.basename.length
: extIndex);
src([...SOURCE_FILES, '!resources/public/include/**/*.js', '!resources/public/profile/**/*.js'], { read: false })
.pipe(tap(file => {
file.contents = browserify(file.path, { debug: isDevEnvironment })
.bundle();
}))
.pipe(buffer())
.pipe(translate(file.path))
.pipe(gulpIf(isDevEnvironment, sourcemaps.init({ loadMaps: true })))
.pipe(minify({
ext: {
src: '.src.js',
min: '.js'
}
}))
.pipe(gulpIf(isDevEnvironment, sourcemaps.write()))
.pipe(rename({ suffix: langcode }))
.pipe(dest('target/classes/public'))
.once('end', callback);
}));
}
exports.html = html;
exports.css = css;
exports.minJS = minJS;
exports.srcJS = srcJS;
exports.lint = lint;
exports.default = parallel(html, css, minJS, series(lint, srcJS));