-
Notifications
You must be signed in to change notification settings - Fork 3
/
gulpfile.js
140 lines (122 loc) · 4.39 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
'use strict';
/**
* Derived from https://github.com/couchbase/docs-site/blob/aa1f5e5a07027047eface38bc3ae34f0690a82d3/gulpfile.js
*/
const gulp = require('gulp');
const connect = require('gulp-connect');
const fs = require('fs');
const exitHook = require('exit-hook');
const open = require('open');
const yaml = require('js-yaml');
const commandLineArgs = require('command-line-args');
const generator = require('@antora/site-generator');
const dateTime = require('date-time');
const { doesNotMatch } = require('assert');
const optionDefinitions = [
{name: 'source', alias: 's', type: String, defaultValue: 'site.yml'},
{name: 'playbook', alias: 'p', type: String},
{name: 'branch', alias: 'b', type: String},
{name: 'dir', alias: 'd', type: String, multiple: true},
{name: 'output', alias: 'o', type: String, defaultValue: 'docs-dev'},
{name: 'keep', alias: 'k', type: Boolean, defaultValue: false}
];
let playbook;
let filename;
gulp.task('build', preparePlaybook(function (playbook, filename, done) {
/**
* Use the '@antora/site-generator-default' node module to build.
* It's analogous to `$ antora --playbook site-dev.yml`.
* Having access to the generator in code may be useful for other
* reasons in the future (i.e to implement custom features).
* NOTE: As opposed to building with the CLI, this method doesn't use
* a separate process for each run. So if a build error occurs with the `gulp`
* command it can be useful to check if it also happens with the CLI command.
*/
let args = [
"--playbook",
filename
];
process.env.IKE_DOCS_BUILT_AT = dateTime({local: false, showTimeZone: true});
generator(args, process.env).then(() => {
done();
}).catch(err => {
console.log(err);
done();
})
}));
gulp.task('preview', gulp.series('build', (done) => {
/**
* Remove the line gulp.src('README.adoc')
* This is placeholder code to follow the gulp-connect
* example. Could not make it work any other way.
*/
gulp.src('README.adoc').pipe(connect.reload());
done()
}));
gulp.task('watch', preparePlaybook(function (playbook, filename, done) {
const dirs = playbook.content.sources
.map(source => [
`${source.url}/**/**.yml`,
`${source.url}/**/**.adoc`,
`${source.url}/dist/ike`
]);
dirs.push([filename]);
gulp.watch(dirs, gulp.series('preview'));
}));
gulp.task('connect', preparePlaybook(function (playbook) {
const connectOptions = {
name: `${playbook.site.title} Antora Doc Server`,
port: 5353,
livereload: true,
root: playbook.output.dir,
};
connect.server(connectOptions);
open(`http://localhost:${connectOptions.port}`)
}));
gulp.task('open', (done) => {
open('docs/index.html');
done()
});
gulp.task('default', gulp.series('build', 'connect', 'watch'));
function preparePlaybook(func) {
// returns a function which will be called by gulp task
return function (cb) {
if (!!!playbook) {
const options = commandLineArgs(optionDefinitions, {partial: true});
filename = options.playbook;
playbook = yaml.safeLoad(fs.readFileSync(options.source, 'UTF-8'));
if (!!!filename) {
filename = loadDevPlaybook(options, playbook);
}
}
func(playbook, filename, cb);
}
}
// Uses playbook specified through p/playbook option or creates one on the fly
function loadDevPlaybook(options, playbook) {
if (!!!options.dir) {
console.log('local clone of the repository is not specified, please clone it and point to it using -d or --dir flag');
process.exit(-1);
}
const filename = `${__dirname}/site-local-dev.yml`;
if (fs.existsSync(filename)) {
// we can use it
// this check also avoids doing the same file manipulations over and over again for sequence of tasks
return filename
}
playbook.content.sources.forEach((e, i) => {
e.url = options.dir[i % options.dir.length];
if (!!options.branch) {
e.branches = [options.branch];
}
});
playbook.output.dir = options.output;
const yamlDoc = yaml.safeDump(playbook);
fs.writeFileSync(filename, yamlDoc);
if (!options.keep) {
exitHook(function () {
fs.unlinkSync(filename);
});
}
return filename
}