@@ -7,27 +7,22 @@ import matcher, {
7
7
} from '../src/matcher'
8
8
9
9
// default: en
10
- const local = [ 'de' , 'es' , 'fr' , 'it' , 'jp' , 'kr' , 'pt' ]
10
+ const locals = [ 'de' , 'es' , 'fr' , 'it' , 'jp' , 'kr' , 'pt' ]
11
11
12
- export function customInsertToLastImport (
13
- insertValue : string
12
+ function customInsertToLastImport (
13
+ rawImportStr : string
14
14
) : ( context : Context ) => Promise < Context > {
15
15
return async ( context : Context ) => {
16
- const endPath = local . find ( ( v ) => context . path . includes ( v ) ) ?? 'en'
17
- insertValue += endPath
16
+ const pathSplit = context . path . split ( '/' )
17
+ const local = locals . find ( ( l ) => pathSplit . find ( ( p ) => l === p ) ) ?? 'en'
18
+ const importStr = rawImportStr + local + "'\n"
18
19
19
- if ( ! context . content . includes ( 'import ' ) ) {
20
- context . content = insertValue + context . content
21
-
22
- return context
23
- }
24
-
25
- const splitByImport = context . content
20
+ const splitContentByImport = context . content
26
21
. split ( 'import ' )
27
22
. splice ( 1 )
28
23
. map ( ( item ) => 'import ' + item )
29
24
30
- const endChunk = splitByImport [ splitByImport . length - 1 ]
25
+ const endChunk = splitContentByImport [ splitContentByImport . length - 1 ]
31
26
32
27
const regex = / [ ' " ] ( [ ^ ' " ] + ) [ ' " ] /
33
28
const found = endChunk . match ( regex )
@@ -36,54 +31,76 @@ export function customInsertToLastImport(
36
31
const splitByModuleName = endChunk . split ( moduleName )
37
32
splitByModuleName [ 0 ] += moduleName + '\n'
38
33
39
- splitByImport [ splitByImport . length - 1 ] = [
34
+ splitContentByImport [ splitContentByImport . length - 1 ] = [
40
35
splitByModuleName [ 0 ] ,
41
- insertValue ,
36
+ importStr ,
42
37
splitByModuleName [ 1 ]
43
38
] . join ( '' )
44
39
}
45
40
46
- context . content = splitByImport . join ( '' )
41
+ context . content = splitContentByImport . join ( '' )
47
42
48
43
return context
49
44
}
50
45
}
51
46
52
- const importStr = `
53
- import {
47
+ function getReplaceImportHeader ( ) {
48
+ const otherLocalImport = locals . map (
49
+ ( local ) => `src/components/${ local } /Header/Header`
50
+ )
51
+
52
+ const replaceImportHeader = [
53
+ 'src/components/Header/Header' ,
54
+ '../../Header/Header' ,
55
+ '../../components/Header/Header' ,
56
+ ...otherLocalImport
57
+ ] . map ( ( searchValue ) => ( {
58
+ searchValue,
59
+ replaceValue : 'src/components/i18n/header'
60
+ } ) )
61
+
62
+ return replaceImportHeader
63
+ }
64
+
65
+ const importStr = `import {
54
66
headerDictionaries,
55
67
headerStyles
56
- } from 'src/components/i18n/header/config/i18n/'\n
57
- `
68
+ } from 'src/components/i18n/header/config/i18n/`
69
+
70
+ const logTitle = `# Header 组件 - 更新组件路径和 Props
58
71
59
- const logTitle = `# page - 更新组件路径和 Props
60
- **import Header from "src/components/Header/Header" => import Header from "src/components/i18n/header"**
72
+ **Header/Header => src/components/i18n/header**
73
+
74
+ 由于项目内引用路径的引号没统一,可以不采用导入语句查找,而是通过导入路径查找
75
+ 导入语句\`import Header from 'src/components/i18n/header'\`相比于导入路径会更严谨
61
76
`
62
77
63
78
matcher ( {
64
- entry : 'e:/HXL/工具/batch-handle-resource/test/data ' ,
79
+ entry : 'e:/Project/spyx-next-web/src ' ,
65
80
rules : [
66
81
{
67
82
match : / \. j s $ / ,
68
83
use : [
69
- filterByContent ( 'import Header from "src/components/ Header/Header" ' ) ,
84
+ filterByContent ( '/ Header/Header' ) ,
70
85
customInsertToLastImport ( importStr ) ,
71
86
replaceContent (
72
87
[
88
+ ...getReplaceImportHeader ( ) ,
73
89
{
74
- searchValue : 'import Header from "src/components/Header/Header"' ,
75
- replaceValue : 'import Header from "src/components/i18n/header"'
90
+ searchValue : '<Header />' ,
91
+ replaceValue :
92
+ '<Header dictionaries={headerDictionaries} styles={headerStyles} />'
76
93
} ,
77
94
{
78
- searchValue : '<Header />' ,
95
+ searchValue : '<Header/>' ,
79
96
replaceValue :
80
97
'<Header dictionaries={headerDictionaries} styles={headerStyles} />'
81
98
}
82
99
] ,
83
- false
100
+ true
84
101
) ,
85
102
log ( {
86
- name : 'page -更新组件路径和Props' ,
103
+ name : 'header组件 -更新组件路径和Props' ,
87
104
title : logTitle ,
88
105
header : [ '次数' , '路径' ] ,
89
106
storePath : 'e:/HXL/工具/batch-handle-resource/log'
0 commit comments