forked from redhat-developer-demos/knative-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-tasks.ts
138 lines (121 loc) · 3.69 KB
/
build-tasks.ts
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
import { remove } from "fs-extra"
import { readFileSync, readFile, writeFileSync } from "fs";
import { task, series, watch } from 'gulp'
import * as generator from "@antora/site-generator-default"
import { resolve } from "path"
import { load } from "js-yaml"
import { create, BrowserSyncInstance } from "browser-sync"
interface BuildConfig {
srcDir: string;
outputDir: string;
playbook: string;
cacheDir: string;
[x: string]: unknown;
$0: string;
_: (string | number)[];
}
//Antora Published Site info
interface PublishedSite {
provider: string;
path: string;
resolvedPath: string;
fileUri: string;
}
export class BuildTasks {
protected buildConfig: BuildConfig;
cleanBuild: string[] = ["cleanCache", "cleanSite", "build"];
devMode: string[] = ["clean", "build", "serve", "watch"];
cleanAll: string[] = ["cleanCache", "cleanSite"];
siteOutDir: string;
playbookFile: string;
watchGlobs: string[];
server: BrowserSyncInstance;
constructor(buildConfig: BuildConfig) {
this.buildConfig = buildConfig;
this.siteOutDir = resolve(`${buildConfig.srcDir}/${buildConfig.outputDir}`);
this.playbookFile = resolve(`${buildConfig.srcDir}/${buildConfig.playbook}`);
this.watchGlobs = this.parsePlaybook();
this.server = create();
}
public configure() {
//individual tasks
task("cleanCache", this.cleanCache.bind(this));
task("cleanSite", this.cleanSite.bind(this));
task("serve", this.serve.bind(this));
task("build", this.build.bind(this));
task("reload", this.reload.bind(this));
task("watch", this.watch.bind(this));
//chained tasks
task("clean", series(this.cleanAll));
task("cleanBuild", series(this.cleanBuild));
task("default", series(this.devMode));
}
cleanCache(cb: Function) {
remove(this.buildConfig.cacheDir)
.then(() => cb())
.catch(err => console.log("Error cleaning cache directory", err));
}
cleanSite(cb: Function) {
remove(this.buildConfig.cacheDir)
.then(() => cb())
.catch(err => console.log("Error cleaning site directory ", err));
}
build(cb: Function) {
console.log(`Building site ${this.playbookFile}`);
const args: string[] = ["--playbook", this.playbookFile, "--redirect_facility", "static"]
generator(args, process.env)
.then((ps) => {
this.patchIndexFile(ps);
cb();
}).catch(err => {
console.log("Error building site ", err);
cb();
});
}
serve(cb: Function) {
this.server.init({
open: false,
host: "0.0.0.0",
server: {
baseDir: this.siteOutDir
}
}, () => {
cb();
});
}
reload(cb: Function) {
this.server.reload()
cb();
}
watch(cb: Function) {
watch(this.watchGlobs, series("build", "reload"))
}
private patchIndexFile(ps: PublishedSite[]): void {
//console.log(JSON.stringify(ps));
const indexFile = `${ps[0].resolvedPath}/index.html`;
//console.log("Index file:", indexFile);
readFile(indexFile, "utf8", (err, data) => {
if (err) {
console.log(err);
}
let fileC = data;
fileC = fileC.replace(/^(<script>location=)(.*)(<\/script>)/gm, "\$1\$2 + window.location.search \$3");
writeFileSync(indexFile, fileC);
});
}
private parsePlaybook(): string[] {
let json_content = readFileSync(this.playbookFile, "utf-8");
let yaml_content = load(json_content);
let dirs = yaml_content.content.sources.map(source => {
return [
resolve(`${source.url}/**/*.yml`),
resolve(`${source.url}/**/*.hbs`),
resolve(`${source.url}/**/*.adoc`)
]
});
dirs.push(this.playbookFile)
dirs = [].concat(...dirs);
//console.log(dirs);
return dirs;
}
}