@chaoswise/intl 1.1.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.
@@ -5,7 +5,7 @@ const getTargetFiles = require('./util/getTargetFiles');
5
5
  const transformAst = require('./util/transformAst');
6
6
  const log = require('./util/log');
7
7
  const file = require('./util/file');
8
- const getGroupName = require('./util/getGroupName');
8
+ const writeNewWordsFile = require('./util/writeNewWordsFile');
9
9
  const { readWordJson } = require('./util/getWord');
10
10
 
11
11
  const service = require('./service');
@@ -13,14 +13,19 @@ const service = require('./service');
13
13
  async function collect() {
14
14
  log.info('词条扫描中...')
15
15
  const conf = getConf();
16
+ const newWordsFileType = conf.newWordsFileType || 'excel';
17
+
18
+ // 检查newWordsFileType
19
+ if (!['json', 'excel'].includes(newWordsFileType)) {
20
+ log.error(`配置:newWordsFileType 只能为json或者excel`);
21
+ process.exit(1);
22
+ }
16
23
 
17
24
  // 读取wordJson
18
25
  if (conf.localWordPath && !readWordJson(conf.localWordPath)) {
19
26
  log.error('localWordPath配置有误');
20
27
  process.exit(1);
21
28
  }
22
- // 读取groupName
23
- const groupName = getGroupName();
24
29
 
25
30
  // 获取需要遍历的文件
26
31
  const files = getTargetFiles(conf);
@@ -67,15 +72,14 @@ async function collect() {
67
72
  log.success('自定义配置文件在项目根目录的.intlconfig.js中');
68
73
  // 本次扫描出新的待翻译词条,写入到newWords中
69
74
  if (Object.keys(newWords).length) {
70
- const newWordsJsonPath = 'newWords.json';
71
-
72
- const oldNewWords = file.readJson(newWordsJsonPath) || {};
73
- file.write(
74
- newWordsJsonPath,
75
- JSON.stringify({ ...oldNewWords, ...newWords, groupName }, null, 2)
76
- );
75
+ writeNewWordsFile(newWordsFileType, newWords);
76
+ }
77
77
 
78
- log.success(`需要翻译的文件在项目根目录的newWords.json中`);
78
+ const warnLogs = log.logs.warn;
79
+ if (warnLogs.length) {
80
+ const fileName = `intl.logs.warn.${(new Date).toLocaleString().replace(/[\/ ]/g, '_')}.txt`;
81
+ file.write(fileName, JSON.stringify(warnLogs, null, 2));
82
+ log.warnToLog(`警告日志文件:${fileName}`);
79
83
  }
80
84
  }
81
85
 
@@ -89,6 +89,9 @@ module.exports = function (excludes = []) {
89
89
  // 特殊文件处理的正则 这里默认匹配router.config.js
90
90
  specialFileReg: [/src\/config\/router\.config\.js/],
91
91
 
92
+ // newWords文件的类型 json或者excel
93
+ newWordsFileType: 'excel',
94
+
92
95
  // 国际化平台地址
93
96
  baseURL: 'http://10.0.1.133:18000',
94
97
  };
@@ -0,0 +1,10 @@
1
+ export declare type Context = {
2
+ flag: string;
3
+ [key: string]: any;
4
+ };
5
+ export declare const bracketContext: Context;
6
+ export declare const bracketArrayContext: Context;
7
+ export declare const bracketDContext: Context;
8
+ export declare const parenContext: Context;
9
+ export declare const braceContext: Context;
10
+ export declare const destructorContext: Context;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ Object.defineProperty(exports, "__esModule", { value: true });
14
+ exports.destructorContext = exports.braceContext = exports.parenContext = exports.bracketDContext = exports.bracketArrayContext = exports.bracketContext = void 0;
15
+ var ContextType = function (flag, props) {
16
+ return __assign({ flag: flag }, props);
17
+ };
18
+ exports.bracketContext = ContextType("[]");
19
+ exports.bracketArrayContext = ContextType("[\\d]");
20
+ exports.bracketDContext = ContextType("[[]]");
21
+ exports.parenContext = ContextType("()");
22
+ exports.braceContext = ContextType("{}");
23
+ exports.destructorContext = ContextType("{x}");
@@ -0,0 +1,15 @@
1
+ import { Segments, Node, DestrcutorRules } from './types';
2
+ declare type Mutatators = {
3
+ getIn: (segments: Segments, source: any) => any;
4
+ setIn: (segments: Segments, source: any, value: any) => void;
5
+ deleteIn?: (segments: Segments, source: any) => any;
6
+ existIn?: (segments: Segments, source: any, start: number) => boolean;
7
+ };
8
+ export declare const getDestructor: (source: string) => any;
9
+ export declare const setDestructor: (source: string, rules: DestrcutorRules) => void;
10
+ export declare const parseDestructorRules: (node: Node) => DestrcutorRules;
11
+ export declare const setInByDestructor: (source: any, rules: DestrcutorRules, value: any, mutators: Mutatators) => void;
12
+ export declare const getInByDestructor: (source: any, rules: DestrcutorRules, mutators: Mutatators) => {};
13
+ export declare const deleteInByDestructor: (source: any, rules: DestrcutorRules, mutators: Mutatators) => void;
14
+ export declare const existInByDestructor: (source: any, rules: DestrcutorRules, start: number, mutators: Mutatators) => boolean;
15
+ export {};
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.existInByDestructor = exports.deleteInByDestructor = exports.getInByDestructor = exports.setInByDestructor = exports.parseDestructorRules = exports.setDestructor = exports.getDestructor = void 0;
4
+ var types_1 = require("./types");
5
+ var utils_1 = require("./utils");
6
+ var DestrcutorCache = new Map();
7
+ var isValid = function (val) { return val !== undefined && val !== null; };
8
+ exports.getDestructor = function (source) {
9
+ return DestrcutorCache.get(source);
10
+ };
11
+ exports.setDestructor = function (source, rules) {
12
+ DestrcutorCache.set(source, rules);
13
+ };
14
+ exports.parseDestructorRules = function (node) {
15
+ var rules = [];
16
+ if (types_1.isObjectPattern(node)) {
17
+ var index_1 = 0;
18
+ node.properties.forEach(function (child) {
19
+ rules[index_1] = {
20
+ path: []
21
+ };
22
+ rules[index_1].key = child.key.value;
23
+ rules[index_1].path.push(child.key.value);
24
+ if (types_1.isIdentifier(child.value)) {
25
+ rules[index_1].key = child.value.value;
26
+ }
27
+ var basePath = rules[index_1].path;
28
+ var childRules = exports.parseDestructorRules(child.value);
29
+ var k = index_1;
30
+ childRules.forEach(function (rule) {
31
+ if (rules[k]) {
32
+ rules[k].key = rule.key;
33
+ rules[k].path = basePath.concat(rule.path);
34
+ }
35
+ else {
36
+ rules[k] = {
37
+ key: rule.key,
38
+ path: basePath.concat(rule.path)
39
+ };
40
+ }
41
+ k++;
42
+ });
43
+ if (k > index_1) {
44
+ index_1 = k;
45
+ }
46
+ else {
47
+ index_1++;
48
+ }
49
+ });
50
+ return rules;
51
+ }
52
+ else if (types_1.isArrayPattern(node)) {
53
+ var index_2 = 0;
54
+ node.elements.forEach(function (child, key) {
55
+ rules[index_2] = {
56
+ path: []
57
+ };
58
+ rules[index_2].key = key;
59
+ rules[index_2].path.push(key);
60
+ if (types_1.isIdentifier(child)) {
61
+ rules[index_2].key = child.value;
62
+ }
63
+ var basePath = rules[index_2].path;
64
+ var childRules = exports.parseDestructorRules(child);
65
+ var k = index_2;
66
+ childRules.forEach(function (rule) {
67
+ if (rules[k]) {
68
+ rules[k].key = rule.key;
69
+ rules[k].path = basePath.concat(rule.path);
70
+ }
71
+ else {
72
+ rules[k] = {
73
+ key: rule.key,
74
+ path: basePath.concat(rule.path)
75
+ };
76
+ }
77
+ k++;
78
+ });
79
+ if (k > index_2) {
80
+ index_2 = k;
81
+ }
82
+ else {
83
+ index_2++;
84
+ }
85
+ });
86
+ return rules;
87
+ }
88
+ if (types_1.isDestructorExpression(node)) {
89
+ return exports.parseDestructorRules(node.value);
90
+ }
91
+ return rules;
92
+ };
93
+ exports.setInByDestructor = function (source, rules, value, mutators) {
94
+ rules.forEach(function (_a) {
95
+ var key = _a.key, path = _a.path;
96
+ mutators.setIn([key], source, mutators.getIn(path, value));
97
+ });
98
+ };
99
+ exports.getInByDestructor = function (source, rules, mutators) {
100
+ var response = {};
101
+ if (rules.length) {
102
+ if (utils_1.isNum(rules[0].path[0])) {
103
+ response = [];
104
+ }
105
+ }
106
+ source = isValid(source) ? source : {};
107
+ rules.forEach(function (_a) {
108
+ var key = _a.key, path = _a.path;
109
+ mutators.setIn(path, response, source[key]);
110
+ });
111
+ return response;
112
+ };
113
+ exports.deleteInByDestructor = function (source, rules, mutators) {
114
+ rules.forEach(function (_a) {
115
+ var key = _a.key;
116
+ mutators.deleteIn([key], source);
117
+ });
118
+ };
119
+ exports.existInByDestructor = function (source, rules, start, mutators) {
120
+ return rules.every(function (_a) {
121
+ var key = _a.key;
122
+ return mutators.existIn([key], source, start);
123
+ });
124
+ };
@@ -0,0 +1,49 @@
1
+ import { Segments, Node, Pattern } from './types';
2
+ export * from './types';
3
+ export declare class Path {
4
+ entire: string;
5
+ segments: Segments;
6
+ isMatchPattern: boolean;
7
+ isWildMatchPattern: boolean;
8
+ haveExcludePattern: boolean;
9
+ matchScore: number;
10
+ tree: Node;
11
+ private matchCache;
12
+ private includesCache;
13
+ constructor(input: Pattern);
14
+ toString(): string;
15
+ toArray(): Segments;
16
+ get length(): number;
17
+ private parse;
18
+ private parseString;
19
+ concat: (...args: Pattern[]) => Path;
20
+ slice: (start?: number, end?: number) => Path;
21
+ push: (item: Pattern) => this;
22
+ pop: () => void;
23
+ splice: (start: number, deleteCount?: number, ...items: Array<string | number>) => this;
24
+ forEach: (callback: (key: string | number) => any) => void;
25
+ map: (callback: (key: string | number) => any) => any[];
26
+ reduce: <T>(callback: (buf: T, item: string | number, index: number) => T, initial: T) => T;
27
+ getNearestChildPathBy: (target?: Pattern) => Path;
28
+ parent: () => Path;
29
+ includes: (pattern: Pattern) => any;
30
+ transform: <T>(regexp: string | RegExp, callback: (...args: string[]) => T) => string | T;
31
+ match: (pattern: Pattern) => boolean;
32
+ matchAliasGroup: (name: Pattern, alias: Pattern) => boolean;
33
+ existIn: (source?: any, start?: number | Path) => boolean;
34
+ getIn: (source?: any) => any;
35
+ setIn: (source?: any, value?: any) => any;
36
+ deleteIn: (source?: any) => any;
37
+ static match(pattern: Pattern): {
38
+ (target: any): boolean;
39
+ path: Path;
40
+ };
41
+ static transform<T>(pattern: Pattern, regexp: string | RegExp, callback: (...args: string[]) => T): any;
42
+ static parse(path?: Pattern): Path;
43
+ static getPath(path?: Pattern): Path;
44
+ static getIn: (source: any, pattern: Pattern) => any;
45
+ static setIn: (source: any, pattern: Pattern, value: any) => any;
46
+ static deleteIn: (source: any, pattern: Pattern) => any;
47
+ static existIn: (source: any, pattern: Pattern, start?: number | Path) => boolean;
48
+ }
49
+ export default Path;