|
| 1 | +import { gray, green, italic } from 'colorette'; |
| 2 | +import { mkdir, opendir, readFile, writeFile } from 'node:fs/promises'; |
| 3 | +import { dirname, join, resolve } from 'node:path'; |
| 4 | +import { inspect } from 'node:util'; |
| 5 | + |
| 6 | +const ci = 'CI' in process.env && process.env.CI !== 'false'; |
| 7 | + |
| 8 | +const FileIcon = ci ? '📄' : '\uE628'; |
| 9 | +const DirectoryIcon = ci ? '📂' : '\uF413'; |
| 10 | + |
| 11 | +const i1 = '\t'; |
| 12 | +const i2 = i1.repeat(2); |
| 13 | +const i3 = i1.repeat(3); |
| 14 | + |
| 15 | +/** |
| 16 | + * Recursively walk through the directory and generate the types. |
| 17 | + * @param path The path to walk through. |
| 18 | + * @param namespace The namespace to use. |
| 19 | + */ |
| 20 | +async function recurse(lines: string[], path: string, namespace: string, options: GenerateOptions) { |
| 21 | + if (options.verbose) console.log(gray(`Reading directory ${DirectoryIcon} ${green(path)}...`)); |
| 22 | + |
| 23 | + for await (const dirent of await opendir(path)) { |
| 24 | + const file = join(path, dirent.name); |
| 25 | + if (dirent.isFile()) { |
| 26 | + if (!dirent.name.endsWith('.json')) continue; |
| 27 | + if (options.verbose) console.log(gray(`Processing ${FileIcon} ${green(file)}...`)); |
| 28 | + |
| 29 | + const name = dirent.name.slice(0, -5); |
| 30 | + const key = namespace ? `'${namespace}/${name}'` : name; |
| 31 | + const data = JSON.stringify(JSON.parse(await readFile(file, 'utf8')), undefined, i1); |
| 32 | + lines.push(`${i3}${key}: ${data.replaceAll('\n', `\n${i3}`)};`); |
| 33 | + } else if (dirent.isDirectory()) { |
| 34 | + await recurse(lines, file, namespace ? `${namespace}/${dirent.name}` : dirent.name, options); |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export async function generate([source, destination]: string[], options: GenerateOptions) { |
| 40 | + const sourceDirectory = resolve(source); |
| 41 | + const destinationFile = resolve(destination); |
| 42 | + |
| 43 | + if (options.verbose) { |
| 44 | + const lines = [ |
| 45 | + `Source: ${DirectoryIcon} ${green(sourceDirectory)}...`, |
| 46 | + `Output: ${FileIcon} ${green(destinationFile)}...`, |
| 47 | + '', |
| 48 | + 'Options:', |
| 49 | + ` - Verbose: ${green(options.verbose ? 'yes' : 'no')}` |
| 50 | + ]; |
| 51 | + console.log(italic(gray(lines.join('\n')))); |
| 52 | + } |
| 53 | + |
| 54 | + const lines = [ |
| 55 | + '// This file is automatically generated, do not edit it.', |
| 56 | + "import 'i18next';", |
| 57 | + '', |
| 58 | + "declare module 'i18next' {", |
| 59 | + `${i1}interface CustomTypeOptions {`, |
| 60 | + `${i2}resources: {` |
| 61 | + ]; |
| 62 | + |
| 63 | + await recurse(lines, sourceDirectory, '', options); |
| 64 | + lines.push(`${i2}};`, `${i1}}`, '}', ''); |
| 65 | + |
| 66 | + let generatedSource = lines.join('\n'); |
| 67 | + if (options.prettier) { |
| 68 | + if (options.verbose) console.log(gray(`Loading prettier...`)); |
| 69 | + |
| 70 | + const { resolveConfig, format } = await import('prettier'); |
| 71 | + const config = await resolveConfig(destinationFile); |
| 72 | + if (options.verbose) console.log(gray(`Formatting with prettier config: ${inspect(config, { colors: true })}`)); |
| 73 | + |
| 74 | + generatedSource = await format(generatedSource, { ...config, filepath: destinationFile }); |
| 75 | + } |
| 76 | + |
| 77 | + await mkdir(dirname(destinationFile), { recursive: true }); |
| 78 | + await writeFile(destinationFile, generatedSource, 'utf8'); |
| 79 | +} |
| 80 | + |
| 81 | +interface GenerateOptions { |
| 82 | + verbose: boolean; |
| 83 | + prettier: boolean; |
| 84 | +} |
0 commit comments