@chaoswise/intl 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/bin/chaoswise-intl.js +5 -1
  2. package/bin/scripts/collect.js +38 -18
  3. package/bin/scripts/conf/default.js +40 -2
  4. package/bin/scripts/conf/getCustomConfig.js +8 -0
  5. package/bin/scripts/conf/index.js +2 -25
  6. package/bin/scripts/conf/initConfig.js +37 -0
  7. package/bin/scripts/initConfig.js +3 -0
  8. package/bin/scripts/service/index.js +2 -2
  9. package/bin/scripts/update.js +5 -5
  10. package/bin/scripts/util/FormPath/contexts.d.ts +10 -0
  11. package/bin/scripts/util/FormPath/contexts.js +23 -0
  12. package/bin/scripts/util/FormPath/destructor.d.ts +15 -0
  13. package/bin/scripts/util/FormPath/destructor.js +124 -0
  14. package/bin/scripts/util/FormPath/index.d.ts +49 -0
  15. package/bin/scripts/util/FormPath/index.js +536 -0
  16. package/bin/scripts/util/FormPath/lru.d.ts +1 -0
  17. package/bin/scripts/util/FormPath/lru.js +246 -0
  18. package/bin/scripts/util/FormPath/matcher.d.ts +33 -0
  19. package/bin/scripts/util/FormPath/matcher.js +216 -0
  20. package/bin/scripts/util/FormPath/parser.d.ts +28 -0
  21. package/bin/scripts/util/FormPath/parser.js +302 -0
  22. package/bin/scripts/util/FormPath/tokenizer.d.ts +26 -0
  23. package/bin/scripts/util/FormPath/tokenizer.js +280 -0
  24. package/bin/scripts/util/FormPath/tokens.d.ts +26 -0
  25. package/bin/scripts/util/FormPath/tokens.js +212 -0
  26. package/bin/scripts/util/FormPath/types.d.ts +76 -0
  27. package/bin/scripts/util/FormPath/types.js +17 -0
  28. package/bin/scripts/util/FormPath/utils.d.ts +10 -0
  29. package/bin/scripts/util/FormPath/utils.js +63 -0
  30. package/bin/scripts/util/downloadJson.js +10 -1
  31. package/bin/scripts/util/file.js +31 -0
  32. package/bin/scripts/util/getGroupName.js +15 -0
  33. package/bin/scripts/util/getWord.js +13 -0
  34. package/bin/scripts/util/log.js +25 -4
  35. package/bin/scripts/util/makeVisitorCollect.js +351 -46
  36. package/bin/scripts/util/makeVisitorUpdate.js +38 -1
  37. package/bin/scripts/util/specialMatch.js +14 -0
  38. package/bin/scripts/util/transformAst.js +30 -22
  39. package/bin/scripts/util/writeNewWordsFile.js +30 -0
  40. package/lib/index.js +2 -1
  41. package/package.json +7 -5
@@ -13,10 +13,12 @@ const pluginSyntaxAsyncGenerators = require('@babel/plugin-syntax-async-generato
13
13
  const pluginSyntaxDoExpressions = require('@babel/plugin-syntax-do-expressions');
14
14
  const pluginSyntaxDynamicImport = require('@babel/plugin-syntax-dynamic-import');
15
15
  const pluginSyntaxFunctionBind = require('@babel/plugin-syntax-function-bind');
16
+ const pluginExportDefaultFrom = require('@babel/plugin-proposal-export-default-from');
16
17
  const presetTypescript = require('@babel/preset-typescript').default;
17
18
 
18
19
  const makeVisitorCollect = require('./makeVisitorCollect');
19
20
  const makeVisitorUpdate = require('./makeVisitorUpdate');
21
+ const log = require('./log');
20
22
 
21
23
  // 获取文件中需要忽略转化通用国际化API规范的所有行号
22
24
  function getIgnoreLines(ast) {
@@ -69,16 +71,13 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
69
71
  const {
70
72
  entry,
71
73
  output,
72
- primaryRegx,
73
- importCode,
74
- i18nObject,
75
- i18nMethod,
76
74
  babelPresets = [],
77
75
  babelPlugins = [],
78
- ignoreComponents,
79
- ignoreMethods,
80
- ignoreAttributes,
76
+ specialFileReg,
77
+
78
+ ...opts
81
79
  } = conf;
80
+ const { importCode } = opts;
82
81
 
83
82
  const allWords = []; // 收集到的所有词条
84
83
  const downloadIds = []; // 需要从平台下载的词条id
@@ -105,24 +104,18 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
105
104
  pluginSyntaxDoExpressions,
106
105
  pluginSyntaxDynamicImport,
107
106
  pluginSyntaxFunctionBind,
107
+ pluginExportDefaultFrom,
108
108
  ...babelPlugins,
109
109
  ],
110
110
  };
111
111
 
112
- const opts = {
113
- primaryRegx,
114
- i18nObject,
115
- i18nMethod,
116
- importCode,
117
- ignoreLines: [],
118
- ignoreMethods,
119
- ignoreComponents,
120
- ignoreAttributes,
121
- };
122
-
123
112
  // 处理所有文件
