-
Notifications
You must be signed in to change notification settings - Fork 22
/
gulpfile.js
137 lines (110 loc) · 4.23 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
var gulp = require('gulp');
var mjml = require('gulp-mjml')
var mjmlEngine = require('mjml')
var i18n = require('gulp-html-i18n')
var download = require("gulp-download-stream");
var buffer = require('vinyl-buffer')
var replace_task = require('gulp-replace-task');
var ext_replace = require('gulp-ext-replace');
var zip = require('gulp-zip');
var replace = require('gulp-replace');
var rename = require("gulp-rename");
var fs = require('fs');
var del = require('del');
var settings = require('./src/config/settings.json');
// Compile files [dev mode]
gulp.task('build:dev', function () {
var css = fs.readFileSync(__dirname+'/src/css/global.css', 'utf8');
var fake_json_path = './src/config/fake.json';
delete require.cache[require.resolve(fake_json_path)]
var fake = require(fake_json_path);
return gulp.src(['src/*.mjml'])
// Compile MJML to HTML
.pipe(mjml(mjmlEngine))
// Delete conditions
.pipe(replace('{{/if}}', ''))
.pipe(replace(/{{if \$[a-z_]+}}/ig, ''))
// We need to decode some curly brackets & dollar signs because of MJML
.pipe(replace('%7B%7B', '{{'))
.pipe(replace('%7D%7D', '}}'))
.pipe(replace('$', '$'))
// CSS injection in the header
.pipe(replace('</head>', '<style>'+css+'</style></head>'))
// Translate
.pipe(i18n({
langDir: './langs',
trace: true,
createLangDirs: true
}))
// Replace variables with fake data
.pipe(replace_task({
patterns: [{json: fake}],
usePrefix: false
}))
.pipe(gulp.dest('./dist/html/'))
});
// Compile files with MJML
gulp.task('build:mjml', function () {
var css = fs.readFileSync(__dirname+'/src/css/global.css', 'utf8');
return gulp.src(['src/*.mjml'])
// Compile MJML to HTML
.pipe(mjml(mjmlEngine))
// We need to decode some curly brackets & dollar signs because of MJML
.pipe(replace('%7B%7B', '{{'))
.pipe(replace('%7D%7D', '}}'))
.pipe(replace('$', '$'))
// CSS injection in the header
.pipe(replace('</head>', '<style>'+css+'</style></head>'))
// Rename files
.pipe(ext_replace('.tpl'))
.pipe(gulp.dest('./dist/'+settings.name))
});
// Copy images in dist folder
gulp.task('build:copy:img', function() {
return gulp.src('src/img/*.{jpg,jpeg,png,gif}')
.pipe(gulp.dest('./dist/'+settings.name+'/img/'));
});
// Copy tpls in dist folder
gulp.task('build:copy:tpl', function () {
return gulp.src('src/*.tpl')
.pipe(gulp.dest('./dist/'+settings.name+'/tpl/'));
});
// Copy preview img in dist folder
gulp.task('build:copy:preview', function() {
return gulp.src('src/preview.jpg')
.pipe(gulp.dest('./dist/'+settings.name+'/'));
});
// Copy settings in dist folder
gulp.task('build:copy:settings', function() {
return gulp.src('src/config/settings.json')
.pipe(gulp.dest('./dist/'+settings.name+'/'));
});
// Compress folder
gulp.task('build:compress', ['build:copy:settings', 'build:mjml', 'build:copy:tpl', 'build:copy:img', 'build:copy:preview'], function() {
return gulp.src('./dist/'+settings.name+'/**/*')
.pipe(zip(settings.name+'.zip'))
.pipe(gulp.dest('./dist/'));
});
// Copy images in dist folder
gulp.task('build', ['build:copy:settings', 'build:mjml', 'build:copy:img', 'build:copy:preview', 'build:copy:tpl', 'build:compress']);
// Watch changes
gulp.task('watch', function () {
gulp.watch('src/**/*.mjml', ['build:dev']);
gulp.watch('src/css/global.css', ['build:dev']);
gulp.watch('src/config/fake.json', ['build:dev']);
});
// Download translations
gulp.task('langs:dl', ['langs:clean'], function () {
['en', 'fr', 'es'].forEach(function(lang) {
download('http://api.addons.prestashop.com/index.php?version=1&method=translations&type=emails&iso_lang='+lang)
.pipe(buffer())
.pipe(rename("lang.json"))
.pipe(gulp.dest('langs/'+lang+'/'));
})
});
// Remove previously downloaded langs
gulp.task('langs:clean', function () {
return del(['langs']);
});
// Run all tasks if no args
gulp.task('default', ['watch']);