Skip to content

Commit 25eddfa

Browse files
committed
storybook: fix all invalid import
1 parent b5acaeb commit 25eddfa

File tree

1 file changed

+32
-5
lines changed

1 file changed

+32
-5
lines changed

packages/ui/scripts/patch-sb-csf.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,53 @@ if (!pkg.main || !pkg.types) {
2222
const replacePatterns = {
2323
"utils/identifier-utils'": "utils/identifier-utils.js'",
2424
"from '@storybook/node-logger'": "from 'storybook/internal/node-logger'",
25+
"/replace-argument'": "/replace-argument.js'",
26+
"/define-meta'": "/define-meta.js'",
27+
"/parser/ast'": "/parser/ast.js'",
2528
}
2629

2730
// Recursive all files in addonPath that match *.js
2831
/**
2932
* @param {string} file
3033
*/
3134
async function fixImport(file) {
32-
const content = await fs.readFile(file, 'utf8')
35+
let content = await fs.readFile(file, 'utf8')
36+
let replaced = false
3337

3438
for (const pattern in replacePatterns) {
3539
if (content.includes(pattern)) {
40+
replaced = true
41+
3642
const lengthLimit = 70 - pattern.length
3743

3844
console.log(
3945
`Fixing pattern "${pattern}" in ${file.length > lengthLimit ? '...' + file.slice(-lengthLimit) : file}`,
4046
)
41-
await fs.writeFile(
42-
file,
43-
content.replace(pattern, replacePatterns[pattern]),
44-
)
47+
48+
content = content.replaceAll(pattern, replacePatterns[pattern])
49+
}
50+
}
51+
52+
if (replaced) {
53+
await fs.writeFile(file, content)
54+
}
55+
56+
for (const line of content.split('\n')) {
57+
if (line.startsWith('import')) {
58+
const match = line.match(/from '(.*)'/)
59+
60+
if (!match) {
61+
continue
62+
}
63+
64+
const importPath = match[1]
65+
if (
66+
importPath.startsWith('.') &&
67+
!(importPath.endsWith('.js') || importPath.endsWith('.svelte'))
68+
) {
69+
console.log(`Warn: ${file}`)
70+
console.log(`Invalid Import: ${importPath}`)
71+
}
4572
}
4673
}
4774
}

0 commit comments

Comments
 (0)