|
| 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 gulp = require("gulp"); |
| 7 | +const yaml = require("yaml-js"); |
| 8 | + |
| 9 | +let filename = "dev-site.yml"; |
| 10 | +let args = ["--playbook", filename]; |
| 11 | + |
| 12 | +gulp.task("build", function(cb) { |
| 13 | + /** |
| 14 | + * Use the '@antora/site-generator-default' node module to build. |
| 15 | + * It's analogous to `$ antora --playbook local-antora-playbook.yml`. |
| 16 | + * Having access to the generator in code may be useful for other |
| 17 | + * reasons in the future (i.e to implement custom features). |
| 18 | + * NOTE: As opposed to building with the CLI, this method doesn't use |
| 19 | + * a separate process for each run. So if a build error occurs with the `gulp` |
| 20 | + * command it can be useful to check if it also happens with the CLI command. |
| 21 | + */ |
| 22 | + generator(args, process.env) |
| 23 | + .then(() => { |
| 24 | + cb(); |
| 25 | + }) |
| 26 | + .catch(err => { |
| 27 | + console.log(err); |
| 28 | + cb(); |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +gulp.task("preview", ["build"], function() { |
| 33 | + /** |
| 34 | + * Remove the line gulp.src('README.adoc') |
| 35 | + * This is placeholder code to follow the gulp-connect |
| 36 | + * example. Could not make it work any other way. |
| 37 | + */ |
| 38 | + gulp.src("README.adoc").pipe(connect.reload()); |
| 39 | +}); |
| 40 | + |
| 41 | +gulp.task("watch", function() { |
| 42 | + let json_content = fs.readFileSync(`${__dirname}/${filename}`, "UTF-8"); |
| 43 | + let yaml_content = yaml.load(json_content); |
| 44 | + let dirs = yaml_content.content.sources.map(source => [ |
| 45 | + `${source.url}/**/**.yml`, |
| 46 | + `${source.url}/**/**.adoc` |
| 47 | + ]); |
| 48 | + dirs.push(["dev-site.yml"]); |
| 49 | + gulp.watch(dirs, ["preview"]); |
| 50 | +}); |
| 51 | + |
| 52 | +gulp.task("connect", function() { |
| 53 | + connect.server({ |
| 54 | + port: 5353, |
| 55 | + name: "Dev Server", |
| 56 | + livereload: true, |
| 57 | + root: "gh-pages" |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +gulp.task("default", ["connect", "watch", "build"]); |
0 commit comments