import fs from 'node:fs/promises'; import { dirname, join } from 'node:path'; import { fileURLToPath } from 'node:url'; const assetFolders = ['icons', 'images', 'svgs/brands', 'svgs/ext/Extras', 'svgs/ext/Code']; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); await Promise.all( assetFolders.map(async (folder) => { const indexFilePath = join(__dirname, '..', folder, 'index.ts'); const assetsFolderPath = join(__dirname, '..', folder); if ( await fs.access(indexFilePath).then( () => true, () => false, ) ) await fs.unlink(indexFilePath); const fileNames = await fs.readdir(assetsFolderPath); const assetImports = fileNames .filter(fileName => fileName !== 'index.ts' && !/(^|\/)\.[^\/\.]/g.test(fileName)) .map((fileName) => { const variableName = fileName.split('.')[0].replace(/-/g, ''); if (folder.startsWith('svgs')) return `import { ReactComponent as ${variableName} } from './${fileName}';`; return `import ${variableName} from './${fileName}';`; }) .join('\n'); const assetExports = fileNames .filter(fileName => fileName !== 'index.ts' && !/(^|\/)\.[^\/\.]/g.test(fileName)) .map(fileName => `${fileName.split('.')[0].replace(/-/g, '')}`) .join(',\n'); const indexFileContent = ` /* * This file was automatically generated by a script. * To regenerate this file, run: pnpm assets gen */ ${assetImports} export { ${assetExports} };` ; await fs.writeFile(indexFilePath, indexFileContent); }), );