Skip to content

Commit 9769645

Browse files
committed
fix: fix intl config json initialize (#1)
1 parent 6ed0dba commit 9769645

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

client/src/lib/file.ts

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import { ProgressLocation, Uri, window as Window, workspace } from "vscode";
55

66
import { JSON_SPACE } from "./constant";
77

8+
/**
9+
* 获取工作区下国际化配置文件 json 路径
10+
*
11+
* @param {Uri} workspaceIntlConfigPath 工作区根目录
12+
* @param {string} jsonPath 国际化的 json 路径
13+
* @return {*} {Uri}
14+
*/
15+
export const getWorkspaceIntlJsonPath = (workspaceIntlConfigPath: Uri, jsonPath: string): Uri =>
16+
{
17+
return Uri.joinPath(workspaceIntlConfigPath, path.join('/' + jsonPath))
18+
}
19+
820
/**
921
* 初始化工作区国际化配置文件
1022
* 判断依据仅仅是,工作区 src 路径下是否有 intl 配置文件夹
@@ -17,17 +29,39 @@ export const initializeWorkplaceIntlConfig = async (intlTempPath: string, worksp
1729
{
1830
if (!workspaceIntlConfigPath) return
1931

32+
const zhPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'zh_CN.json')
33+
const enPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'en_US.json')
34+
35+
// 初始化整个文件夹
2036
try
2137
{
2238
await workspace.fs.stat(workspaceIntlConfigPath)
23-
console.log('does workspace intl config exist')
2439
}
2540
catch (e)
2641
{
27-
console.log('nope, workspace intl config does not exist')
2842

2943
// 若拷贝失败,抛出错误
30-
await workspace.fs.copy(Uri.file(intlTempPath), workspaceIntlConfigPath)
44+
return await workspace.fs.copy(Uri.file(intlTempPath), workspaceIntlConfigPath)
45+
}
46+
47+
// 初始化中文国际化配置
48+
try
49+
{
50+
await workspace.fs.stat(zhPath)
51+
}
52+
catch (e)
53+
{
54+
await workspace.fs.copy(Uri.file(path.join(intlTempPath, '/zh_CN.json')), zhPath)
55+
}
56+
57+
//初始化英文国际化配置
58+
try
59+
{
60+
await workspace.fs.stat(enPath)
61+
}
62+
catch (e)
63+
{
64+
await workspace.fs.copy(Uri.file(path.join(intlTempPath, '/en_US.json')), enPath)
3165
}
3266
}
3367

@@ -44,8 +78,8 @@ export const getIntlConfig = async (
4478
if (!workspaceIntlConfigPath) return [undefined, undefined]
4579

4680
// 工作区国际化配置文件路径
47-
const zhPath = Uri.joinPath(workspaceIntlConfigPath, path.join('/zh_CN.json'))
48-
const enPath = Uri.joinPath(workspaceIntlConfigPath, path.join('/en_US.json'))
81+
const zhPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'zh_CN.json')
82+
const enPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'en_US.json')
4983

5084
// 获取工作区国际化配置文件内容
5185
const [rawZHConfig, rawENConfig] = await Promise.all([
@@ -128,8 +162,8 @@ export const writeConfigIntoWorkSpace = async (zhConfig: Record<string, string>,
128162
if (!workspaceIntlConfigPath) return
129163

130164
// 工作区国际化配置文件路径
131-
const zhPath = Uri.joinPath(workspaceIntlConfigPath, path.join('/zh_CN.json'))
132-
const enPath = Uri.joinPath(workspaceIntlConfigPath, path.join('/en_US.json'))
165+
const zhPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'zh_CN.json')
166+
const enPath = getWorkspaceIntlJsonPath(workspaceIntlConfigPath, 'en_US.json')
133167

134168
return await Window.withProgress({
135169
cancellable: false,

0 commit comments

Comments
 (0)