Skip to content

Commit ed004d0

Browse files
committed
完善
1 parent ac34b88 commit ed004d0

File tree

2 files changed

+49
-31
lines changed

2 files changed

+49
-31
lines changed

scripts/i18n-use-update.ts

+46-29
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,22 @@ import matcher, {
77
} from '../src/matcher'
88

99
// default: en
10-
const local = ['de', 'es', 'fr', 'it', 'jp', 'kr', 'pt']
10+
const locals = ['de', 'es', 'fr', 'it', 'jp', 'kr', 'pt']
1111

12-
export function customInsertToLastImport(
13-
insertValue: string
12+
function customInsertToLastImport(
13+
rawImportStr: string
1414
): (context: Context) => Promise<Context> {
1515
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"
1819

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
2621
.split('import ')
2722
.splice(1)
2823
.map((item) => 'import ' + item)
2924

30-
const endChunk = splitByImport[splitByImport.length - 1]
25+
const endChunk = splitContentByImport[splitContentByImport.length - 1]
3126

3227
const regex = /['"]([^'"]+)['"]/
3328
const found = endChunk.match(regex)
@@ -36,54 +31,76 @@ export function customInsertToLastImport(
3631
const splitByModuleName = endChunk.split(moduleName)
3732
splitByModuleName[0] += moduleName + '\n'
3833

39-
splitByImport[splitByImport.length - 1] = [
34+
splitContentByImport[splitContentByImport.length - 1] = [
4035
splitByModuleName[0],
41-
insertValue,
36+
importStr,
4237
splitByModuleName[1]
4338
].join('')
4439
}
4540

46-
context.content = splitByImport.join('')
41+
context.content = splitContentByImport.join('')
4742

4843
return context
4944
}
5045
}
5146

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 {
5466
headerDictionaries,
5567
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
5871
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'\`相比于导入路径会更严谨
6176
`
6277

6378
matcher({
64-
entry: 'e:/HXL/工具/batch-handle-resource/test/data',
79+
entry: 'e:/Project/spyx-next-web/src',
6580
rules: [
6681
{
6782
match: /\.js$/,
6883
use: [
69-
filterByContent('import Header from "src/components/Header/Header"'),
84+
filterByContent('/Header/Header'),
7085
customInsertToLastImport(importStr),
7186
replaceContent(
7287
[
88+
...getReplaceImportHeader(),
7389
{
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} />'
7693
},
7794
{
78-
searchValue: '<Header />',
95+
searchValue: '<Header/>',
7996
replaceValue:
8097
'<Header dictionaries={headerDictionaries} styles={headerStyles} />'
8198
}
8299
],
83-
false
100+
true
84101
),
85102
log({
86-
name: 'page-更新组件路径和Props',
103+
name: 'header组件-更新组件路径和Props',
87104
title: logTitle,
88105
header: ['次数', '路径'],
89106
storePath: 'e:/HXL/工具/batch-handle-resource/log'

src/matcher/handle.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import { Context } from './'
44
import { LogFileGenerate } from '../shared'
55

66
export function filterByContent(
7-
target: string
7+
includes: string | string[]
88
): (context: Context) => Promise<Context> {
99
return async (context: Context) => {
10-
context.isFilter = !context.content.includes(target)
10+
const targets = Array.isArray(includes) ? includes : [includes]
11+
context.isFilter = !targets.find((item) => context.content.includes(item))
1112

1213
return context
1314
}

0 commit comments

Comments
 (0)