Bug 30237: Reference new WELCOME notice
[koha.git] / gulpfile.js
1 /* eslint-env node */
2 /* eslint no-console:"off" */
3
4 const { dest, parallel, series, src, watch } = require('gulp');
5
6 const child_process = require('child_process');
7 const fs = require('fs');
8 const os = require('os');
9 const path = require('path');
10 const util = require('util');
11
12 const sass = require("gulp-sass");
13 const cssnano = require("gulp-cssnano");
14 const rtlcss = require('gulp-rtlcss');
15 const sourcemaps = require('gulp-sourcemaps');
16 const autoprefixer = require('gulp-autoprefixer');
17 const concatPo = require('gulp-concat-po');
18 const exec = require('gulp-exec');
19 const merge = require('merge-stream');
20 const through2 = require('through2');
21 const Vinyl = require('vinyl');
22 const args = require('minimist')(process.argv.slice(2));
23 const rename = require('gulp-rename');
24
25 const STAFF_JS_BASE = "koha-tmpl/intranet-tmpl/prog/js";
26 const STAFF_CSS_BASE = "koha-tmpl/intranet-tmpl/prog/css";
27 const OPAC_JS_BASE = "koha-tmpl/opac-tmpl/bootstrap/js";
28 const OPAC_CSS_BASE = "koha-tmpl/opac-tmpl/bootstrap/css";
29
30 if (args.view == "opac") {
31     var css_base = OPAC_CSS_BASE;
32     var js_base = OPAC_JS_BASE;
33 } else {
34     var css_base = STAFF_CSS_BASE;
35     var js_base = STAFF_JS_BASE;
36 }
37
38 var sassOptions = {
39     errLogToConsole: true,
40     precision: 3
41 }
42
43 // CSS processing for development
44 function css() {
45     var stream = src(css_base + "/src/**/*.scss")
46         .pipe(sourcemaps.init())
47         .pipe(sass(sassOptions).on('error', sass.logError))
48         .pipe(autoprefixer())
49         .pipe(dest(css_base));
50
51     if (args.view == "opac") {
52         stream = stream
53             .pipe(rtlcss())
54             .pipe(rename({
55                 suffix: '-rtl'
56             })) // Append "-rtl" to the filename.
57             .pipe(dest(css_base));
58     }
59
60     stream = stream.pipe(sourcemaps.write('./maps'))
61         .pipe(dest(css_base));
62
63     return stream;
64
65 }
66
67 // CSS processing for production
68 function build() {
69     var stream = src(css_base + "/src/**/*.scss")
70         .pipe(sass(sassOptions).on('error', sass.logError))
71         .pipe(autoprefixer())
72         .pipe(cssnano({
73             zindex: false
74         }))
75         .pipe(dest(css_base));
76
77     if( args.view == "opac" ){
78         stream = stream.pipe(rtlcss())
79         .pipe(rename({
80             suffix: '-rtl'
81         })) // Append "-rtl" to the filename.
82         .pipe(dest(css_base));
83     }
84
85     return stream;
86 }
87
88 const poTasks = {
89     'marc-MARC21': {
90         extract: po_extract_marc_marc21,
91         create: po_create_marc_marc21,
92         update: po_update_marc_marc21,
93     },
94     'marc-UNIMARC': {
95         extract: po_extract_marc_unimarc,
96         create: po_create_marc_unimarc,
97         update: po_update_marc_unimarc,
98     },
99     'staff-prog': {
100         extract: po_extract_staff,
101         create: po_create_staff,
102         update: po_update_staff,
103     },
104     'opac-bootstrap': {
105         extract: po_extract_opac,
106         create: po_create_opac,
107         update: po_update_opac,
108     },
109     'pref': {
110         extract: po_extract_pref,
111         create: po_create_pref,
112         update: po_update_pref,
113     },
114     'messages': {
115         extract: po_extract_messages,
116         create: po_create_messages,
117         update: po_update_messages,
118     },
119     'messages-js': {
120         extract: po_extract_messages_js,
121         create: po_create_messages_js,
122         update: po_update_messages_js,
123     },
124     'installer': {
125         extract: po_extract_installer,
126         create: po_create_installer,
127         update: po_update_installer,
128     },
129     'installer-MARC21': {
130         extract: po_extract_installer_marc21,
131         create: po_create_installer_marc21,
132         update: po_update_installer_marc21,
133     },
134 };
135
136 const poTypes = Object.keys(poTasks);
137
138 function po_extract_marc (type) {
139     return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
140         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', `Koha-marc-${type}.pot`))
141         .pipe(dest('misc/translator'))
142 }
143
144 function po_extract_marc_marc21 ()  { return po_extract_marc('MARC21') }
145 function po_extract_marc_unimarc () { return po_extract_marc('UNIMARC') }
146
147 function po_extract_staff () {
148     const globs = [
149         'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
150         'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
151         'koha-tmpl/intranet-tmpl/prog/en/xslt/*.xsl',
152         '!koha-tmpl/intranet-tmpl/prog/en/**/*MARC21*',
153         '!koha-tmpl/intranet-tmpl/prog/en/**/*UNIMARC*',
154         '!koha-tmpl/intranet-tmpl/prog/en/**/*marc21*',
155         '!koha-tmpl/intranet-tmpl/prog/en/**/*unimarc*',
156     ];
157
158     return src(globs, { read: false, nocase: true })
159         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-staff-prog.pot'))
160         .pipe(dest('misc/translator'))
161 }
162
163 function po_extract_opac () {
164     const globs = [
165         'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
166         'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
167         'koha-tmpl/opac-tmpl/bootstrap/en/xslt/*.xsl',
168         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*MARC21*',
169         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*UNIMARC*',
170         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*marc21*',
171         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*unimarc*',
172     ];
173
174     return src(globs, { read: false, nocase: true })
175         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-opac-bootstrap.pot'))
176         .pipe(dest('misc/translator'))
177 }
178
179 const xgettext_options = '--from-code=UTF-8 --package-name Koha '
180     + '--package-version= -k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 '
181     + '-k__p:1c,2 -k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ '
182     + '-kN__n:1,2 -kN__p:1c,2 -kN__np:1c,2,3 --force-po';
183
184 function po_extract_messages_js () {
185     const globs = [
186         'koha-tmpl/intranet-tmpl/prog/js/**/*.js',
187         'koha-tmpl/opac-tmpl/bootstrap/js/**/*.js',
188     ];
189
190     return src(globs, { read: false, nocase: true })
191         .pipe(xgettext(`xgettext -L JavaScript ${xgettext_options}`, 'Koha-messages-js.pot'))
192         .pipe(dest('misc/translator'))
193 }
194
195 function po_extract_messages () {
196     const perlStream = src(['**/*.pl', '**/*.pm'], { read: false, nocase: true })
197         .pipe(xgettext(`xgettext -L Perl ${xgettext_options}`, 'Koha-perl.pot'))
198
199     const ttStream = src([
200             'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
201             'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
202             'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
203             'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
204         ], { read: false, nocase: true })
205         .pipe(xgettext('misc/translator/xgettext-tt2 --from-code=UTF-8', 'Koha-tt.pot'))
206
207     const headers = {
208         'Project-Id-Version': 'Koha',
209         'Content-Type': 'text/plain; charset=UTF-8',
210     };
211
212     return merge(perlStream, ttStream)
213         .pipe(concatPo('Koha-messages.pot', { headers }))
214         .pipe(dest('misc/translator'))
215 }
216
217 function po_extract_pref () {
218     return src('koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref', { read: false })
219         .pipe(xgettext('misc/translator/xgettext-pref', 'Koha-pref.pot'))
220         .pipe(dest('misc/translator'))
221 }
222
223 function po_extract_installer () {
224     const globs = [
225         'installer/data/mysql/en/mandatory/*.yml',
226         'installer/data/mysql/en/optional/*.yml',
227     ];
228
229     return src(globs, { read: false, nocase: true })
230         .pipe(xgettext('misc/translator/xgettext-installer', 'Koha-installer.pot'))
231         .pipe(dest('misc/translator'))
232 }
233
234 function po_extract_installer_marc (type) {
235     const globs = `installer/data/mysql/en/marcflavour/${type}/**/*.yml`;
236
237     return src(globs, { read: false, nocase: true })
238         .pipe(xgettext('misc/translator/xgettext-installer', `Koha-installer-${type}.pot`))
239         .pipe(dest('misc/translator'))
240 }
241
242 function po_extract_installer_marc21 ()  { return po_extract_installer_marc('MARC21') }
243
244 function po_create_type (type) {
245     const access = util.promisify(fs.access);
246     const exec = util.promisify(child_process.exec);
247
248     const languages = getLanguages();
249     const promises = [];
250     for (const language of languages) {
251         const locale = language.split('-').filter(s => s.length !== 4).join('_');
252         const po = `misc/translator/po/${language}-${type}.po`;
253         const pot = `misc/translator/Koha-${type}.pot`;
254
255         const promise = access(po)
256             .catch(() => exec(`msginit -o ${po} -i ${pot} -l ${locale} --no-translator`))
257         promises.push(promise);
258     }
259
260     return Promise.all(promises);
261 }
262
263 function po_create_marc_marc21 ()       { return po_create_type('marc-MARC21') }
264 function po_create_marc_unimarc ()      { return po_create_type('marc-UNIMARC') }
265 function po_create_staff ()             { return po_create_type('staff-prog') }
266 function po_create_opac ()              { return po_create_type('opac-bootstrap') }
267 function po_create_pref ()              { return po_create_type('pref') }
268 function po_create_messages ()          { return po_create_type('messages') }
269 function po_create_messages_js ()       { return po_create_type('messages-js') }
270 function po_create_installer ()         { return po_create_type('installer') }
271 function po_create_installer_marc21 ()  { return po_create_type('installer-MARC21') }
272
273 function po_update_type (type) {
274     const msgmerge_opts = '--backup=off --quiet --sort-output --update';
275     const cmd = `msgmerge ${msgmerge_opts} <%= file.path %> misc/translator/Koha-${type}.pot`;
276     const languages = getLanguages();
277     const globs = languages.map(language => `misc/translator/po/${language}-${type}.po`);
278
279     return src(globs)
280         .pipe(exec(cmd, { continueOnError: true }))
281         .pipe(exec.reporter({ err: false, stdout: false }))
282 }
283
284 function po_update_marc_marc21 ()       { return po_update_type('marc-MARC21') }
285 function po_update_marc_unimarc ()      { return po_update_type('marc-UNIMARC') }
286 function po_update_staff ()             { return po_update_type('staff-prog') }
287 function po_update_opac ()              { return po_update_type('opac-bootstrap') }
288 function po_update_pref ()              { return po_update_type('pref') }
289 function po_update_messages ()          { return po_update_type('messages') }
290 function po_update_messages_js ()       { return po_update_type('messages-js') }
291 function po_update_installer ()         { return po_update_type('installer') }
292 function po_update_installer_marc21 ()  { return po_update_type('installer-MARC21') }
293
294 /**
295  * Gulp plugin that executes xgettext-like command `cmd` on all files given as
296  * input, and then outputs the result as a POT file named `filename`.
297  * `cmd` should accept -o and -f options
298  */
299 function xgettext (cmd, filename) {
300     const filenames = [];
301
302     function transform (file, encoding, callback) {
303         filenames.push(path.relative(file.cwd, file.path));
304         callback();
305     }
306
307     function flush (callback) {
308         fs.mkdtemp(path.join(os.tmpdir(), 'koha-'), (err, folder) => {
309             const outputFilename = path.join(folder, filename);
310             const filesFilename = path.join(folder, 'files');
311             fs.writeFile(filesFilename, filenames.join(os.EOL), err => {
312                 if (err) return callback(err);
313
314                 const command = `${cmd} -o ${outputFilename} -f ${filesFilename}`;
315                 child_process.exec(command, err => {
316                     if (err) return callback(err);
317
318                     fs.readFile(outputFilename, (err, data) => {
319                         if (err) return callback(err);
320
321                         const file = new Vinyl();
322                         file.path = path.join(file.base, filename);
323                         file.contents = data;
324                         callback(null, file);
325                     });
326                 });
327             });
328         })
329     }
330
331     return through2.obj(transform, flush);
332 }
333
334 /**
335  * Return languages selected for PO-related tasks
336  *
337  * This can be either languages given on command-line with --lang option, or
338  * all the languages found in misc/translator/po otherwise
339  */
340 function getLanguages () {
341     if (Array.isArray(args.lang)) {
342         return args.lang;
343     }
344
345     if (args.lang) {
346         return [args.lang];
347     }
348
349     const filenames = fs.readdirSync('misc/translator/po')
350         .filter(filename => filename.endsWith('.po'))
351         .filter(filename => !filename.startsWith('.'))
352
353     const re = new RegExp('-(' + poTypes.join('|') + ')\.po$');
354     languages = filenames.map(filename => filename.replace(re, ''))
355
356     return Array.from(new Set(languages));
357 }
358
359 exports.build = build;
360 exports.css = css;
361
362 exports['po:create'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].create)));
363 exports['po:update'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].update)));
364 exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));
365
366 exports.default = function () {
367     watch(css_base + "/src/**/*.scss", series('css'));
368 }