From f7226e3635f5726803e8a2fb77c2930ee918fe55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Wed, 12 Apr 2023 19:43:48 +0200 Subject: [PATCH] Automatically find subfolders --- .../tasks/__tests__/convertSvgToJsx.test.tsx | 2 +- .../scripts/prebuild/tasks/convertSvgToJsx.js | 54 +++++++++++++------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/packages/dnb-eufemia/scripts/prebuild/tasks/__tests__/convertSvgToJsx.test.tsx b/packages/dnb-eufemia/scripts/prebuild/tasks/__tests__/convertSvgToJsx.test.tsx index c40aca98783..39a9e732ee7 100644 --- a/packages/dnb-eufemia/scripts/prebuild/tasks/__tests__/convertSvgToJsx.test.tsx +++ b/packages/dnb-eufemia/scripts/prebuild/tasks/__tests__/convertSvgToJsx.test.tsx @@ -22,7 +22,7 @@ jest.mock('ora', () => { beforeAll(async () => { await convertSvgToJsx({ preventDelete: true, - srcPath: [path.resolve(__dirname, './test-files/**/*.svg')], + srcPath: path.resolve(__dirname, './test-files/**/*.svg'), destPath: path.resolve(__dirname, './test-files/dist'), customIconsLockFile: path.resolve( __dirname, diff --git a/packages/dnb-eufemia/scripts/prebuild/tasks/convertSvgToJsx.js b/packages/dnb-eufemia/scripts/prebuild/tasks/convertSvgToJsx.js index b0256dcece7..9c0568156ee 100644 --- a/packages/dnb-eufemia/scripts/prebuild/tasks/convertSvgToJsx.js +++ b/packages/dnb-eufemia/scripts/prebuild/tasks/convertSvgToJsx.js @@ -22,16 +22,15 @@ import { } from '../../figma/tasks/assetsExtractors' import packpath from 'packpath' +const FALLBACK = 'dnb' // defines if an index file should be created const ROOT_DIR = packpath.self() export default async function convertSvgToJsx({ - srcPath = ['./assets/icons/**/*.svg'], + srcPath = './assets/icons/**/*.svg', destPath = './src/icons', preventDelete = false, customIconsLockFile = null, } = {}) { - log.start('> PrePublish: converting svg to jsx') - if (!preventDelete) { await del( [ @@ -45,18 +44,39 @@ export default async function convertSvgToJsx({ force: true, } ) - } - const dnbIcons = await transformSvg({ - srcPath, - destPath, - assetsDir: 'dnb', - customIconsLockFile, - }) + log.info( + '> PrePublish: deleted all svg files before converting "svg to jsx"!' + ) + } - log.succeed( - `> PrePublish: Converting "svg to jsx" is done (${dnbIcons.length} icons)` + const dirs = ( + await fs.readdir( + path.dirname(path.resolve(ROOT_DIR, srcPath, '../')), + { + withFileTypes: true, + } + ) ) + .filter((dir) => dir.isDirectory()) + .map((dir) => dir.name) + + await asyncForEach(dirs, async (assetsDir) => { + log.start( + `> PrePublish: converting "svg to jsx" for "${assetsDir}" as started ...` + ) + + const icons = await transformSvg({ + srcPath, + destPath, + assetsDir, + customIconsLockFile, + }) + + log.succeed( + `> PrePublish: Converting "svg to jsx" for "${assetsDir}" is done (${icons.length} icons)` + ) + }) } const transformSvg = async ({ @@ -110,9 +130,11 @@ const transformToJsx = (content, file) => { fs.unlinkSync(file.path) return Promise.resolve('') } + const basename = path.basename(file.path) const filename = basename.replace(path.extname(file.path), '') const componentName = iconCase(filename) + try { content = content.replace( /clip[0-9]+/g, @@ -180,6 +202,7 @@ const makeIconsEntryFiles = async ({ const basename = path.basename(file) const filename = basename.replace(path.extname(file), '') const name = iconCase(filename) + return { name, filename, @@ -188,7 +211,7 @@ const makeIconsEntryFiles = async ({ }) .sort(({ name: a }, { name: b }) => (a > b ? 1 : -1)) - if (assetsDir === 'dnb') { + if (assetsDir === FALLBACK) { await generateFallbackIndexFiles({ icons, destPath, assetsDir }) } @@ -198,9 +221,10 @@ const makeIconsEntryFiles = async ({ file: customIconsLockFile || iconsLockFile, }) - if (assetsDir === 'dnb') { + if (assetsDir === FALLBACK) { await generateIndexFile({ icons, destPath, assetsDir: '' }) // generate fallback index file } + await generateIndexFile({ icons, destPath, assetsDir }) await generateGroupFiles({ icons, destPath, assetsDir, lockFileContent }) @@ -215,7 +239,7 @@ const generateIndexFile = async ({ icons, destPath, assetsDir }) => { .map( ({ name, filename }) => `import ${name} from '.${ - assetsDir === '' ? '/dnb' : '' + assetsDir === '' ? `/${FALLBACK}` : '' }/${filename}'` ) .join('\n')