diff options
Diffstat (limited to 'packages/assets/scripts/generate.mjs')
-rw-r--r-- | packages/assets/scripts/generate.mjs | 79 |
1 files changed, 37 insertions, 42 deletions
diff --git a/packages/assets/scripts/generate.mjs b/packages/assets/scripts/generate.mjs index 44ba4e4..08fc2ba 100644 --- a/packages/assets/scripts/generate.mjs +++ b/packages/assets/scripts/generate.mjs @@ -1,48 +1,45 @@ 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( - ` +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 @@ -52,11 +49,9 @@ prettier.resolveConfig(join(__dirname, '..', '..', '..', '.prettierrc.js')).then export { ${assetExports} - };`, - { ...options, parser: 'typescript' } - ); + };` + ; - await fs.writeFile(indexFilePath, indexFileContent); - }) - ) + await fs.writeFile(indexFilePath, indexFileContent); + }), ); |