forked from myparcelnl/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gulpfile.js
195 lines (174 loc) · 4.95 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
186
187
188
189
190
191
192
193
194
195
const babelify = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const clean = require('gulp-clean');
const gulp = require('gulp');
const gulpPoSync = require('gulp-po-sync');
const postcss = require('gulp-postcss');
const sass = require('gulp-sass')(require('sass'));
const sourcemaps = require('gulp-sourcemaps');
const tap = require('gulp-tap');
const uglify = require('gulp-uglify');
const wpPot = require('gulp-wp-pot');
const zip = require('gulp-zip');
const {exec} = require('child_process');
const {downloadTranslations} = require('./private/downloadTranslations');
const PHP_FILES = ['*.php', 'migration/**/*.php', 'templates/**/*.php', 'includes/**/*.php'];
/**
* Callback for use with tasks using child_process.exec().
*
* @param {Function} callback
* @param {ExecException} err
* @param {String} stdout
* @param {String} stderr
*/
function execCallback(callback, err, stdout, stderr) {
/* eslint-disable no-console */
if (stdout) {
console.log(stdout);
}
if (stderr) {
console.warn(stderr);
}
/* eslint-enable no-console */
if (typeof callback === 'function') {
callback(err);
}
}
/**
* Empty the dist folder.
*/
gulp.task('clean', () => gulp.src('dist/**/*.*', {read: false})
.pipe(clean({force: true})));
/**
* Run babel on the javascript files.
*/
gulp.task('build:js', () => gulp.src('src/js/**/*.js', {read: false})
.pipe(tap((file) => {
file.contents = browserify(file.path)
.transform(babelify)
.bundle();
}))
.pipe(buffer())
.pipe(sourcemaps.init())
.pipe(uglify())
.pipe(sourcemaps.write('./source-maps'))
.pipe(gulp.dest('assets/js')));
/**
* Copy the delivery options js.
*/
gulp.task('copy:delivery-options', () => gulp.src('node_modules/@myparcel/delivery-options/dist/myparcel.js')
.pipe(gulp.dest('assets/js')));
/**
* Compile and run postcss.
*/
gulp.task('build:scss', () => gulp.src('src/scss/**/*.scss')
.pipe(sass())
.pipe(postcss())
.pipe(gulp.dest('assets/css')));
/**
* Copy the static css files and images.
*/
gulp.task('copy', () => gulp.src([
'src/css/**/*.*',
'src/img/**/*.*',
], {
base: 'src',
})
.pipe(gulp.dest('assets')));
/**
* Collect all files and put it in a zip file.
*/
gulp.task('zip', () => gulp.src([
'*.png',
'LICENSE',
'assets/**/*',
'includes/**/*',
'languages/**/*',
'migration/**/*',
'readme.txt',
'templates/**/*',
'vendor/**/*',
'woocommerce-myparcel.php',
'wpm-config.json',
], {base: '.'})
.pipe(zip('woocommerce-myparcelbe.zip'))
.pipe(gulp.dest('./')));
/**
* Sync .pot file with source code.
*/
gulp.task('translations:pot', () => gulp.src(PHP_FILES, {read: false})
.pipe(wpPot({
bugReport: 'https://github.com/myparcelbe/woocommerce/issues',
domain: 'woocommerce-myparcelbe',
package: 'WooCommerce MyParcel BE',
team: 'MyParcel <[email protected]>',
}))
.pipe(gulp.dest('languages/woocommerce-myparcelbe.pot')));
/**
* Download translations as csv and convert them to .po files.
*/
gulp.task('translations:import', (callback) => {
downloadTranslations();
callback();
});
/**
* Sync .po files with .pot file.
*/
gulp.task('translations:po', () => gulp.src('languages/**/*.po', {read: false})
.pipe(gulpPoSync('languages/woocommerce-myparcelbe.pot'))
.pipe(gulp.dest('languages')));
/**
* Convert existing .po files to csv files.
*/
gulp.task('translations:po2csv', (callback) => {
exec('po2csv translations private/temp', (...params) => execCallback(callback, ...params));
});
/**
* Regenerate .pot files and export to csv for updating the external sheet with new or updated keys.
*/
gulp.task('translations:export', gulp.series(
'translations:pot',
'translations:po',
'translations:po2csv',
));
gulp.task('update:composer', (callback) => {
exec('composer update', (...params) => execCallback(callback, ...params));
});
gulp.task('update:npm', (callback) => {
exec('npm update @myparcel/delivery-options', (...params) => execCallback(callback, ...params));
});
/**
* The default task.
*/
const build = gulp.series(
'clean',
gulp.parallel(
'build:js',
'build:scss',
'copy',
'translations:import',
'translations:pot',
'update:composer',
gulp.series(
'update:npm',
'copy:delivery-options',
),
),
);
gulp.task('build', build);
gulp.task('build:zip', gulp.series('build', 'zip'));
const watch = () => {
gulp.watch(['src/css/**/*', 'src/img/**/*'], null, gulp.series('copy'));
// Don't use babel in watch mode
gulp.watch(['src/js/**/*'], null, () => gulp.src('src/js/**/*.js').pipe(gulp.dest('assets/js')));
gulp.watch(['node_modules/@myparcel/delivery-options/**/*'], null, gulp.series('copy:delivery-options'));
gulp.watch(['src/scss/**/*'], null, gulp.series('build:scss'));
gulp.watch(['composer.json'], null, gulp.series('update:composer'));
gulp.watch(['package.json'], null, gulp.series('update:npm'));
};
gulp.task('watch', gulp.series(
build,
watch,
));
exports.default = build;