124
113
  files.forEach((file) => {
125
114
  const { filePath } = file;
115
+
116
+ if (specialFileReg.some(reg => reg.test(filePath))) {
117
+ file.special = true;
118
+ }
126
119
  const isTSX = ['.ts', '.tsx'].includes(path.extname(filePath));
127
120
 
128
121
  const r = {
@@ -133,7 +126,16 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
133
126
  hasTouch: false, // 是否存在需要替换的词条
134
127
  };
135
128
  const sourceCode = fs.readFileSync(filePath, 'utf8');
136
- const ast = babel.parseSync(sourceCode, transformOptions);
129
+
130
+ let ast;
131
+ try {
132
+ ast = babel.parseSync(sourceCode, transformOptions);
133
+ } catch (error) {
134
+ replaceWords && log.error(`文件解析出错:${file.filePath}
135
+ ${error}`)
136
+ return;
137
+ }
138
+
137
139
  opts.ignoreLines = getIgnoreLines(ast);
138
140
 
139
141
  let makeVisitor = makeVisitorCollect;
@@ -141,8 +143,14 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
141
143
  makeVisitor = makeVisitorUpdate
142
144
  }
143
145
 
144
- const visitor = makeVisitor(opts, r);
145
- traverse(ast, visitor);
146
+ try {
147
+ const visitor = makeVisitor(opts, r, file);
148
+ traverse(ast, visitor);
149
+ } catch (e) {
150
+ replaceWords && log.error(`文件解析出错:${file.filePath}
151
+ ${e}`)
152
+ return;
153
+ }
146
154
 
147
155
  // 不传入需要替换的文词条则不进行文件修改
148
156
  // 在只需要提取中文词条的情况下不传入replaceWords
@@ -156,7 +164,7 @@ module.exports = function (type, files = [], conf = {}, replaceWords) {
156
164
 
157
165
  if (!r.hasTouch) {
158
166
  code = sourceCode;
159
- } else if (!r.hasImport && importCode) {
167
+ } else if (!r.hasImport && !file.special && importCode && Object.keys(replaceWords).length) {
160
168
  code = `${importCode}\n${code}`;
161
169
  }
162
170
 
@@ -0,0 +1,30 @@
1
+ const file = require('./file');
2
+ const log = require('./log');
3
+ const getGroupName = require('./getGroupName');
4
+
5
+ module.exports = async function writeNewWordsFile(type, newWords) {
6
+ const isJson = type === 'json';
7
+ type = isJson? 'json': 'xlsx';
8
+ // 读取groupName
9
+ const groupName = getGroupName();
10
+ const fileName = `${groupName}.${type}`
11
+
12
+ // 写入json
13
+ if (isJson) {
14
+ const oldNewWords = file.readJson(fileName) || {};
15
+
16
+ file.write(
17
+ fileName,
18
+ JSON.stringify({ ...oldNewWords, ...newWords }, null, 2)
19
+ );
20
+ } else {
21
+ // 写入xlsx
22
+ const oldNewWords = file.readElsx(fileName)?.Sheet1 || [];
23
+ // 把新增的词条对象,转为数组
24
+ const newWordsArr = Object.entries(newWords).map(([id, word]) => ({ id, ...word }));
25
+ // 合并数据,并写入文件
26
+ file.writeXlsx(fileName, { Sheet1: [...oldNewWords, ...newWordsArr ] });
27
+ }
28
+
29
+ log.success(`需要翻译的文件在项目根目录的${fileName}中`);
30
+ };
package/lib/index.js CHANGED
@@ -25,4 +25,5 @@ _Object$defineProperty(exports, "useIntl", {
25
25
  });
26
26
  var _useIntl = _interopRequireDefault(require("./useIntl"));
27
27
  var _context = _interopRequireDefault(require("./useIntl/context"));
28
- var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
28
+ var _reactIntlUniversal = _interopRequireDefault(require("react-intl-universal"));
29
+ _reactIntlUniversal["default"].options.escapeHtml = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chaoswise/intl",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "author": "cloudwiser",
5
5
  "description": "intl",
6
6
  "main": "lib/index.js",
@@ -67,19 +67,21 @@
67
67
  "babel-plugin-macros": "^3.1.0",
68
68
  "babel-plugin-polyfill-corejs3": "^0.5.2",
69
69
  "chalk": "^2.4.1",
70
+ "glob": "^8.0.3",
70
71
  "gulp": "^4.0.2",
71
72
  "gulp-babel": "^8.0.0",
72
- "gulp-typescript": "^5.0.1",
73
73
  "gulp-connect": "^5.7.0",
74
74
  "gulp-jsdoc3": "^3.0.0",
75
+ "gulp-typescript": "^5.0.1",
75
76
  "prettier": "^2.8.1",
76
- "glob": "^8.0.3",
77
77
  "react-intl-universal": "^2.6.11",
78
- "uuid": "^9.0.0"
78
+ "uuid": "^9.0.0",
79
+ "xlsx": "^0.18.5"
79
80
  },
80
81
  "devDependencies": {
81
82
  "react": "^16.13.1",
82
83
  "react-dom": "^16.13.1"
83
84
  },
84
- "license": "MIT"
85
+ "license": "MIT",
86
+ "gitHead": "e56a7289b5085c5d38813e85776dbd5f789606b0"
85
87
  }