forked from mendix/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gulpfile.js
275 lines (249 loc) · 8.94 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const gulp = require('gulp-help')(require('gulp'));
const gutil = require('gulp-util');
const sass = require('gulp-sass');
const sourcemaps = require('gulp-sourcemaps');
const minify = require('gulp-minify');
const hash = require('gulp-hash');
const CONFIG = require('./_gulp/config');
const server = require('./_gulp/server');
const jsonServer = require('./_gulp/json');
const hugo = require('./_gulp/hugo');
const redirect_mappings = require('./_gulp/mappings_redirects');
const asset_mappings = require('./_gulp/mappings_assets');
const htmlproofer = require('./_gulp/htmlproofer');
const algolia = require('./_gulp/algolia');
const menu_check = require('./_gulp/menu_check');
const menu_build = require('./_gulp/menu_build');
const generatePDF = require('./_gulp/pdf');
const { gulpErr } = require('./_gulp/helpers');
const { cyan, red } = require('./_gulp/helpers/command_line').colors;
const path = require('path');
const pump = require('pump');
const browserSync = require('browser-sync').create();
const del = require('del');
const runSequence = require('run-sequence');
const PUBLISH_DRAFTS = typeof process.env.HUGO_ENV !== 'undefined' ? process.env.HUGO_ENV === 'test' : false;
/* DONT EDIT BELOW */
gutil.log(`Gulp started at ${cyan(CONFIG.BUILDDATE)}, drafts ${cyan(PUBLISH_DRAFTS ? 'enabled' : 'disabled')}`);
/*************************************************
CLEAN
**************************************************/
const cleanTask = p => del([ p ], { force: true });
gulp.task('clean', `Cleanup the ${CONFIG.DIST_FOLDER} directory`, () => cleanTask(CONFIG.DIST_FOLDER));
gulp.task('clean:js', `Cleanup the ${CONFIG.PATHS.scripts.dest} directory`, () => cleanTask(CONFIG.PATHS.scripts.dest));
gulp.task('clean:css', `Cleanup the ${CONFIG.PATHS.styles.dest} directory`, () => cleanTask(CONFIG.PATHS.styles.dest));
/*************************************************
MAPPINGS
**************************************************/
gulp.task('write:mappings', `Write mappings from _assets/mappings/redirect.json to ${CONFIG.DIST_FOLDER}/mappings/redirect.map`, done => {
redirect_mappings()
.then(done)
.catch(err => {
gulpErr('write:mapping', err);
return process.exit(2);
});
});
gulp.task('write:assetmappings', `Write asset mappings to ${CONFIG.DIST_FOLDER}/mappings/assets.map`, done => {
asset_mappings(CONFIG.CURRENTFOLDER)
.then(done)
.catch(err => {
gulpErr('write:assetmapping', err);
return process.exit(2);
});
});
/*************************************************
MENU
**************************************************/
const writeMenu = (exitOnError) => {
return (done) => {
menu_build
.build({
src: CONFIG.CONTENTFOLDER,
destination: path.join(CONFIG.CURRENTFOLDER, 'static/json/'),
space: path.join(CONFIG.CURRENTFOLDER, 'data/spaces.yml')
})
.then((errors) => {
if (exitOnError && errors) {
const err = new Error('Errors in writing menu');
err.showStack = false;
done(err);
} else {
done();
}
})
.catch((e) => {
console.log(e);
return process.exit(2);
})
}
}
gulp.task('write:menu', `Write menu jsons (development)`, writeMenu(false));
gulp.task('build:menu', `Build menu jsons (production)`, writeMenu(true));
gulp.task('check:menu', `Check menu structure in the build folder`, done => {
menu_check.check(path.resolve(CONFIG.CURRENTFOLDER, '_site/json'), function (err) {
if (err) {
return process.exit(2);
} else {
done();
}
});
});
/*************************************************
JAVASCRIPT
**************************************************/
gulp.task('js-watch', `Internal task, don't use`, ['build:js'], function (done) {
browserSync.reload();
done();
});
// BUILD
gulp.task('build:js', `Compress js files`, ['clean:js'], (done) => {
pump([
gulp.src(CONFIG.PATHS.scripts.src),
minify({
ext: {
src: '-debug.js',
min: '.js'
}
}),
gulp.dest(CONFIG.PATHS.scripts.dest),
hash(),
gulp.dest(CONFIG.PATHS.scripts.dest),
hash.manifest('assetsjs.json', true, 4),
gulp.dest(CONFIG.DATAFOLDER)
], done);
});
/*************************************************
SASS
**************************************************/
gulp.task('build:sass', `Sass build`, ['clean:css'], () => {
return gulp
.src(CONFIG.PATHS.styles.src)
.pipe(sass({
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(gulp.dest(CONFIG.PATHS.styles.dest))
.pipe(hash())
.pipe(gulp.dest(CONFIG.PATHS.styles.dest))
.pipe(hash.manifest('assetscss.json', true, 4))
.pipe(gulp.dest(CONFIG.DATAFOLDER));
});
gulp.task('dev:sass', `Sass build (dev task, sourcemaps included)`, ['clean:css'], () => {
return gulp
.src(CONFIG.PATHS.styles.src)
.pipe(sourcemaps.init())
.pipe(sass({
outputStyle: 'compressed'
}).on('error', sass.logError))
.pipe(sourcemaps.write())
.pipe(gulp.dest(CONFIG.PATHS.styles.dest))
.pipe(browserSync.stream())
.pipe(hash())
.pipe(gulp.dest(CONFIG.PATHS.styles.dest))
.pipe(hash.manifest('assetscss.json', true, 4))
.pipe(gulp.dest(CONFIG.DATAFOLDER));
});
/*************************************************
HUGO
**************************************************/
gulp.task('build:hugo', `Build`, [], done => {
hugo.build(PUBLISH_DRAFTS, done);
});
/*************************************************
MAIN BUILD TASKS
**************************************************/
gulp.task('build', `BUILD. Used for production`, done => {
runSequence('clean', 'write:mappings', ['build:menu', 'build:sass', 'build:js'], 'write:assetmappings', 'build:hugo', 'pdf', 'check', (err) => {
//if any error happened in the previous tasks, exit with a code > 0
if (err) {
var exitCode = 2;
gutil.log('[ERROR] gulp build task failed:', red(err.message));
gutil.log('[FAIL] gulp build task failed - exiting with code ' + exitCode);
return process.exit(exitCode);
}
else {
return done();
}
});
});
/*************************************************
MAIN SERVE TASK
**************************************************/
gulp.task('dev', ``, ['dev:sass', 'build:js', 'write:menu', 'build:hugo'], done => {
server.spawn(CONFIG.CURRENTFOLDER);
jsonServer.spawn(CONFIG.CURRENTFOLDER);
hugo.spawn({
watch: true,
drafts: PUBLISH_DRAFTS
}, false, browserSync);
browserSync.init({
port: CONFIG.PORT,
proxy: 'localhost:8888',
online: false,
open: false
});
gulp.watch(CONFIG.PATHS.styles.src, ['dev:sass']);
gulp.watch(CONFIG.PATHS.scripts.src, ['js-watch']);
gutil.log(`\n\n*********\nOpen your browser with this address: ${cyan(`localhost:${CONFIG.PORT}`)}\n*********\n`);
});
gulp.task('serve', `Serve`, done => {
runSequence('clean', ['build:menu', 'build:sass', 'build:js'], 'dev');
})
/*************************************************
JSON SERVER
**************************************************/
gulp.task('json', `Run JSON export server`, ['dev:sass', 'build:js', 'write:menu', 'build:hugo'], done => {
jsonServer.spawn(CONFIG.CURRENTFOLDER);
})
/*************************************************
HTML PROOFER
**************************************************/
gulp.task('check:html', `Check HTML files in the build folder`, done => {
htmlproofer.check({
external: !!process.env.EXTERNAL,
dir: path.resolve(CONFIG.CURRENTFOLDER, '_site'),
callback: function (err) {
if (err) {
return process.exit(2);
} else {
done();
}
}
});
});
/*************************************************
MAIN CHECK TASK
**************************************************/
gulp.task('check', `Test the html and menu`, done => {
runSequence('check:html', 'check:menu', done);
});
/*************************************************
ALGOLIA
**************************************************/
gulp.task('algolia', `Push Algolia indexes`, done => {
algolia.run({
target : path.resolve(CONFIG.CURRENTFOLDER, '_site'),
source : CONFIG.CONTENTFOLDER,
spacesFile: path.resolve(CONFIG.CURRENTFOLDER, 'data/spaces.yml'),
algolia_app_id: CONFIG.ALGOLIA_APP_ID,
algolia_index: CONFIG.ALGOLIA_INDEX,
cb: done
});
});
/*************************************************
PDFS
**************************************************/
gulp.task('pdf', `Generate PDFs`, done => {
const bestPracticesFolder = 'best-practices';
generatePDF({
src: path.join(CONFIG.CONTENTFOLDER, bestPracticesFolder),
dist: path.join(CONFIG.DIST_FOLDER, bestPracticesFolder),
drafts: PUBLISH_DRAFTS,
cb: (err) => {
if (err) {
return process.exit(2);
} else {
done();
}
}
});
})