fis3-parser-react-i18n
Advanced tools
Comparing version 0.0.0-beta.2 to 0.0.1-beta.1
111
index.js
@@ -7,2 +7,5 @@ var parse = require('@babel/parser').parse; | ||
var crypto = require('crypto'); | ||
var fs = require('fs'); | ||
var p = require('path'); | ||
var ignore = require('ignore'); | ||
function hash(str) { | ||
@@ -13,6 +16,49 @@ var md5 = crypto.createHash('md5'); | ||
var zhExt = /[\u4e00-\u9fa5]+/; | ||
var baseLocale = null; | ||
function noLocale(value, id, relativePath) { | ||
if (!baseLocale) { | ||
return false; | ||
} | ||
if (!baseLocale[id]) { | ||
console.log('\x1B[31m%s\x1B[0m', "\n\u3010i18n\u3011\u5728\u8BED\u6599\u5305\u4E2D\u672A\u53D1\u73B0\u4EE5\u4E0B\u5B57\u6BB5 ".concat(relativePath, "\uFF1A").concat(value, " \u8BF7\u6267\u884Ci18n update\n")); | ||
return true; | ||
} | ||
return false; | ||
} | ||
function readChinese(languages) { | ||
var chinesePath = languages.find(function (language) { return language.name === 'zh-CN'; }).path; | ||
var code = fs.readFileSync(p.resolve(process.cwd(), chinesePath, 'zh-CN.ts'), 'utf8'); | ||
var ast = parse(code, { | ||
sourceType: 'module', | ||
plugins: ['typescript'] | ||
}); | ||
traverse(ast, { | ||
ObjectProperty: function (path) { | ||
var key = path.node.key.value || path.node.key.name; | ||
var value = path.node.value.value || path.node.value.name; | ||
baseLocale[key] = value; | ||
} | ||
}); | ||
} | ||
module.exports = function (content, file, options) { | ||
var _a = options.importInfo, importSource = _a.source, importImported = _a.imported, importLocal = _a.local; | ||
var filePath = file.realpath; | ||
var _a = options.ignore || {}, sameGit = _a.sameGit, list = _a.list; | ||
var languages = options.languages; | ||
var ignoreList = list || []; | ||
if (sameGit) { | ||
var gitIgnore = fs | ||
.readFileSync(p.resolve(process.cwd(), '.gitignore')) | ||
.toString(); | ||
ignoreList.push.apply(ignoreList, gitIgnore.split('\n')); | ||
} | ||
var relativePath = p.relative(process.cwd(), filePath); | ||
var includes = ignore().add(options.includes); | ||
var included = includes.ignores(relativePath); | ||
var ig = ignore().add(ignoreList); | ||
if (ig.ignores(relativePath) && !included) { | ||
return content; | ||
} | ||
var _b = options.importInfo, importSource = _b.source, importImported = _b.imported, importLocal = _b.local; | ||
var plugins = ['typescript', 'decorators-legacy']; | ||
if (/.*(tsx|jsx)$/.test(file)) { | ||
if (/.*(tsx|jsx)$/.test(filePath)) { | ||
plugins.push('jsx'); | ||
@@ -26,6 +72,11 @@ } | ||
if (ast.errors.length > 0) { | ||
throw new Error(JSON.stringify(ast.errors)); | ||
console.warn(ast.errors); | ||
return content; | ||
} | ||
var needI18n = false; | ||
var i18nFnName = importLocal; | ||
if (!baseLocale) { | ||
baseLocale = {}; | ||
readChinese(languages); | ||
} | ||
var visitor = { | ||
@@ -64,2 +115,5 @@ Program: { | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
var origininalValue = path.node.value; | ||
@@ -75,2 +129,3 @@ var trimmedValue = origininalValue.trim(); | ||
]); | ||
needI18n = true; | ||
} | ||
@@ -99,20 +154,29 @@ }, | ||
var variables_1 = []; | ||
var i_1 = 0; | ||
var value = path | ||
.toString() | ||
.replace(/`/g, '') | ||
.replace(/\$\{([\s\S]+?)\}/g, function ($1, $2) { | ||
variables_1.push($2); | ||
return "{{".concat($2, "}}"); | ||
.replace(/\$\{([\s\S]+?)\}/g, function () { | ||
var agr = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
agr[_i] = arguments[_i]; | ||
} | ||
i_1++; | ||
variables_1.push({ | ||
id: i_1, | ||
name: agr[1] | ||
}); | ||
return "{{@".concat(i_1, "}}"); | ||
}); | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
path.replaceWith(t.callExpression(t.identifier(importLocal), [ | ||
t.stringLiteral(id), | ||
t.objectExpression(node.expressions.map(function (item) { | ||
return t.objectProperty(t.stringLiteral(generate(item, { | ||
jsescOption: { | ||
minimal: true | ||
} | ||
}).code), item); | ||
t.objectExpression(node.expressions.map(function (item, index) { | ||
return t.objectProperty(t.stringLiteral("@".concat(index + 1)), item); | ||
})) | ||
])); | ||
needI18n = true; | ||
} | ||
@@ -132,2 +196,5 @@ } | ||
var id = hash(value); | ||
if (noLocale(value, id, relativePath)) { | ||
return; | ||
} | ||
if (t.isJSXAttribute(path.parent)) { | ||
@@ -139,2 +206,3 @@ path.replaceWith(t.jsxExpressionContainer(t.callExpression(t.identifier(importLocal), [t.stringLiteral(id)]))); | ||
} | ||
needI18n = true; | ||
} | ||
@@ -144,9 +212,14 @@ } | ||
traverse(ast, visitor); | ||
var codeRes = generate(ast, { | ||
jsescOption: { | ||
minimal: true | ||
}, | ||
decoratorsBeforeExport: true | ||
}, content); | ||
return codeRes.code; | ||
if (needI18n) { | ||
var codeRes = generate(ast, { | ||
jsescOption: { | ||
minimal: true | ||
}, | ||
decoratorsBeforeExport: true | ||
}, content); | ||
return codeRes.code; | ||
} | ||
else { | ||
return content; | ||
} | ||
}; |
{ | ||
"name": "fis3-parser-react-i18n", | ||
"version": "0.0.0-beta.2", | ||
"version": "0.0.1-beta.1", | ||
"description": "fis3处理react国际化插件", | ||
@@ -19,3 +19,4 @@ "main": "index.js", | ||
"@babel/traverse": "^7.18.10", | ||
"@babel/types": "^7.18.10" | ||
"@babel/types": "^7.18.10", | ||
"ignore": "^5.2.0" | ||
}, | ||
@@ -27,2 +28,2 @@ "devDependencies": { | ||
} | ||
} | ||
} |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
9066
216
0
6
2
+ Addedignore@^5.2.0
+ Addedignore@5.3.1(transitive)