forked from d3v2a/vue-typed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
80 lines (64 loc) · 1.78 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
var browserSync = require('browser-sync').create()
var reload = browserSync.reload
var gulp = require('gulp')
var gitbook = require('gitbook')
var path = require('path')
var del = require('del')
// path of your *.md book files
var rootPath = path.join(__dirname, 'docs')
// output path where generated *.html will be stored
var outputPath = path.join(rootPath, '_book')
// gulp.watch instance
var watcher
// rebuild book + start server
gulp.task('default', function (done) {
rebuildBook(function () {
browserSync.init({
server: {
baseDir: outputPath
}
})
done()
})
})
// kill watcher + remove/clean output dir + build book
function rebuildBook (done) {
// pause watcher while building book progress
if (watcher) {
watcher.end()
watcher = undefined;
}
// ensure output path is not exists otherwise book won't generated
return del(path.join(outputPath)).then(paths => {
buildBook().then(function () {
// start watcher after built
startWatch().on('ready', function(){
done();
}).on('error', function(e){
console.error('ERROR: watch error... $s', e)
})
})
})
}
// build book
function buildBook () {
var fs = gitbook.createNodeFS(rootPath)
var book = gitbook.Book.createForFS(fs)
var Parse = gitbook.Parse
var Output = gitbook.Output
var Generator = Output.getGenerator('website')
return Parse.parseBook(book)
.then(function (resultBook) {
return Output.generate(Generator, resultBook, {
root: outputPath
})
})
}
// start watching md files + when files changed reload browser
function startWatch() {
var rp = path.join(rootPath, '/**/*.md')
watcher = gulp.watch([rp, '!' + rp + '/_book/' ], function () {
rebuildBook(reload)
})
return watcher;
}