-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathutils.ts
More file actions
364 lines (333 loc) · 9.44 KB
/
Copy pathutils.ts
File metadata and controls
364 lines (333 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/**
* @author linhuiw
* @desc 工具方法
*/
import * as _ from 'lodash';
import * as vscode from 'vscode';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as slash from 'slash2';
import * as globby from 'globby';
import { pinyin } from 'pinyin-pro';
import { TranslateAPiEnum } from './define';
import { getObjectLiteralExpression } from './astUtils';
/**
* 将对象拍平
* @param obj 原始对象
* @param prefix
*/
export function flatten(obj, prefix?) {
var propName = prefix ? prefix + '.' : '',
ret = {};
for (var attr in obj) {
if (_.isArray(obj[attr])) {
var len = obj[attr].length;
ret[attr] = obj[attr].join(',');
} else if (typeof obj[attr] === 'object') {
_.extend(ret, flatten(obj[attr], propName + attr));
} else {
ret[propName + attr] = obj[attr];
}
}
return ret;
}
/**
* 查找当前位置的 Code
*/
export function findPositionInCode(text: string, code: string) {
const lines = code.split('\n');
const lineNum = lines.findIndex(line => line.includes(text));
if (lineNum === -1) {
return null;
}
let chNum = lines[lineNum].indexOf(text);
if (text.startsWith(' ')) {
chNum += 1;
}
return new vscode.Position(lineNum, chNum);
}
export function findMatchKey(langObj, text) {
for (const key in langObj) {
if (langObj[key] === text) {
return key;
}
}
return null;
}
/**
* 获取文件夹下所有文件
* @function getAllFiles
* @param {string} dir Dir path string.
* @return {string[]} Array with all file names that are inside the directory.
*/
export const getAllFiles = dir =>
fs.readdirSync(dir).reduce((files, file) => {
// 避免读取node_modules造成性能问题
if (file === 'node_modules') {
return [...files];
}
const name = path.join(dir, file);
const isDirectory = fs.statSync(name).isDirectory();
return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
}, []);
/**
* 获取文件 Json
*/
export function getLangJson(fileName) {
const fileContent = fs.readFileSync(fileName, { encoding: 'utf8' });
let objStr = getObjectLiteralExpression(fileContent);
let jsObj = {};
try {
jsObj = eval('(' + objStr + ')');
} catch (err) {
console.log(objStr);
console.error(err);
}
return jsObj;
}
/**
* 获取配置,支持从vscode和配置文件(优先)中取到配置项
*/
export const getConfiguration = text => {
let value = vscode.workspace.getConfiguration('vscode-i18n-linter').get(text);
let kiwiConfigJson = getConfigFile();
if (!kiwiConfigJson) {
return value;
}
const config = getLangJson(kiwiConfigJson);
if (text in config) {
value = config[text];
}
return value;
};
/**
* 查找kiwi-cli配置文件
*/
export const getConfigFile = () => {
let kiwiConfigJson = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/.kiwirc.js`;
// 先找js
if (!fs.existsSync(kiwiConfigJson)) {
kiwiConfigJson = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/.kiwirc.ts`;
//再找ts
if (!fs.existsSync(kiwiConfigJson)) {
return null;
}
}
return kiwiConfigJson;
};
/**
* 查找kiwi-linter配置文件
*/
export const getKiwiLinterConfigFile = () => {
let kiwiConfigJson = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/kiwi-config.json`;
// 先找js
if (!fs.existsSync(kiwiConfigJson)) {
return null;
}
return kiwiConfigJson;
};
/**
* 获得项目配置信息中的 googleApiKey
*/
function getConfigByKey(key: string): any {
const configFile = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/kiwi-config.json`;
let content = '';
try {
if (fs.existsSync(configFile)) {
content = JSON.parse(fs.readFileSync(configFile, 'utf8'))[key];
}
} catch (error) {
console.log(error);
}
return content;
}
/**
* 重试方法
* @param asyncOperation
* @param times
*/
function retry(asyncOperation, times = 1) {
let runTimes = 1;
const handleReject = e => {
if (runTimes++ < times) {
return asyncOperation().catch(handleReject);
} else {
throw e;
}
};
return asyncOperation().catch(handleReject);
}
/**
* 设置超时
* @param promise
* @param ms
*/
function withTimeout(promise, ms) {
const timeoutPromise = new Promise((resolve, reject) => {
setTimeout(() => {
reject(`Promise timed out after ${ms} ms.`);
}, ms);
});
return Promise.race([promise, timeoutPromise]);
}
/**
* 翻译中文
*/
export function translateText(text: string, type: TranslateAPiEnum) {
const googleApiKey = getConfigByKey('googleApiKey');
const { translate: googleTranslate } = require('google-translate')(googleApiKey);
const { appId, appKey } = getConfigByKey('baiduApiKey');
const baiduTranslate = require('baidu-translate');
function _translateText() {
return withTimeout(
new Promise((resolve, reject) => {
// google
if (type === TranslateAPiEnum.Google) {
googleTranslate(text, 'zh', 'en', (err, translation) => {
if (err) {
reject(err);
} else {
const result = translation.translatedText ? translation.translatedText.split('$') : [];
resolve(result);
}
});
}
// baidu
if (type === TranslateAPiEnum.Baidu) {
baiduTranslate(
appId,
appKey,
'en',
'zh'
)(text)
.then(data => {
if (data && data.trans_result) {
const result = data.trans_result.map(item => item.dst) || [];
resolve(result);
}
})
.catch(err => {
reject(err);
});
}
// pinyin
if (type === TranslateAPiEnum.PinYin) {
const result = pinyin(text, { toneType: 'none' });
resolve(result.split('$'));
}
}),
3000
);
}
return retry(_translateText, 3);
}
/**
* 获取多项目配置
*/
export function getTargetLangPath(currentFilePath) {
const configFile = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/kiwi-config.json`;
let targetLangPath = '';
try {
if (fs.existsSync(configFile)) {
const { projects = [] } = JSON.parse(fs.readFileSync(configFile, 'utf8'));
for (const config of projects) {
if (currentFilePath.indexOf(`/${config.target}/`) > -1) {
targetLangPath = `${vscode.workspace.workspaceFolders[0].uri.fsPath}/${config.kiwiDir}/zh_CN/`;
return targetLangPath;
}
}
}
} catch (error) {
console.log(error);
}
return targetLangPath;
}
/**
* 获取当前文件对应的项目路径
*/
export function getCurrentProjectLangPath() {
let currentProjectLangPath = '';
const targetLangPath = getTargetLangPath(vscode.window.activeTextEditor.document.uri.path);
if (targetLangPath) {
currentProjectLangPath = `${targetLangPath}**/*.ts`;
}
return currentProjectLangPath;
}
/**
* 获取当前文件对应的语言路径
*/
export function getLangPrefix() {
const langPrefix = getTargetLangPath(vscode.window.activeTextEditor.document.uri.path);
return langPrefix;
}
/**
* 获取当前文件路径层级 [pageA, moudleA, componentA]
*/
export function getCurrActivePageI18nKey() {
const activeEditor = vscode.window.activeTextEditor;
let suggestion = [];
if (activeEditor) {
const currentFilename = activeEditor.document.fileName;
const suggestPageRegex = /\/pages\/\w+\/([^\/]+)\/([^\/\.]+)/;
if (currentFilename.includes('/pages/')) {
suggestion = currentFilename.match(suggestPageRegex);
}
if (suggestion && suggestion.length) {
// 匹配到则去除第一项currentFilename,保留元组项
suggestion.shift();
} else {
const names = slash(currentFilename).split('/') as string[];
const fileName = _.last(names);
const fileKey = fileName.split('.')[0].replace(new RegExp('-', 'g'), '_');
const dir = names[names.length - 2].replace(new RegExp('-', 'g'), '_');
if (dir === fileKey) {
suggestion = [dir];
} else {
suggestion = [dir, fileKey];
}
}
}
return suggestion;
}
/**
* 检测翻译源
*/
export function getTranslateAPiList() {
let apiList = [{ label: TranslateAPiEnum.PinYin, description: '拼音' }];
const googleApiKey = getConfigByKey('googleApiKey');
const { appId, appKey } = getConfigByKey('baiduApiKey');
// google翻译暂时不开放
// if (googleApiKey) {
// apiList.push({ label: TranslateAPiEnum.Google, description: '谷歌' });
// }
if (appId && appKey) {
apiList.push({ label: TranslateAPiEnum.Baidu, description: '百度' });
}
return apiList;
}
/** 获取语言文件名称 */
export function getI18NFileNames(DEFAULT_I18N_GLOB?:string) {
const _I18N_GLOB = getCurrentProjectLangPath() || DEFAULT_I18N_GLOB;
const paths = globby.sync(_I18N_GLOB);
return (paths || [])
.map(i => {
return i
.split('/')
.pop()
.replace(/\.tsx?$/, '');
})
.filter(i => i !== 'index');
}
/** 纠正path中文件名称的方法 */
export function getSafePath(path: string, DEFAULT_I18N_GLOB?:string) {
if (!path) {
return path;
}
const fileNames = getI18NFileNames(DEFAULT_I18N_GLOB);
const [filename, ...restPath] = path.split('.');
const sameFileNameIndex = fileNames.map(i => i.toLowerCase()).findIndex(i => i === filename.toLowerCase());
if (sameFileNameIndex > -1) {
return [fileNames[sameFileNameIndex], ...restPath].join('.');
}
return path;
}