Skip to content

Commit

Permalink
wip: 汉化完成
Browse files Browse the repository at this point in the history
  • Loading branch information
jaw52 committed Mar 7, 2023
1 parent c2ff4b8 commit bb1d833
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
18 changes: 13 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { green } from 'kolorist'
import locales from './utils/locales'
import { transformStart } from './transformStart'

/* 用户参数 */
export interface Setting {
scanPath: string
isGitMv: 1 | 0
lang: 'zh' | 'en'
}

const runTransform = async () => {
const { scanPath, isGitMv, lang = 'zh' } = await prompts([
{
Expand Down Expand Up @@ -36,25 +43,26 @@ const runTransform = async () => {
},
], {
onCancel: () => process.exit(1),
}) as { scanPath: string; isGitMv: 1 | 0; lang: 'zh' | 'en' }
}) as Setting

const needTransformList = await transformStart(scanPath.trim(), isGitMv)
const needTransformList = await transformStart({ scanPath: scanPath.trim(), isGitMv, lang })
const t = locales[lang]

if (needTransformList.length > 0) {
consola.success(`${locales[lang].finish} ${green('to jsx')}`)
consola.success(`${t.finish} ${green('to jsx')}`)

const { show = false } = await prompts({
name: 'show',
type: 'confirm',
message: `${locales[lang].show}?`,
message: `${t.show}?`,
initial: false,
}) as { show?: boolean }

if (show)
needTransformList.forEach(item => consola.log(item))
}
else {
consola.info(locales[lang].noFiles)
consola.info(t.noFiles)
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/transformStart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import pLimit from 'p-limit'
import { loadArgs } from './utils/loadArgs'
import { gitMv } from './utils/gitMv'
import { formatMs } from './utils/formatTime'
import locales from './utils/locales'
import type { Setting } from '.'

const traverse = (_traverse as any).default as typeof _traverse

Expand Down Expand Up @@ -38,11 +40,10 @@ const runRename = async (oldPath: string, isGitMv: 1 | 0) => {
}

/**
* @param scanPath 扫描目录
* @param isGitMv 是否是Git mv方式迁移
* @returns 迁移的文件List
*/
export const transformStart = async (scanPath: string, isGitMv: 1 | 0): Promise<string[]> => {
export const transformStart = async ({ scanPath, isGitMv, lang }: Setting): Promise<string[]> => {
const t = locales[lang]
const { ignore, concurrency } = loadArgs()

const tsFiles = glob.sync(`${slash(scanPath)}/**/*.{ts,js}`, {
Expand Down Expand Up @@ -74,23 +75,23 @@ export const transformStart = async (scanPath: string, isGitMv: 1 | 0): Promise<
})
}
catch (err) {
consola.error('Babel failed to parse the file', err)
consola.error(t.babelFail, err)
}
}

const spinner = ora()
const startTime = Date.now()

try {
spinner.start('Start scanning\n')
spinner.start(`${t.start}\n`)
const limit = pLimit(concurrency)

await Promise.all(
needTransformList.map(path => limit(async () => runRename(path, isGitMv))),
)

const runTime = formatMs(Date.now() - startTime)
spinner.succeed(`Finish scanning - ${runTime}`)
spinner.succeed(`${t.finish} - ${runTime}`)
}
catch {
spinner.fail('Fail')
Expand Down
6 changes: 6 additions & 0 deletions src/utils/locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ export default {
show: '是否展示修改结果',
noFiles: '未发现有需要迁移的文件',
finish: '完成',
start: '开始扫描',
end: '完成扫描',
babelFail: 'Babel解析文件失败',
isGitMv: '是否使用Git mv方式进行批量修改后缀名(Git托管的项目推荐这种方式)',
},
en: {
scanPath: 'Please specify the folder to be scanned',
show: 'Display modification results',
noFiles: 'No files found to be migrated',
finish: 'Finish',
start: 'Start scanning',
end: 'Finish scanning',
babelFail: 'Babel failed to parse the file',
isGitMv: 'Whether to use Git mv method to batch modify suffix names (Git managed projects recommend this method)',
},
}

0 comments on commit bb1d833

Please sign in to comment.