-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
68 lines (46 loc) · 1.45 KB
/
index.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
const path = require('path')
const log = require('fancy-log')
const PluginError = require('plugin-error')
const through = require('through2-concurrent')
const chalk = require('chalk')
const plur = require('plur')
const gettextParser = require('gettext-parser')
const PLUGIN_NAME = 'gulp-compile-translation-files'
module.exports = (opts) => {
const validExts = ['.po']
opts = Object.assign({
verbose: process.argv.indexOf('--verbose') !== -1
}, opts)
let totalFiles = 0;
return through.obj({
maxConcurrency: 8
}, (file, enc, cb) => {
if (file.isNull()) {
cb(null, file)
return
}
if (file.isStream()) {
return cb(new PluginError(PLUGIN_NAME, 'Streaming not supported'))
}
if (validExts.indexOf(path.extname(file.path).toLowerCase()) === -1) {
if (opts.verbose) {
log(`${PLUGIN_NAME}: Skipping unsupported file ${chalk.blue(file.relative)}`)
}
return cb(null, file)
}
if (opts.verbose) {
log(`${PLUGIN_NAME}: Compiling file ${file.relative}`)
}
const data = gettextParser.po.parse(file.contents)
const output = gettextParser.mo.compile(data)
file.contents = output
file.path = file.path.replace(/\.[^/.]+$/, '.mo')
totalFiles++
log(PLUGIN_NAME + ':', chalk.green('✔ ') + file.relative + chalk.gray(` successfully compiled`));
cb(null, file)
}, cb => {
let msg = `Compiled ${totalFiles} translation ${plur('file', totalFiles)}`
log(PLUGIN_NAME + ':', msg)
cb()
})
}