-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
123 lines (94 loc) · 2.66 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
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
/*
* gulp-potomo
* https://github.com/felixzapata/gulp-potomo
*
* Copyright (c) 2016 Félix Zapata
* Licensed under the ISC license.
*/
'use strict';
var fs = require('fs-extra');
var path = require('path');
var R = require('ramda');
var chalk = require('chalk');
var through = require('through2');
var shell = require('shelljs');
var gutil = require('gulp-util');
var PLUGIN_NAME = 'gulp-potomo';
function fileExists(filePath) {
try {
return fs.statSync(filePath).isFile();
} catch (e) {
return false;
}
}
function gulpPotomo(customOptions, cb) {
var defaultOptions = {
poDel: false,
verbose: true
};
var options = customOptions ? R.merge(defaultOptions, customOptions) : defaultOptions;
function log(message) {
if(options.verbose) {
console.log(message);
}
}
function bufferContents(file, enc, cb) {
/* jshint validthis: true */
var mofileDest;
var moFile;
var moFileName;
var command;
var message;
var tempFolder;
// Return warning if not found msgfmt command
if (!shell.which('msgfmt')) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'GNU gettext is not installed in your system PATH.'));
cb();
return;
}
// ignore empty files
if (file.isNull()) {
cb();
return;
}
if (file.isStream()) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Streaming not supported'));
cb();
return;
}
// create a temp folder to save the mo files
tempFolder = path.join(__dirname, 'tmp/');
fs.mkdirsSync(tempFolder);
moFileName = path.basename(file.path, '.po') + '.mo';
mofileDest = path.join(tempFolder, moFileName);
command = 'msgfmt -o "' + mofileDest + '" "' + file.path + '"';
if (shell.exec(command).code !== 0) {
this.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Failed to Compile "*.po" files into binary "*.mo" files with "msgfmt".'));
cb();
return;
} else {
log('File ' + chalk.cyan(mofileDest) + ' Created.');
moFile = new gutil.File({
path: moFileName,
contents: new Buffer(fs.readFileSync(mofileDest))
});
}
// remove the temp folder
fs.removeSync(tempFolder);
// Delete Source PO file(s).
if (options.poDel && fileExists(file.path)) {
fs.removeSync(file.path);
}
// Process the Message.
message = 'Compiled ' + file.path + ' file.';
if (options.poDel) {
message = 'Deleted ' + file.path + ' files.';
}
log(chalk.green(message));
this.push(moFile);
cb();
}
return through.obj(bufferContents, cb);
}
// Exporting the plugin main function
module.exports = gulpPotomo;