* Extracts the component status for each file in the given directory. * * @param filenames Array of filenames to read front-matter from * @param dir Absolute path to directory containing files * @returns A promise that resolves to an array containing outcome of file front-matter extraction
(filenames: string[], dir: string)
| 19 | * @returns A promise that resolves to an array containing outcome of file front-matter extraction |
| 20 | */ |
| 21 | function getComponentStatuses(filenames: string[], dir: string) { |
| 22 | const promises: Promise<ComponentStatus | null>[] = [] |
| 23 | |
| 24 | const handleCallback = ( |
| 25 | filename: string, |
| 26 | resolve: (value: ComponentStatus | null) => void, |
| 27 | reject: (value: unknown) => void, |
| 28 | ) => { |
| 29 | fs.readFile(path.resolve(dir, filename), 'utf-8', (err, content) => { |
| 30 | if (err) return reject(err) |
| 31 | |
| 32 | if (fm.test(content)) { |
| 33 | const { |
| 34 | attributes: {title, status}, |
| 35 | } = fm(content) |
| 36 | |
| 37 | if (status) { |
| 38 | return resolve({[title]: status}) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | resolve(null) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | for (const filename of filenames) { |
| 47 | const promise: Promise<ComponentStatus | null> = new Promise((resolve, reject) => { |
| 48 | return handleCallback(filename, resolve, reject) |
| 49 | }) |
| 50 | promises.push(promise) |
| 51 | } |
| 52 | |
| 53 | return Promise.all(promises) |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Orchestrates the process of reading component status for each file in the given directory. |
no test coverage detected