forked from myparcelnl/prestashop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
252 lines (225 loc) · 6.14 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
const babelify = require('babelify');
const browserify = require('browserify');
const buffer = require('vinyl-buffer');
const clean = require('gulp-clean');
const {exec} = require('child_process');
const gulp = require('gulp');
const packageJson = require('./package.json');
const rename = require('gulp-rename');
const {replaceCaseSensitive} = require('./private/replaceCaseSensitive');
const sourcemaps = require('gulp-sourcemaps');
const tap = require('gulp-tap');
const uglify = require('gulp-uglify');
const zip = require('gulp-zip');
const MODULE_NAME_NL = 'myparcelnl';
const MODULE_NAME_BE = 'myparcelbe';
const modules = [MODULE_NAME_BE, MODULE_NAME_NL];
const moduleNameMap = {
[MODULE_NAME_NL]: 'MyParcelNL',
[MODULE_NAME_BE]: 'MyParcelBE',
};
const {version} = packageJson;
/**
* Files where module name should be transformed in filenames and contents.
*
* @type {string[]}
*/
const sourceFiles = [
'controllers/**/*',
'mails/**/*',
'src/**/*',
'upgrade/**/*',
'views/**/*',
'!views/js/**/*',
'!views/js',
'!views/dist/**/*',
'composer.json',
'index.php',
'myparcelbe.php',
];
/**
* Files that should be copied without modifying contents or filenames.
*
* @type {string[]}
*/
const copyFiles = [
'views/dist/**/*',
'composer.lock',
'logo.png',
'package-lock.json',
'package.json',
];
/**
* Files that should be excluded from the final zip file.
*
* @type {string[]}
*/
const excludeFiles = [
'composer.lock',
'package-lock.json',
'package.json',
];
/**
* 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) {
if (stderr) {
// eslint-disable-next-line no-console
console.warn(stderr);
}
if (typeof callback === 'function') {
callback(err);
}
}
/**
* @param {string} moduleName
* @returns {Function}
*/
function createTransformTask(moduleName) {
return () => gulp.src(sourceFiles, {base: '.'})
.pipe(tap((file) => {
if (!file.isDirectory()) {
file.contents = Buffer.from(replaceCaseSensitive(file.contents.toString(), MODULE_NAME_BE, moduleName));
}
}))
.pipe(rename((path) => {
path.basename = replaceCaseSensitive(path.basename, MODULE_NAME_BE, moduleName);
}))
.pipe(gulp.dest(`dist/${moduleName}`));
}
/**
* @param {string} moduleName
* @returns {Function}
*/
function createCopyTask(moduleName) {
return () => gulp.src(copyFiles, {base: '.'})
.pipe(gulp.dest(`dist/${moduleName}`));
}
/**
* @param {string} moduleName
*
* @returns {Function}
*/
function createTransferTask(moduleName) {
return gulp.series(
`copy:${moduleName}`,
`transform:${moduleName}`,
);
}
/**
* @param {string} moduleName
* @returns {Function}
*/
function createBuildTask(moduleName) {
return gulp.series(
`transfer:${moduleName}`,
`composer:update:${moduleName}`,
);
}
/**
* Collect all files and put them in a zip file.
*
* @param {string} moduleName
* @returns {Function}
*/
function createZipTask(moduleName) {
return () => gulp.src([
`./dist/${moduleName}/**/*`,
...excludeFiles.map((filename) => `!./dist/${moduleName}/${filename}`),
], {base: 'dist'})
.pipe(zip(`${moduleNameMap[moduleName]}-${version}.zip`))
.pipe(gulp.dest('dist'));
}
/**
* @param {string} task
*/
function createTasksForAllModules(task) {
gulp.task(task, gulp.parallel(...modules.map((moduleName) => `${task}:${moduleName}`)));
}
/**
* Run babel on the javascript files.
*/
gulp.task('js:build', () => gulp.src(['./views/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('.'))
.pipe(gulp.dest('views/dist/js')));
/**
* Copy the js to dist without doing any processing on it.
*/
gulp.task('js:copy', () => gulp.src('./views/js/**/*.js')
.pipe(gulp.dest('views/dist/js')));
gulp.task('js:clean', () => gulp.src('./views/dist/*', {allowEmpty: true, read: false})
.pipe(clean({force: true})));
/**
*
* @param {string} moduleName
* @returns {Function}
*/
function createComposerTask(moduleName) {
return (callback) => {
exec(`cd dist/${moduleName} && composer install --no-dev`, (...params) => execCallback(callback, ...params));
};
}
modules.forEach((moduleName) => {
gulp.task(`copy:${moduleName}`, createCopyTask(moduleName));
gulp.task(`transform:${moduleName}`, createTransformTask(moduleName));
gulp.task(`transfer:${moduleName}`, createTransferTask(moduleName));
gulp.task(`composer:update:${moduleName}`, createComposerTask(moduleName));
gulp.task(`build:${moduleName}`, createBuildTask(moduleName));
gulp.task(`zip:${moduleName}`, createZipTask(moduleName));
});
createTasksForAllModules('build');
createTasksForAllModules('transform');
createTasksForAllModules('copy');
createTasksForAllModules('transfer');
createTasksForAllModules('composer:update');
createTasksForAllModules('zip');
/**
* Copy delivery options into module.
*/
gulp.task('copy:delivery-options', () => gulp.src('./node_modules/@myparcel/delivery-options/dist/myparcel.js')
.pipe(gulp.dest('views/dist/js/external/')));
/**
* Empty the dist folders.
*/
gulp.task('clean', () => gulp.src(['dist/*', 'views/dist/*'], {read: false})
.pipe(clean({force: true})));
/**
* The default task.
*/
const build = gulp.series(
'clean',
'js:build',
'copy:delivery-options',
gulp.parallel(
...modules.map((moduleName) => gulp.series(
`build:${moduleName}`,
`zip:${moduleName}`,
)),
),
);
gulp.task('build', build);
const watch = () => {
gulp.watch(['views/js/**/*'], null, gulp.series('js:clean', 'js:copy', 'copy:delivery-options', 'copy'));
// When files are modified, just transfer them.
gulp.watch(sourceFiles, {events: ['change']}, gulp.series('transfer'));
// When files are added or deleted, transfer the files and run composer update.
gulp.watch(sourceFiles, {events: ['add', 'unlink']}, gulp.series('build'));
};
gulp.task('watch', gulp.series(
build,
watch,
));
exports.default = build;