( dataFolder: string, collectionType: string, collectionName: string, language?: string, )
| 9 | } |
| 10 | |
| 11 | export 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 | }; |
no test coverage detected