forked from arno-di-loreto/openapi-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gulpfile.js
111 lines (97 loc) · 3.19 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
const gulp = require('gulp');
const concat = require('gulp-concat');
const yamltojson = require('gulp-yaml');
const prettify = require('gulp-jsbeautifier');
const handlebars = require('gulp-handlebars');
const wrap = require('gulp-wrap');
const declare = require('gulp-declare');
const del = require('del');
const webserver = require('gulp-webserver');
// https://github.com/gulpjs/gulp/blob/master/docs/recipes/running-task-steps-per-folder.md
const fs = require('fs');
const path = require('path');
const merge = require('merge-stream');
const gulpSequence = require('gulp-sequence');
//
const filelist = require('gulp-filelist');
/**
* @description List directories in a directory
* @param {string} dir The directory
* @return {string[]} The directories
*/
function getFolders(dir) {
return fs.readdirSync(dir)
.filter(function(file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
// Concat data/<each version>/**/*.yaml in <each version>.json
gulp.task('build-data-files', function() {
const dataPath = 'data';
const folders = getFolders(dataPath);
var tasks = folders.map(function(folder) {
return gulp.src(path.join(dataPath, folder, '/**/*.yaml'))
.pipe(concat(folder + '.yaml'))
.pipe(yamltojson(folder + '.json'))
.pipe(prettify(folder + '.json'))
.pipe(gulp.dest('dist'));
});
return merge(tasks);
});
// Copy versions.json file to dist
gulp.task('build-version-file', function() {
return gulp.src('data/*.json')
.pipe(gulp.dest('dist'));
});
// Build versions.json and <each version>.json files
gulp.task('build-data', function(callback) {
gulpSequence('build-data-files', 'build-version-file')(callback);
});
// Generate js code for the tooltip panel's templating and copy to dist
gulp.task('templates', () => {
gulp.src('web/templates/*.hbs')
.pipe(handlebars({
handlebars: require('handlebars')
}))
.pipe(wrap('Handlebars.template(<%= contents %>)'))
.pipe(declare({
namespace: 'OpenAPISpecificationVisualDocumentation',
noRedeclare: true // Avoid duplicate declarations
}))
.pipe(concat('templates.js'))
.pipe(gulp.dest('dist/js'));
});
// Copy html, css dans js files to dist
gulp.task('static', () => {
return gulp.src('web/static/**/*')
.pipe(gulp.dest('dist/'));
});
// Copy static and templated files to dist
gulp.task('build-web', ['static', 'templates']);
// Copy web and data files to dist
gulp.task('build', function(callback) {
gulpSequence('clean', 'build-web', 'build-data')(callback);
});
// Deletes dist
gulp.task('clean', del.bind(null, ['dist']));
// Watch for modifications on web and data files, relaunch build if files are modified
gulp.task('watch', ['build'], function() {
return gulp.watch(['data/**/*', 'web/**/*'], ['build']);
});
// Launch web server on dist directory with live-reload
gulp.task('webserver', function() {
return gulp.src(['dist'])
.pipe(webserver({
port: 8080,
livereload: true,
directoryListing: false,
open: true,
fallback: 'index.html'
}));
});
// Launch dev mode
gulp.task('serve', function(callback) {
gulpSequence('watch', 'webserver')(callback);
});
// Build by default
gulp.task('default', ['build']);