Skip to content

Commit

Permalink
patch storybook addon svelte csf package
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Dec 24, 2024
1 parent 00b5aad commit ca9ecc6
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion packages/ui/scripts/patch-sb-csf.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import fs from 'node:fs/promises'

const patchLocation = './node_modules/@storybook/addon-svelte-csf/package.json'
const addonPath = './node_modules/@storybook/addon-svelte-csf'
const patchLocation = addonPath + '/package.json'

const pkgJson = await fs.readFile(patchLocation, 'utf8')
const pkg = JSON.parse(pkgJson)
Expand All @@ -13,3 +14,46 @@ if (!pkg.main || !pkg.types) {
pkg.types = './dist/index.d.ts'
await fs.writeFile(patchLocation, JSON.stringify(pkg, null, 2))
}

// ! Temporary Solution
// Patch broken import
// See: https://github.com/storybookjs/addon-svelte-csf/issues/252

const importToFix = ['utils/identifier-utils']

// Recursive all files in addonPath that match *.js
/**
* @param {string} file
*/
async function fixImport(file) {
const content = await fs.readFile(file, 'utf8')

for (const im of importToFix) {
if (content.includes(im + "'")) {
console.log(
`Fixing import ${im} in ${file.length > 50 ? '...' + file.slice(-50) : file}`,
)
await fs.writeFile(file, content.replace(im + "'", im + ".js'"))
}
}
}

/**
* @param {string} dir
*/
async function walkDir(dir) {
const files = await fs.readdir(dir)

for (const file of files) {
// If is directory recursive
if ((await fs.stat(dir + '/' + file)).isDirectory()) {
await walkDir(dir + '/' + file)
} else {
if (file.endsWith('.js')) {
await fixImport(dir + '/' + file)
}
}
}
}

await walkDir(addonPath)

0 comments on commit ca9ecc6

Please sign in to comment.