@@ -22,26 +22,53 @@ if (!pkg.main || !pkg.types) {
22
22
const replacePatterns = {
23
23
"utils/identifier-utils'" : "utils/identifier-utils.js'" ,
24
24
"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'" ,
25
28
}
26
29
27
30
// Recursive all files in addonPath that match *.js
28
31
/**
29
32
* @param {string } file
30
33
*/
31
34
async function fixImport ( file ) {
32
- const content = await fs . readFile ( file , 'utf8' )
35
+ let content = await fs . readFile ( file , 'utf8' )
36
+ let replaced = false
33
37
34
38
for ( const pattern in replacePatterns ) {
35
39
if ( content . includes ( pattern ) ) {
40
+ replaced = true
41
+
36
42
const lengthLimit = 70 - pattern . length
37
43
38
44
console . log (
39
45
`Fixing pattern "${ pattern } " in ${ file . length > lengthLimit ? '...' + file . slice ( - lengthLimit ) : file } ` ,
40
46
)
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 ( / f r o m ' ( .* ) ' / )
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
+ }
45
72
}
46
73
}
47
74
}
0 commit comments