Bug 17229: Check if patron is expired in CanItemBeReserved
[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-NORMARC': {
95         extract: po_extract_marc_normarc,
96         create: po_create_marc_normarc,
97         update: po_update_marc_normarc,
98     },
99     'marc-UNIMARC': {
100         extract: po_extract_marc_unimarc,
101         create: po_create_marc_unimarc,
102         update: po_update_marc_unimarc,
103     },
104     'staff-prog': {
105         extract: po_extract_staff,
106         create: po_create_staff,
107         update: po_update_staff,
108     },
109     'opac-bootstrap': {
110         extract: po_extract_opac,
111         create: po_create_opac,
112         update: po_update_opac,
113     },
114     'pref': {
115         extract: po_extract_pref,
116         create: po_create_pref,
117         update: po_update_pref,
118     },
119     'messages': {
120         extract: po_extract_messages,
121         create: po_create_messages,
122         update: po_update_messages,
123     },
124     'messages-js': {
125         extract: po_extract_messages_js,
126         create: po_create_messages_js,
127         update: po_update_messages_js,
128     },
129     'installer': {
130         extract: po_extract_installer,
131         create: po_create_installer,
132         update: po_update_installer,
133     },
134     'installer-MARC21': {
135         extract: po_extract_installer_marc21,
136         create: po_create_installer_marc21,
137         update: po_update_installer_marc21,
138     },
139 };
140
141 const poTypes = Object.keys(poTasks);
142
143 function po_extract_marc (type) {
144     return src(`koha-tmpl/*-tmpl/*/en/**/*${type}*`, { read: false, nocase: true })
145         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', `Koha-marc-${type}.pot`))
146         .pipe(dest('misc/translator'))
147 }
148
149 function po_extract_marc_marc21 ()  { return po_extract_marc('MARC21') }
150 function po_extract_marc_normarc () { return po_extract_marc('NORMARC') }
151 function po_extract_marc_unimarc () { return po_extract_marc('UNIMARC') }
152
153 function po_extract_staff () {
154     const globs = [
155         'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
156         'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
157         'koha-tmpl/intranet-tmpl/prog/en/xslt/*.xsl',
158         'koha-tmpl/intranet-tmpl/prog/en/columns.def',
159         '!koha-tmpl/intranet-tmpl/prog/en/**/*MARC21*',
160         '!koha-tmpl/intranet-tmpl/prog/en/**/*NORMARC*',
161         '!koha-tmpl/intranet-tmpl/prog/en/**/*UNIMARC*',
162         '!koha-tmpl/intranet-tmpl/prog/en/**/*marc21*',
163         '!koha-tmpl/intranet-tmpl/prog/en/**/*normarc*',
164         '!koha-tmpl/intranet-tmpl/prog/en/**/*unimarc*',
165     ];
166
167     return src(globs, { read: false, nocase: true })
168         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-staff-prog.pot'))
169         .pipe(dest('misc/translator'))
170 }
171
172 function po_extract_opac () {
173     const globs = [
174         'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
175         'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
176         'koha-tmpl/opac-tmpl/bootstrap/en/xslt/*.xsl',
177         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*MARC21*',
178         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*NORMARC*',
179         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*UNIMARC*',
180         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*marc21*',
181         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*normarc*',
182         '!koha-tmpl/opac-tmpl/bootstrap/en/**/*unimarc*',
183     ];
184
185     return src(globs, { read: false, nocase: true })
186         .pipe(xgettext('misc/translator/xgettext.pl --charset=UTF-8 -s', 'Koha-opac-bootstrap.pot'))
187         .pipe(dest('misc/translator'))
188 }
189
190 const xgettext_options = '--from-code=UTF-8 --package-name Koha '
191     + '--package-version= -k -k__ -k__x -k__n:1,2 -k__nx:1,2 -k__xn:1,2 '
192     + '-k__p:1c,2 -k__px:1c,2 -k__np:1c,2,3 -k__npx:1c,2,3 -kN__ '
193     + '-kN__n:1,2 -kN__p:1c,2 -kN__np:1c,2,3 --force-po';
194
195 function po_extract_messages_js () {
196     const globs = [
197         'koha-tmpl/intranet-tmpl/prog/js/**/*.js',
198         'koha-tmpl/opac-tmpl/bootstrap/js/**/*.js',
199     ];
200
201     return src(globs, { read: false, nocase: true })
202         .pipe(xgettext(`xgettext -L JavaScript ${xgettext_options}`, 'Koha-messages-js.pot'))
203         .pipe(dest('misc/translator'))
204 }
205
206 function po_extract_messages () {
207     const perlStream = src(['**/*.pl', '**/*.pm'], { read: false, nocase: true })
208         .pipe(xgettext(`xgettext -L Perl ${xgettext_options}`, 'Koha-perl.pot'))
209
210     const ttStream = src([
211             'koha-tmpl/intranet-tmpl/prog/en/**/*.tt',
212             'koha-tmpl/intranet-tmpl/prog/en/**/*.inc',
213             'koha-tmpl/opac-tmpl/bootstrap/en/**/*.tt',
214             'koha-tmpl/opac-tmpl/bootstrap/en/**/*.inc',
215         ], { read: false, nocase: true })
216         .pipe(xgettext('misc/translator/xgettext-tt2 --from-code=UTF-8', 'Koha-tt.pot'))
217
218     const headers = {
219         'Project-Id-Version': 'Koha',
220         'Content-Type': 'text/plain; charset=UTF-8',
221     };
222
223     return merge(perlStream, ttStream)
224         .pipe(concatPo('Koha-messages.pot', { headers }))
225         .pipe(dest('misc/translator'))
226 }
227
228 function po_extract_pref () {
229     return src('koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/*.pref', { read: false })
230         .pipe(xgettext('misc/translator/xgettext-pref', 'Koha-pref.pot'))
231         .pipe(dest('misc/translator'))
232 }
233
234 function po_extract_installer () {
235     const globs = [
236         'installer/data/mysql/en/mandatory/*.yml',
237         'installer/data/mysql/en/optional/*.yml',
238     ];
239
240     return src(globs, { read: false, nocase: true })
241         .pipe(xgettext('misc/translator/xgettext-installer', 'Koha-installer.pot'))
242         .pipe(dest('misc/translator'))
243 }
244
245 function po_extract_installer_marc (type) {
246     const globs = `installer/data/mysql/en/marcflavour/${type}/**/*.yml`;
247
248     return src(globs, { read: false, nocase: true })
249         .pipe(xgettext('misc/translator/xgettext-installer', `Koha-installer-${type}.pot`))
250         .pipe(dest('misc/translator'))
251 }
252
253 function po_extract_installer_marc21 ()  { return po_extract_installer_marc('MARC21') }
254
255 function po_create_type (type) {
256     const access = util.promisify(fs.access);
257     const exec = util.promisify(child_process.exec);
258
259     const languages = getLanguages();
260     const promises = [];
261     for (const language of languages) {
262         const locale = language.split('-').filter(s => s.length !== 4).join('_');
263         const po = `misc/translator/po/${language}-${type}.po`;
264         const pot = `misc/translator/Koha-${type}.pot`;
265
266         const promise = access(po)
267             .catch(() => exec(`msginit -o ${po} -i ${pot} -l ${locale} --no-translator`))
268         promises.push(promise);
269     }
270
271     return Promise.all(promises);
272 }
273
274 function po_create_marc_marc21 ()       { return po_create_type('marc-MARC21') }
275 function po_create_marc_normarc ()      { return po_create_type('marc-NORMARC') }
276 function po_create_marc_unimarc ()      { return po_create_type('marc-UNIMARC') }
277 function po_create_staff ()             { return po_create_type('staff-prog') }
278 function po_create_opac ()              { return po_create_type('opac-bootstrap') }
279 function po_create_pref ()              { return po_create_type('pref') }
280 function po_create_messages ()          { return po_create_type('messages') }
281 function po_create_messages_js ()       { return po_create_type('messages-js') }
282 function po_create_installer ()         { return po_create_type('installer') }
283 function po_create_installer_marc21 ()  { return po_create_type('installer-MARC21') }
284
285 function po_update_type (type) {
286     const msgmerge_opts = '--backup=off --quiet --sort-output --update';
287     const cmd = `msgmerge ${msgmerge_opts} <%= file.path %> misc/translator/Koha-${type}.pot`;
288     const languages = getLanguages();
289     const globs = languages.map(language => `misc/translator/po/${language}-${type}.po`);
290
291     return src(globs)
292         .pipe(exec(cmd, { continueOnError: true }))
293         .pipe(exec.reporter({ err: false, stdout: false }))
294 }
295
296 function po_update_marc_marc21 ()       { return po_update_type('marc-MARC21') }
297 function po_update_marc_normarc ()      { return po_update_type('marc-NORMARC') }
298 function po_update_marc_unimarc ()      { return po_update_type('marc-UNIMARC') }
299 function po_update_staff ()             { return po_update_type('staff-prog') }
300 function po_update_opac ()              { return po_update_type('opac-bootstrap') }
301 function po_update_pref ()              { return po_update_type('pref') }
302 function po_update_messages ()          { return po_update_type('messages') }
303 function po_update_messages_js ()       { return po_update_type('messages-js') }
304 function po_update_installer ()         { return po_update_type('installer') }
305 function po_update_installer_marc21 ()  { return po_update_type('installer-MARC21') }
306
307 /**
308  * Gulp plugin that executes xgettext-like command `cmd` on all files given as
309  * input, and then outputs the result as a POT file named `filename`.
310  * `cmd` should accept -o and -f options
311  */
312 function xgettext (cmd, filename) {
313     const filenames = [];
314
315     function transform (file, encoding, callback) {
316         filenames.push(path.relative(file.cwd, file.path));
317         callback();
318     }
319
320     function flush (callback) {
321         fs.mkdtemp(path.join(os.tmpdir(), 'koha-'), (err, folder) => {
322             const outputFilename = path.join(folder, filename);
323             const filesFilename = path.join(folder, 'files');
324             fs.writeFile(filesFilename, filenames.join(os.EOL), err => {
325                 if (err) return callback(err);
326
327                 const command = `${cmd} -o ${outputFilename} -f ${filesFilename}`;
328                 child_process.exec(command, err => {
329                     if (err) return callback(err);
330
331                     fs.readFile(outputFilename, (err, data) => {
332                         if (err) return callback(err);
333
334                         const file = new Vinyl();
335                         file.path = path.join(file.base, filename);
336                         file.contents = data;
337                         callback(null, file);
338                     });
339                 });
340             });
341         })
342     }
343
344     return through2.obj(transform, flush);
345 }
346
347 /**
348  * Return languages selected for PO-related tasks
349  *
350  * This can be either languages given on command-line with --lang option, or
351  * all the languages found in misc/translator/po otherwise
352  */
353 function getLanguages () {
354     if (Array.isArray(args.lang)) {
355         return args.lang;
356     }
357
358     if (args.lang) {
359         return [args.lang];
360     }
361
362     const filenames = fs.readdirSync('misc/translator/po')
363         .filter(filename => filename.endsWith('.po'))
364         .filter(filename => !filename.startsWith('.'))
365
366     const re = new RegExp('-(' + poTypes.join('|') + ')\.po$');
367     languages = filenames.map(filename => filename.replace(re, ''))
368
369     return Array.from(new Set(languages));
370 }
371
372 exports.build = build;
373 exports.css = css;
374
375 exports['po:create'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].create)));
376 exports['po:update'] = parallel(...poTypes.map(type => series(poTasks[type].extract, poTasks[type].update)));
377 exports['po:extract'] = parallel(...poTypes.map(type => poTasks[type].extract));
378
379 exports.default = function () {
380     watch(css_base + "/src/**/*.scss", series('css'));
381 }