( dataFolder: string, _path: string, include?: string[], language?: string, )
| 2 | import fse from "fs-extra"; |
| 3 | |
| 4 | export const getEntry = <T = Record<string, unknown>>( |
| 5 | dataFolder: string, |
| 6 | _path: string, |
| 7 | include?: string[], |
| 8 | language?: string, |
| 9 | ) => { |
| 10 | const path = join(dataFolder, "models", _path); |
| 11 | // Entry doesn't exist |
| 12 | if (!fse.existsSync(path)) |
| 13 | return { |
| 14 | error: { |
| 15 | code: 404, |
| 16 | }, |
| 17 | }; |
| 18 | |
| 19 | let entry = {}; |
| 20 | |
| 21 | // Read info.json |
| 22 | let info = { |
| 23 | ...fse.readJsonSync(`${path}/info.json`), |
| 24 | slug: _path.substring(_path.indexOf("/") + 1), |
| 25 | }; |
| 26 | |
| 27 | // check if the info file contain a title for |
| 28 | // the given language |
| 29 | const INFO_CONTAINS_LANG_TITLE = language && info[language]?.title; |
| 30 | info = INFO_CONTAINS_LANG_TITLE ? info[language] : info; |
| 31 | |
| 32 | // Filter properties |
| 33 | |
| 34 | if (!include) { |
| 35 | entry = { ...info }; |
| 36 | } else { |
| 37 | entry = Object.keys(info) |
| 38 | .filter((key) => include.includes(key)) |
| 39 | .reduce( |
| 40 | (obj, key) => ({ |
| 41 | ...obj, |
| 42 | [key]: info[key], |
| 43 | }), |
| 44 | {}, |
| 45 | ); |
| 46 | } |
| 47 | |
| 48 | // Read content.md |
| 49 | if ( |
| 50 | (!include || include.includes("content")) && |
| 51 | fse.existsSync(`${path}/content.md`) && |
| 52 | !INFO_CONTAINS_LANG_TITLE |
| 53 | ) |
| 54 | entry = { |
| 55 | ...entry, |
| 56 | content: String(fse.readFileSync(`${path}/content.md`)), |
| 57 | }; |
| 58 | |
| 59 | // Return the Entry |
| 60 | return entry as T; |
| 61 | }; |
no outgoing calls
no test coverage detected