From 25eddfaf8558f6a27145bf9d06e430b9d74d1b27 Mon Sep 17 00:00:00 2001 From: Nutthapat Pongtanyavichai Date: Tue, 24 Dec 2024 15:51:20 +0700 Subject: [PATCH] storybook: fix all invalid import --- packages/ui/scripts/patch-sb-csf.js | 37 +++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/packages/ui/scripts/patch-sb-csf.js b/packages/ui/scripts/patch-sb-csf.js index 724de8839..d6e3d3346 100644 --- a/packages/ui/scripts/patch-sb-csf.js +++ b/packages/ui/scripts/patch-sb-csf.js @@ -22,6 +22,9 @@ 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 @@ -29,19 +32,43 @@ const replacePatterns = { * @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}`) + } } } }