-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·65 lines (51 loc) · 1.52 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
#!/usr/bin/env node
const gettextParser = require("gettext-parser");
const fs = require("fs");
const path = require("path");
const colors = require("colors");
console.log(colors.dim("Compile MO files from PO files..."));
const targetDir = path.resolve(process.argv.slice(2)[0]);
compilePoFiles(targetDir);
function compilePoFiles(dir) {
fs.readdir(dir, function(err, files) {
if (err) {
console.error("Could not list the directory.", err);
process.exit(1);
}
files.forEach(filename => {
compilePoFile(path.join(dir, filename));
});
});
}
function compilePoFile(sourcePath) {
fs.stat(sourcePath, function(error, stat) {
if (error) {
console.error("Error stating file.", error);
return;
}
if (!stat.isFile()) return;
const { dir, ext, name, base } = path.parse(sourcePath);
if (ext !== ".po") return;
const targetFilename = name + ".mo";
fs.readFile(sourcePath, (error, input) => {
if (error) {
console.error("Error reading file.", error);
return;
}
const po = gettextParser.po.parse(input);
const output = gettextParser.mo.compile(po);
fs.writeFile(path.join(dir, targetFilename), output, error => {
if (error) {
console.error("Error writing file.", error);
return;
}
console.log(
colors.yellow("[" + dir + "] ") +
colors.cyan(base) +
colors.dim(" => ") +
colors.green(targetFilename)
);
});
});
});
}