-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrichie.ts
72 lines (58 loc) · 2 KB
/
richie.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
import { readFile } from "node:fs/promises";
import configurations from "./configLoader";
import functionMapper from "./lib/function-map";
import { createJsonLD, writeOutput } from "./lib/node-utils";
import { sweep } from "./lib/sweeper";
import {
configurationOptions,
richieGroupA,
richieGroupB,
richieGroupC,
richies,
} from "./lib/types";
export async function richie(
richieNames: richies[],
filepath: string,
destinationPath: string = "",
): Promise<void> {
const functionMap = functionMapper(configurations());
const destinationFile: string =
!!destinationPath ? destinationPath : filepath;
const source: string = await readFile(filepath, { encoding: "utf8" });
return new Promise(async (resolve, reject) => {
let richResultSnippets: string = "";
let cleanSource: string = source;
for (const richieName of richieNames) {
//standardize parameters
const aggregatorParams: string[] | boolean =
richieGroupA.includes(richieName) ? [source]
: richieGroupB.includes(richieName) ? [source, filepath]
: richieGroupC.includes(richieName) ? [filepath]
: false;
if (!aggregatorParams) {
reject(new Error("Unsupported Richie name"));
} else {
const aggregator: Function = functionMap[richieName].aggregator;
const serializer: Function = functionMap[richieName].serializer;
const aggregatedData = await aggregator(...aggregatorParams);
const serializerParams: any[] =
richieName === "productwv" ?
[...Object.values(aggregatedData)] // [productMeta,variesBy]
: richieName === "product" ?
[Object.values(aggregatedData)[0]] // [productMeta]
: [aggregatedData];
const serializedData = serializer(...serializerParams);
richResultSnippets += createJsonLD(serializedData);
cleanSource = sweep(richieName, cleanSource);
}
}
writeOutput(cleanSource, destinationFile, richResultSnippets)
.then(() => {
resolve();
})
.catch((error) => {
reject(error);
});
});
}
export type rjsOptions = configurationOptions;