Skip to content

Commit

Permalink
storybook: fix all invalid import
Browse files Browse the repository at this point in the history
  • Loading branch information
leomotors committed Dec 24, 2024
1 parent b5acaeb commit 25eddfa
Showing 1 changed file with 32 additions and 5 deletions.
37 changes: 32 additions & 5 deletions packages/ui/scripts/patch-sb-csf.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,53 @@ if (!pkg.main || !pkg.types) {
const replacePatterns = {
"utils/identifier-utils'": "utils/identifier-utils.js'",
"from '@storybook/node-logger'": "from 'storybook/internal/node-logger'",
"/replace-argument'": "/replace-argument.js'",
"/define-meta'": "/define-meta.js'",
"/parser/ast'": "/parser/ast.js'",
}

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

for (const pattern in replacePatterns) {
if (content.includes(pattern)) {
replaced = true

const lengthLimit = 70 - pattern.length

console.log(
`Fixing pattern "${pattern}" in ${file.length > lengthLimit ? '...' + file.slice(-lengthLimit) : file}`,
)
await fs.writeFile(
file,
content.replace(pattern, replacePatterns[pattern]),
)

content = content.replaceAll(pattern, replacePatterns[pattern])
}
}

if (replaced) {
await fs.writeFile(file, content)
}

for (const line of content.split('\n')) {
if (line.startsWith('import')) {
const match = line.match(/from '(.*)'/)

if (!match) {
continue
}

const importPath = match[1]
if (
importPath.startsWith('.') &&
!(importPath.endsWith('.js') || importPath.endsWith('.svelte'))
) {
console.log(`Warn: ${file}`)
console.log(`Invalid Import: ${importPath}`)
}
}
}
}
Expand Down

0 comments on commit 25eddfa

Please sign in to comment.