-
Notifications
You must be signed in to change notification settings - Fork 84
/
postBuildFactory.ts
37 lines (27 loc) · 1.13 KB
/
postBuildFactory.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
import fs from "fs";
import path from "path";
import util from "util";
import { ProcessedPluginOptions, PostBuildData } from "../../shared/interfaces";
import { buildIndex } from "./buildIndex";
import { debugInfo } from "./debug";
import { processDocInfos } from "./processDocInfos";
import { scanDocuments } from "./scanDocuments";
const writeFileAsync = util.promisify(fs.writeFile);
export function postBuildFactory(config: ProcessedPluginOptions) {
return async function postBuild(buildData: PostBuildData): Promise<void> {
debugInfo("gathering documents");
const data = processDocInfos(buildData, config);
debugInfo("parsing documents");
// Give every index entry a unique id so that the index does not need to store long URLs.
const allDocuments = await scanDocuments(data);
debugInfo("building index");
const searchIndex = buildIndex(allDocuments, config);
debugInfo("writing index to disk");
await writeFileAsync(
path.join(buildData.outDir, "search-index.json"),
JSON.stringify(searchIndex),
{ encoding: "utf8" }
);
debugInfo("index written to disk successfully!");
};
}