Skip to content

Commit cd2fa66

Browse files
author
Luke Schierer
committed
build: external source plugin
this will be the right solution after ProjectEvergreen/greenwood#1296 is closed out and solved.
1 parent 6103460 commit cd2fa66

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { General } from "../../schemas/generals";
2+
import collection from "../../assets/collections/generals/collection";
3+
4+
import debugFunction from "../../lib/debug";
5+
const DEBUG = debugFunction("plugins/collections/generals");
6+
7+
export const GeneralsSourcesPlugin = () => {
8+
return {
9+
type: "source",
10+
name: "source-plugin-generals",
11+
provider: () => {
12+
return async function () {
13+
const generals = [];
14+
await Promise.all(
15+
collection.map(async (item) => {
16+
if (DEBUG) {
17+
console.log(`item is ${item}`);
18+
}
19+
await import(`../../assets/collections/generals/${item}`, {
20+
with: { type: "json" },
21+
})
22+
.then((jsonData) => {
23+
const keys = Object.keys(jsonData);
24+
if (keys.includes("default")) {
25+
const valid = General.safeParse(jsonData["default"]);
26+
if (valid.success) {
27+
generals.push(valid.data);
28+
} else {
29+
if (DEBUG) {
30+
console.error(
31+
`error parsing ${item}, `,
32+
valid.error.message
33+
);
34+
console.error(JSON.stringify(jsonData));
35+
}
36+
}
37+
}
38+
})
39+
.catch((error) => {
40+
console.error(
41+
`failed to load file for ${item}, `,
42+
`error is: `,
43+
JSON.stringify(error)
44+
);
45+
});
46+
})
47+
);
48+
return generals.map((item) => {
49+
const id = item.id;
50+
const route = `/generals/details/${id.toLowerCase()}/`;
51+
52+
return {
53+
title: id,
54+
body: getBody(item),
55+
route,
56+
id,
57+
label: id,
58+
};
59+
});
60+
};
61+
},
62+
};
63+
};
64+
65+
const getBody = (general) => {
66+
return `
67+
<h2 class="spectrum-Heading spectrum-Heading--sizeM">
68+
${general.id}
69+
</h2>
70+
`;
71+
};

0 commit comments

Comments
 (0)