|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const connect = require('gulp-connect') |
| 4 | +const fs = require('fs') |
| 5 | +const generator = require('@antora/site-generator-default') |
| 6 | +const { reload: livereload } = process.env.LIVERELOAD === 'true' ? require('gulp-connect') : {} |
| 7 | +const { series, src, watch } = require('gulp') |
| 8 | +const yaml = require('js-yaml') |
| 9 | + |
| 10 | +const playbookFilename = 'antora-playbook.yml' |
| 11 | +const playbook = yaml.load(fs.readFileSync(playbookFilename, 'utf8')) |
| 12 | +const outputDir = (playbook.output || {}).dir || './build/site' |
| 13 | +const serverConfig = { name: 'Preview Site', livereload, port: 5000, root: outputDir } |
| 14 | +const antoraArgs = ['--playbook', playbookFilename] |
| 15 | +const watchPatterns = playbook.content.sources.filter((source) => !source.url.includes(':')).reduce((accum, source) => { |
| 16 | + accum.push(`${source.url}/${source.start_path ? source.start_path + '/' : ''}antora.yml`) |
| 17 | + accum.push(`${source.url}/${source.start_path ? source.start_path + '/' : ''}**/*.adoc`) |
| 18 | + return accum |
| 19 | +}, []) |
| 20 | + |
| 21 | +function generate (done) { |
| 22 | + generator(antoraArgs, process.env) |
| 23 | + .then(() => done()) |
| 24 | + .catch((err) => { |
| 25 | + console.log(err) |
| 26 | + done() |
| 27 | + }) |
| 28 | +} |
| 29 | + |
| 30 | +function serve (done) { |
| 31 | + connect.server(serverConfig, function () { |
| 32 | + this.server.on('close', done) |
| 33 | + watch(watchPatterns, generate) |
| 34 | + if (livereload) watch(this.root).on('change', (filepath) => src(filepath, { read: false }).pipe(livereload())) |
| 35 | + }) |
| 36 | +} |
| 37 | + |
| 38 | +module.exports = { serve, generate, default: series(generate, serve) } |
0 commit comments