This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (79 loc) · 2.84 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
'use strict';
var glob = require('glob');
var fs = require('fs');
var through = require('through2');
var gutil = require('gulp-util');
var _ = require('lodash');
var mergeStream = require('merge-stream');
module.exports = function(options) {
var translationsSrc = options.translationsSrc;
var translations = [];
return through.obj(function(file, encoding, callback) {
if (file.isNull()) {
callback(null, file);
return;
}
if (file.isStream()) {
callback(new gutil.PluginError('gulp-missing-translations', 'Streaming not supported'));
return;
}
translations = translations.concat(file.contents.toString().match(/MVTR_[A-Z0-9_]+/g));
callback(null, file);
}, function(callback) {
var streams;
var missingTranslationsCount = 0;
translations = _(translations).compact().uniq().value();
glob(translationsSrc, null, function(er, translationFiles) {
if (er) {
return;
}
streams = translationFiles.map(function(translationFile) {
return fs.createReadStream(translationFile)
.pipe(through.obj(function(file, encoding, cb) {
var fileContent = file.toString();
var missingTranslations = [];
translations.forEach(function(translation) {
if (_.endsWith(translation, '_')) {
if (fileContent.indexOf(translation) === -1) {
missingTranslations.push(translation);
missingTranslationsCount++;
}
return;
}
// all translations in JSON file ends with "
if (fileContent.indexOf(translation + '"') === -1) {
missingTranslations.push(translation);
missingTranslationsCount++;
}
});
printResults(translationFile, missingTranslations);
cb(null, file);
}));
});
mergeStream.apply(null, streams)
.on('finish', function() {
if (missingTranslationsCount === 0) {
callback();
return;
}
callback(new gutil.PluginError(
'gulp-missing-translations',
'Missing ' + missingTranslationsCount + ' translations!')
);
});
});
});
function printResults(translationFile, missingTranslations) {
if (missingTranslations.length === 0) {
gutil.log(gutil.colors.bgGreen.grey(translationFile));
gutil.log(gutil.colors.blue('No missing translations.'));
} else {
gutil.log(gutil.colors.bgYellow.grey(translationFile));
gutil.log(gutil.colors.yellow(missingTranslations.length + ' missing translations.'));
missingTranslations.forEach(function(translation) {
gutil.log(gutil.colors.red(translation));
})
}
gutil.log(gutil.colors.blue('----------------------------'));
}
};