Skip to content

Commit

Permalink
Automatically find subfolders
Browse files Browse the repository at this point in the history
  • Loading branch information
tujoworker committed Apr 13, 2023
1 parent 4b5b0e7 commit f7226e3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
54 changes: 39 additions & 15 deletions packages/dnb-eufemia/scripts/prebuild/tasks/convertSvgToJsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand All @@ -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 ({
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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 })
}

Expand All @@ -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 })

Expand All @@ -215,7 +239,7 @@ const generateIndexFile = async ({ icons, destPath, assetsDir }) => {
.map(
({ name, filename }) =>
`import ${name} from '.${
assetsDir === '' ? '/dnb' : ''
assetsDir === '' ? `/${FALLBACK}` : ''
}/${filename}'`
)
.join('\n')
Expand Down

0 comments on commit f7226e3

Please sign in to comment.