MCPcopy Create free account
hub / github.com/dzcode-io/dzcode.io / getCollection

Function getCollection

data/src/get/collection.ts:11–56  ·  view source on GitHub ↗
(
  dataFolder: string,
  collectionType: string,
  collectionName: string,
  language?: string,
)

Source from the content-addressed store, hash-verified

9}
10
11export const getCollection = <T = Record<string, unknown>>(
12 dataFolder: string,
13 collectionType: string,
14 collectionName: string,
15 language?: string,
16) => {
17 // add .c
18 collectionName = collectionName.replace(".c.json", ".json");
19 // Collection doesn't exist
20 const path = join(dataFolder, "models", collectionType, collectionName);
21 if (!fse.existsSync(path)) return 404;
22
23 // Read [collection].json
24 const collection: Collection = fse.readJsonSync(path);
25 let items: string[] = [];
26
27 if (collection.items === "all") {
28 const files = glob.sync(join(dataFolder, "models", `/${collectionType}/**/info.json`));
29 const dPath = `data/models/${collectionType}/`;
30 items = files.map((filePath) => {
31 return filePath.substring(
32 filePath.lastIndexOf(dPath) + dPath.length,
33 filePath.lastIndexOf("/info.json"),
34 );
35 });
36 } else {
37 items = collection.items;
38 }
39
40 // Collect Entries
41 const entries = items.map((slug) => {
42 const entry = getEntry<T>(
43 dataFolder,
44 `${collectionType}/${slug}`,
45 collection.include,
46 language,
47 );
48 return {
49 slug,
50 ...entry,
51 };
52 });
53
54 // Return matched Entries of the Collection
55 return entries as T[];
56};

Callers 5

index.tsFile · 0.90
build.tsFile · 0.90
constructorMethod · 0.90
constructorMethod · 0.90
webpack.plugins.tsFile · 0.90

Calls 1

getEntryFunction · 0.90

Tested by

no test coverage detected