-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
200 lines (175 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
var path = require('path');
var process = require('process');
var rm = require('rimraf');
var gulp = require('gulp');
var gutil = require('gulp-util');
var inject = require('gulp-inject');
var cached = require('gulp-cached');
var changed = require('gulp-changed');
var plumber = require('gulp-plumber');
var aglio = require('gulp-aglio');
var drakov = require('drakov');
var browserSync = require('browser-sync').create();
const reload = browserSync.reload;
const PrjRoot = process.cwd();
const Config = {
entry: './index.html',
dirs: {
src: 'docs',
dist: 'dist'
},
// broswersync-config
serve: {
docServer: {
port: 3000,
server: {
baseDir: "./"
}
},
mockServer: {
//https://github.com/Aconex/drakov/issues/16
sourceFiles: "./docs/**/*.{md,apib}",
serverPort: 3002,
// staticPaths: [
// '/path/to/static/files',
// '/another/path/to/static/files',
// '/path/to/more/files=/mount/it/here'
// ],
stealthmode: true
}
}
}
const CACHE_KEYS = {
TRANSPILING: 'transpile-api-blueprint-docs',
INJECTING: 'inject-transpiled-api-blueprint-docs'
}
const AglioOptions = {
theme: 'default',
themeFullWidth: true,
themeTemplate: 'triple',
includePath: path.posix.join(PrjRoot, Config.dirs.src)
};
const transpileBluePrint = function transpileBluePrint(globs, options = AglioOptions, dist = Config.dirs.dist) {
let transpilePromise = new Promise((resolve, reject) => {
gulp.src(globs, { base: Config.dirs.src })
.pipe(plumber(function rejector(err) {
let { message } = err;
console.error(err);
this.emit('end', err);
}))
.pipe(cached(CACHE_KEYS['TRANSPILING'], {
optimizeMemory: true
}))
.pipe(aglio(options))
.pipe(changed(Config.dirs.dist, { extension: '.html', hasChanged: changed.compareContents }))
.pipe(gulp.dest(dist))
.on('end', function(err) {
if (err) {
reject(err)
} else {
resolve();
}
})
});
return transpilePromise;
}
const injectTranspiledDocs = function injectTranspiledDocs() {
let target = gulp.src(`${Config.entry}`);
let sources = gulp.src(`./${Config.dirs.dist}/**/*.html`, { read: false });
let transpiledPromise = new Promise((resolve, reject) => {
target
.pipe(plumber(function rejector(err) {
let { message } = err;
console.error(err);
this.emit('end', err);
}))
.pipe(cached(CACHE_KEYS['INJECTING']))
.pipe(inject(sources, {
relative: true,
transform: function(filePath, file, i, length) {
let fileName = path.relative(Config.dirs.dist, filePath);
// console.log(filePath, Config.dirs.dist, fileName);
return '<li><a href="' + filePath + '">' + fileName + '</a></li>';
}
}))
.pipe(gulp.dest('./'))
.on('end', function(err) {
if (err) {
reject(err)
} else {
resolve();
}
})
})
return transpiledPromise;
}
const del = function del(globs) {
let delPromise = new Promise((resolve, reject) => {
rm(globs, resolve)
}).catch(err => Promise.reject(err));
return delPromise
}
const serveDocs = function serveDocs(done) {
browserSync.init(Config.serve.docServer);
done();
}
const serveMocks = function serveDocs(done) {
// @TODO
drakov.run(Config.serve.mockServer);
done();
}
gulp.task('clean', gulp.series(function cleanDistDir() {
let globs = path.posix.join(PrjRoot, Config.dirs.dist, '/**/*');
let cleanPromise = del(globs).then(() => {
console.log(`${globs} were all cleaned ... `);
})
return cleanPromise;
}))
gulp.task('watch', gulp.series(function watchChanges(done) {
let globs = path.posix.join(Config.dirs.src, '/**/*.{md,apib}');
let watcher = gulp.watch(globs);
watcher
.on('add', function(filePath, stats) {
console.log(`${filePath} was added`);
transpileBluePrint(filePath)
.then(() => {
console.log(`${filePath} was transpiled`);
injectTranspiledDocs()
.then(reload)
.catch(reload);
})
})
.on('change', function(filePath, stats) {
console.log(`${filePath} was changed`);
transpileBluePrint(filePath)
.then(() => {
console.log(`${filePath} was transpiled`);
injectTranspiledDocs()
.then(reload)
.catch(reload);
})
})
.on('unlink', function(filePath, stats) {
console.log(`${filePath} was removed`);
let pathInfo = path.relative(Config.dirs.src, filePath);
let { dir, base, name } = path.parse(pathInfo);
let globs = path.posix.join(PrjRoot, Config.dirs.dist, dir, `${name}.html`);
del(globs)
.then(() => {
console.log(`${name}.html was removed in dist dir`);
injectTranspiledDocs()
.then(reload)
.catch(reload);
})
})
done();
}))
gulp.task('transpile', gulp.series(function transpile() {
let globs = path.posix.join(Config.dirs.src, '/**/*.{md,apib}');
return transpileBluePrint(globs, AglioOptions);
}));
gulp.task("inject", gulp.series(injectTranspiledDocs));
gulp.task("serve::docs", gulp.series(serveDocs));
gulp.task("serve::mocks", gulp.series(serveMocks));
gulp.task("serve", gulp.parallel('serve::docs', 'serve::mocks'));
gulp.task('default', gulp.series('clean', 'transpile', gulp.parallel('watch', 'inject'), 'serve'))