|
| 1 | +// import fg from 'fast-glob'; |
| 2 | +// import { pascalCase } from 'change-case'; |
| 3 | +// import { promises as fs } from 'fs'; |
| 4 | +// import path from 'path'; |
| 5 | + |
| 6 | +var { default: fg } = await import("fast-glob"); |
| 7 | +var { pascalCase } = await import("change-case"); |
| 8 | +var { promises: fs } = await import("fs"); |
| 9 | +var { default: path } = await import("path"); |
| 10 | + |
| 11 | +var inputDir = '/home/srghma/projects/faker/src/locales'; |
| 12 | +var outputDir = '/home/srghma/projects/purescript-fakerjs/src/Fakerjs2/Locales'; |
| 13 | + |
| 14 | +// Find all .ts files except index.ts and metadata.ts |
| 15 | +var entries = await fg('**/*.ts', { |
| 16 | + cwd: inputDir, |
| 17 | + ignore: ['**/index.ts', '**/metadata.ts'], |
| 18 | +}); |
| 19 | + |
| 20 | +// Step 1: Build a naive tree (pure objects with `true`) |
| 21 | +const tree = {}; |
| 22 | +for (const filepath of entries) { |
| 23 | + const parts = filepath.split(path.sep); |
| 24 | + const filename = path.basename(parts.pop(), '.ts'); |
| 25 | + const pathParts = parts; |
| 26 | + setDeep(tree, [...pathParts, filename], true); |
| 27 | +} |
| 28 | + |
| 29 | +function setDeep(obj, parts, value) { |
| 30 | + const last = parts.pop(); |
| 31 | + let curr = obj; |
| 32 | + for (const part of parts) { |
| 33 | + curr[part] = curr[part] || {}; |
| 34 | + curr = curr[part]; |
| 35 | + } |
| 36 | + curr[last] = value; |
| 37 | +} |
| 38 | + |
| 39 | +// Step 2: Postprocess tree into "smart" format (arrays for leaves) |
| 40 | +function postprocess(node) { |
| 41 | + if (typeof node !== 'object' || node === null) { |
| 42 | + return node; |
| 43 | + } |
| 44 | + |
| 45 | + const entries = Object.entries(node); |
| 46 | + const onlyFiles = entries.every(([, value]) => value === true); |
| 47 | + |
| 48 | + if (onlyFiles) { |
| 49 | + return entries.map(([name]) => name); // convert into array |
| 50 | + } |
| 51 | + |
| 52 | + const result = {}; |
| 53 | + for (const [key, value] of entries) { |
| 54 | + result[key] = postprocess(value); |
| 55 | + } |
| 56 | + return result; |
| 57 | +} |
| 58 | + |
| 59 | +const smartTree = postprocess(tree); |
| 60 | + |
| 61 | +// Done! Now smartTree looks like you want: |
| 62 | +// folders -> { folders or arrays of files } |
| 63 | + |
| 64 | +console.dir(smartTree, { depth: null }); |
| 65 | + |
| 66 | +await walkAndGenerate(tree, []); |
| 67 | + |
| 68 | +await Promise.all( |
| 69 | + Object.entries(filesByDir).map(async ([dir, files]) => { |
| 70 | + var moduleName = dir.split(path.sep).map(pascalCase).join('.'); |
| 71 | + var targetDir = path.join(outputDir, dir); |
| 72 | + var baseName = pascalCase(path.basename(dir)); |
| 73 | + |
| 74 | + var pursFilePath = path.join(outputDir, `${dir}.purs`); |
| 75 | + var jsFilePath = path.join(outputDir, `${dir}.js`); |
| 76 | + |
| 77 | + // Prepare contents |
| 78 | + var pursLines = [ |
| 79 | + `module Fakerjs2.Locales.${moduleName} where`, |
| 80 | + '', |
| 81 | + '-- This file was generated by a script', |
| 82 | + 'import Prelude', |
| 83 | + 'import Fakerjs2.Types', |
| 84 | + '', |
| 85 | + ...files.map(file => `foreign import ${file} :: ${pascalCase(file.replace(/_/g, ' ')).replace(/\s+/g, '')}`), |
| 86 | + ]; |
| 87 | + |
| 88 | + var jsLines = files.map(file => |
| 89 | + `export { default as ${file} } from '...'` |
| 90 | + ); |
| 91 | + |
| 92 | + // Ensure directory exists |
| 93 | + await fs.mkdir(path.dirname(pursFilePath), { recursive: true }); |
| 94 | + await fs.mkdir(path.dirname(jsFilePath), { recursive: true }); |
| 95 | + |
| 96 | + // Write files |
| 97 | + await Promise.all([ |
| 98 | + fs.writeFile(pursFilePath, pursLines.join('\n') + '\n'), |
| 99 | + fs.writeFile(jsFilePath, jsLines.join('\n') + '\n'), |
| 100 | + ]); |
| 101 | + }) |
| 102 | +); |
0 commit comments