diff options
Diffstat (limited to 'packages/assets/scripts/generate.mjs')
-rw-r--r-- | packages/assets/scripts/generate.mjs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/packages/assets/scripts/generate.mjs b/packages/assets/scripts/generate.mjs new file mode 100644 index 0000000..44ba4e4 --- /dev/null +++ b/packages/assets/scripts/generate.mjs @@ -0,0 +1,62 @@ +import fs from 'node:fs/promises'; +import { dirname, join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import prettier from 'prettier'; + +const assetFolders = ['icons', 'images', 'svgs/brands', 'svgs/ext/Extras', 'svgs/ext/Code']; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +prettier.resolveConfig(join(__dirname, '..', '..', '..', '.prettierrc.js')).then((options) => + 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 = await prettier.format( + ` + /* + * This file was automatically generated by a script. + * To regenerate this file, run: pnpm assets gen + */ + + ${assetImports} + + export { + ${assetExports} + };`, + { ...options, parser: 'typescript' } + ); + + await fs.writeFile(indexFilePath, indexFileContent); + }) + ) +); |