-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgulpfile.js
245 lines (224 loc) · 5.71 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
const path = require('path');
const gulp = require('gulp');
const pump = require('pump');
const htmlclean = require('gulp-htmlclean');
const ejs = require('gulp-ejs');
const eslint = require('gulp-eslint');
const rename = require('gulp-rename');
const concat = require('gulp-concat');
const minify = require('gulp-minify');
const less = require('gulp-less');
const postcss = require('gulp-postcss');
const autoprefixer = require('autoprefixer');
const mqpacker = require('css-mqpacker');
const cssnano = require('cssnano');
const ghpages = require('gh-pages');
const marked = require('gulp-marked');
const inject = require('gulp-inject-self');
const replace = require('gulp-replace');
const del = require('del');
const runSequence = require('run-sequence');
const reload = require('require-reload')(require)
let argv = require('yargs').argv;
let content = reload('./src/content/content.json');
// Language List to build (for languages other than default, add a / at the end)
let langList = [''];
if (argv.lang) {
langList = [(argv.lang == 'en' ? '' : argv.lang + '/')];
}
let lang = '';
let root = '';
// Helper function to find a key and its value in any object
function searchKey(obj, key = 'key') {
var json = JSON.stringify(obj);
var reg = new RegExp(`(?!^)(?:"${key}":)([^\\\\]+?})`);
var group = reg.exec(json)[1];
return JSON.parse(group);
}
// Clean HTML
gulp.task('html', function (cb) {
pump([
gulp.src(`src/*.ejs`),
ejs({
site_title: 'Garlic Recipes',
content: content['content'],
navbar: content['navbar']
}, {}, {
ext: '.html'
}),
replace('ROOT', function () {
return root;
}),
replace('LANGUAGE_REPLACE', function () {
let language = ((lang && lang != '') ? lang.substring(0, lang.length - 1) : 'en');
return language;
}),
htmlclean(),
gulp.dest(`dist/${lang}`)
], cb);
});
// Markdown Content Files
gulp.task('md', ['html'], function (cb) {
pump([
gulp.src(`src/content/${lang}*.md`),
marked(),
ejs({
site_title: 'Garlic Recipes',
page_title: 'PAGE_TITLE',
content: content['content'],
navbar: content['navbar']
}, {}, {
ext: '.html'
}),
inject(`dist/${lang}template.html`, /<INJECT>/, {
replaceWith: function (fileContent) {
return '\n' + fileContent;
}
}),
replace('ROOT', function () {
return root;
}),
replace('PAGE_TITLE', function () {
var name = this.file.relative.replace(/(.*)\.(.*?)$/, "$1");
var data = searchKey(content['content'], name);
return data.title;
}),
replace('LANGUAGE_REPLACE', function () {
let language = ((lang && lang != '') ? lang.substring(0, lang.length - 1) : 'en');
return language;
}),
htmlclean(),
gulp.dest(`dist/${lang}`)
], cb);
});
// Remove useless html files
gulp.task('clean-html', function (cb) {
return del([
'dist/**/template.html',
'dist/**/blank.html'
])
});
// Run task js, only if verify is successful
gulp.task('js', ['verify'], function (cb) {
pump([
gulp.src(['src/js/**/*.js', '!src/lib/**/*']),
concat('script.js'),
gulp.dest('dist/js'),
minify({
ext: {
min: '.min.js'
},
noSource: true
}),
gulp.dest('dist/js')
], cb);
});
// Lints the js files
gulp.task('verify', function (cb) {
pump([
gulp.src(['src/js/**/*.js', '!src/lib/**/*']),
eslint(),
eslint.format(),
eslint.failAfterError()
], cb);
});
// Copies any file in lib
gulp.task('lib', function (cb) {
pump([
gulp.src(['src/lib/**/*']),
gulp.dest('dist/lib')
], cb);
});
// Copies any file in files
gulp.task('files', function (cb) {
pump([
gulp.src(['src/files/**/*']),
gulp.dest('dist/files')
], cb);
});
// Do a bunch of stuff to CSS files
gulp.task('less', function (cb) {
pump([
gulp.src('src/less/**/*.less'),
less(),
postcss([
autoprefixer({
browsers: ['last 2 versions', '> 2%']
}),
mqpacker
]),
concat('style.css'),
gulp.dest('dist/css'),
postcss([
cssnano
]),
rename('style.min.css'),
gulp.dest('dist/css')
], cb);
});
// Copy fonts over to build
gulp.task('fonts', function (cb) {
pump([
gulp.src('src/fonts/**/*'),
gulp.dest('dist/fonts')
], cb);
});
// Copy images over to build
gulp.task('images', function (cb) {
pump([
gulp.src('src/images/**/*'),
gulp.dest('dist/images')
], cb);
});
// Remove previous build
gulp.task('clean', function (cb) {
return del([
'dist/**/*'
]);
});
function buildLanguage(index, cb) {
lang = langList[index];
content = reload(`./src/content/${lang}content.json`);
runSequence('md', function () {
if (index == langList.length - 1) {
// If it's the last item in langlist call cb
cb();
}else {
buildLanguage(index + 1, cb);
}
});
}
// Reload config file & build html
gulp.task('reload', function (cb) {
// Start a reflexive build + reload
buildLanguage(0, cb);
});
// Upload to GitHub Pages
gulp.task('deploy', function (cb) {
root = 'https://pandawan.github.io/GarlicRecipes'
runSequence('clean', 'build', 'clean-html', function () {
// No need to call gulp prod anymore, dist folder is ignored by gitignore
ghpages.publish('dist', cb);
});
});
// Build everything as if you were deploying
gulp.task('prod', function (cb) {
root = '';
runSequence('clean', 'build', 'clean-html');
});
// Build everything
gulp.task('build', function (cb) {
runSequence(['js', 'less', 'reload', 'lib', 'fonts', 'files', 'images'], 'clean-html', cb);
});
// Watch for files and build
gulp.task('watch', ['build'], function () {
gulp.watch('src/js/**/*.js', ['js']);
gulp.watch('src/less/**/*.less', ['less']);
gulp.watch('src/**/*.ejs', ['html']);
gulp.watch('src/content/**/*.md', ['md']);
gulp.watch('src/content/content.json', ['reload'])
gulp.watch('src/lib/**/*', ['lib']);
gulp.watch('src/fonts/**/*', ['fonts']);
gulp.watch('src/files/**/*', ['files']);
gulp.watch('src/images/**/*', ['images']);
});