-
Notifications
You must be signed in to change notification settings - Fork 8
/
gulpfile.js
executable file
·185 lines (175 loc) · 5.46 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* Gulpfile.
*
* A simple implementation of Gulp.
*
* Implements:
* 1. CSS concatenation and minification
* 2. JS concatenation
* 3. Watch files
*
* @since 3.1.6
* @author Guillermo Figueroa Mesa (@gfirem)
*/
/**
* Configuration.
*/
const styleDir = './admin/css/*.css';
const styleDestination = './admin/css/';
const jsDir = './admin/js/*.js';
const jsDestination = './admin/js/';
const styleWatchFiles = './admin/css/*.css';
const jsWatchFiles = './admin/js/*.js';
const languagesFolder = './languages/';
const languages = [
{
lang: 'en',
spec: 'US'
},
{
lang: 'es',
spec: 'ES'
},
{
lang: 'fr',
spec: 'FR'
},
{
lang: 'nb',
spec: 'NO'
},
{
lang: 'pt',
spec: 'BR'
},
{
lang: 'fa',
spec: 'IR'
}
];
/**
* Load modules
*/
require('dotenv').config();
const gulp = require('gulp');
const minifycss = require('gulp-uglifycss');
const concat = require('gulp-concat');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
const notify = require('gulp-notify');
const clean = require('gulp-clean');
const stripDebug = require('gulp-strip-debug');
const eslint = require('gulp-eslint');
const wpPot = require('gulp-wp-pot');
const pofill = require('gulp-pofill');
const gettext = require('gulp-gettext');
const googleTranslate = require('google-translate')(process.env.GOOGLE_API);
gulp.task('lint', () => {
return gulp.src([jsDir, '!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});
//Extract localization strings and translate it with google
gulp.task('prepare-localization', function() {
return gulp.src('./**/*.php')
.pipe(wpPot({
domain: 'wc4bp',
package: 'WC4BP',
bugReport: 'https://github.com/Themekraft/wc4bp/issues',
lastTranslator: 'Sven Lehnert <[email protected]>',
team: 'ThemeKraft Team <[email protected]>',
gettextFunctions: [
{name: '__'},
{name: '_e'},
{name: 'esc_html__'},
{name: 'esc_html_e'},
{name: 'esc_attr_e'},
{name: '_x', context: 2},
{name: '_esc_attr'},
{name: '_esc_attr_echo'},
{name: '_esc_html'},
{name: '_esc_html_echo'},
{name: '_ex', context: 2},
{name: '_esc_attr_x', context: 2},
{name: '_esc_html_x', context: 2},
{name: '_n', plural: 2},
{name: '_n_noop', plural: 2},
{name: '_nx', plural: 2, context: 4},
{name: '_nx_noop', plural: 2, context: 3}
]
}))
.pipe(gulp.dest(languagesFolder + 'wc4bp.pot'));
});
//Extract localization strings and translate it with google
gulp.task('translate', ['prepare-localization'], function() {
let task = [];
//Create the es po file
languages.forEach((lang) => {
task.push(
gulp.src(languagesFolder + 'wc4bp.pot')
.pipe(pofill({
items: function(item) {
return new Promise((resolve) => {
// If msgstr is empty, use identity translation
if (!item.msgstr.length) {
item.msgstr = [''];
}
if (!item.msgstr[0]) {
googleTranslate.translate(item.msgid, lang.lang, function(err, translation) {
if (translation && translation.translatedText) {
item.msgstr[0] = translation.translatedText;
}
resolve(item);
});
} else {
resolve(item);
}
});
}
}))
.pipe(rename('wc4bp-' + lang.lang + '_' + lang.spec.toUpperCase() + '.po'))
.pipe(gulp.dest(languagesFolder))
);
});
return task;
});
// Compile *.po to *.mo binaries for usage.
gulp.task('compile-translations', function() {
return gulp.src(languagesFolder + '*.po')
.pipe(gettext())
.pipe(gulp.dest(languagesFolder))
});
gulp.task('clean-min-styles', function() {
return gulp.src(styleDestination + '*.min.css', {read: false})
.pipe(clean({force: true}));
});
gulp.task('clean-min-js', function() {
return gulp.src(jsDestination + '*.min.js', {read: false})
.pipe(clean({force: true}));
});
gulp.task('styles', ['clean-min-styles'], function() {
gulp.src(styleDir)
.pipe(rename({suffix: '.min'}))
.pipe(minifycss({
maxLineLen: 10
}))
.pipe(gulp.dest(styleDestination))
.pipe(notify({message: 'TASK: "CSS" Completed!', onLast: true}))
});
gulp.task('js', ['clean-min-js'], function() {
gulp.src(jsDir)
.pipe(rename({suffix: '.min'}))
.pipe(uglify({
output: {
comments: "all"
}
}))
.pipe(stripDebug())
.pipe(gulp.dest(jsDestination))
.pipe(notify({message: 'TASK: "JS" Completed!', onLast: true}));
});
gulp.task('default', [], function() {
gulp.run('styles');
gulp.run('js');
});