-
Notifications
You must be signed in to change notification settings - Fork 43
/
gulpfile.js
158 lines (139 loc) · 3.83 KB
/
gulpfile.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
var gulp = require('gulp');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var rename = require('gulp-rename');
var watch = require('gulp-watch');
var batch = require('gulp-batch');
var ejs = require('gulp-ejs');
var gutil = require('gulp-util');
var async = require('async');
var wiredep = require('wiredep').stream;
var fs = require('fs');
var path = require('path');
gulp.task('default', ['sass', 'uglify', 'wiredep']);
/**
* Build styles files
*/
gulp.task('sass', function () {
gulp.src('src/styles/main.scss')
.pipe(sass())
.pipe(rename("styles.css"))
.pipe(gulp.dest('public/styles/'));
});
/**
* Build minified javascript files
*/
gulp.task('uglify', function () {
gulp.src(['src/js/*.js'])
.pipe(uglify())
.pipe(rename("scripts.min.js"))
.pipe(gulp.dest('public/js'));
});
/**
* Add bower dependencies into .html files
*/
gulp.task('wiredep', ['ejs'], function () {
gulp.src(['public/*.html'])
.pipe(wiredep())
.pipe(gulp.dest('public'));
});
/**
* Create HTML files from .ejs files
*/
gulp.task('ejs', function (cb) {
var langs = getAvailableLanguagesCatalogs();
gutil.log("Found these languages catalogs (with translation ratio >0.7): ", langs);
//A separate gulp stream must be launched for generating html files
//for each language.
allTasks = langs.map(function(langCode) {
return function(cb) {
//Open the language catalog
var catalog = langCode == "en" ? "" : require('./locale/' + langCode + '.json');
//Launch a stream for generating the .ejs file for this language
gulp.src('src/*.ejs')
.on('end', function() {
gutil.log("Ended generating " + langCode + " files from .ejs sources");
cb();
})
.pipe(ejs({
//The EJS helper function to be used to wrap any string to be translated
_: function(str) {
if (catalog.hasOwnProperty(str)) {
return catalog[str];
}
return str;
}
}).on('error', cb))
.pipe(rename(function(path) {
if (langCode !== "en") {
path.basename += "-" + langCode;
}
}))
.pipe(gulp.dest('public'));
}
});
async.parallel(allTasks, function(err) {
cb(err);
})
});
/**
* Create catalog.json
*/
gulp.task('update-translation', function () {
var allStrings = {};
gulp.src('src/*.ejs')
.on('end', function() {
fs.writeFile('locale/catalog.json', JSON.stringify(allStrings, null, 4), function(err) {
if(err) {
gutil.log(err);
return;
}
gutil.log("Catalog with all strings saved to locale/catalog.json");
});
})
.pipe(ejs({
"_": function(str) {
allStrings[str] = str;
}
}).on('error', gutil.log));
});
/**
* Watch changes in the src directory and launch the appropriate tasks.
*/
gulp.task('watch', function () {
gulp.watch('src/styles/**/*.scss', ['sass']);
gulp.watch('src/js/*.js', ['uglify']);
gulp.watch(['src/*.ejs', 'locale/*.json'], ['wiredep']);
});
/**
* Return a list of language name corresponding to catalogs .json files stored
* in locale directory, with a translation ratio that is high enough.
*/
function getAvailableLanguagesCatalogs() {
var availableLangs = ["en"];
fs.readdirSync('locale').forEach(function(file) {
if (path.basename(file) === "catalog.json" ||
path.extname(file) !== ".json")
return;
var ratio = getTranslatedRatio(require('./locale/' + file));
if (ratio > 0.7) availableLangs.push(path.basename(file, ".json"));
});
return availableLangs;
}
/**
* Return the total number of keys that differs from their values
* in the specified object.
*/
function getTranslatedRatio(catalog) {
var translatedCount = 0;
var totalCount = 0;
for (var p in catalog) {
if (catalog.hasOwnProperty(p)) {
if (p !== catalog[p]) {
translatedCount++;
}
totalCount++;
}
}
return translatedCount/totalCount;
}