| 8 | import { getDirectories } from './utils'; |
| 9 | |
| 10 | export const verifyFilenames = async (metadata: Metadata, dir: string) => { |
| 11 | // Read all the filenames in the files directory |
| 12 | const filenames = await fs.readdir(path.join(dir, 'files')); |
| 13 | |
| 14 | // Make sure filenames is not empty |
| 15 | if (filenames.length === 0) { |
| 16 | throw new Error('No files found in the files directory.'); |
| 17 | } |
| 18 | |
| 19 | // Generate a list of expected filenames |
| 20 | const expectedFilenames: string[] = []; |
| 21 | |
| 22 | // Iterate over all subsets |
| 23 | for (const subset of metadata.subsets) { |
| 24 | // Iterate over all weights |
| 25 | for (const weight of metadata.weights) { |
| 26 | // Iterate over all styles |
| 27 | for (const style of metadata.styles) { |
| 28 | // Add the expected filename to the list |
| 29 | expectedFilenames.push( |
| 30 | `${metadata.id}-${subset}-${weight}-${style}.woff2`, |
| 31 | `${metadata.id}-${subset}-${weight}-${style}.woff`, |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | // Check if all expected filenames are present and show all missing or non-matching filenames |
| 38 | const missingFilenames = expectedFilenames.filter( |
| 39 | (filename) => !filenames.includes(filename), |
| 40 | ); |
| 41 | if (missingFilenames.length > 0) { |
| 42 | throw new Error( |
| 43 | `The following files are missing: ${missingFilenames.join( |
| 44 | ', ', |
| 45 | )}\n\n\tPlease ensure the file names match the format "${ |
| 46 | metadata.id |
| 47 | }-subset-weight-style.extension"\n\tExample: "${ |
| 48 | metadata.id |
| 49 | }-latin-400-normal.woff2" or "${metadata.id}-latin-ext-700-italic.woff"`, |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | // Check if all filenames are expected and show all non-matching filenames |
| 54 | const nonMatchingFilenames = filenames.filter( |
| 55 | (filename) => !expectedFilenames.includes(filename), |
| 56 | ); |
| 57 | if (nonMatchingFilenames.length > 0) { |
| 58 | throw new Error( |
| 59 | `The following files are not expected: ${nonMatchingFilenames.join( |
| 60 | ', ', |
| 61 | )}\n\n\tPlease ensure the file names match the format "${ |
| 62 | metadata.id |
| 63 | }-subset-weight-style.extension"\n\tExample: "${ |
| 64 | metadata.id |
| 65 | }-latin-400-normal.woff2" or "${metadata.id}-latin-ext-700-italic.woff"`, |
| 66 | ); |
| 67 | } |