oak-domain 4.0.3 → 4.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.
- package/lib/base-app-domain/ActionAuth/Schema.d.ts +1 -2
- package/lib/base-app-domain/ActionDefDict.d.ts +2 -2
- package/lib/base-app-domain/I18n/Schema.d.ts +1 -2
- package/lib/base-app-domain/Modi/Schema.d.ts +1 -2
- package/lib/base-app-domain/ModiEntity/Schema.d.ts +1 -2
- package/lib/base-app-domain/Oper/Schema.d.ts +8 -3
- package/lib/base-app-domain/Oper/Storage.js +19 -1
- package/lib/base-app-domain/OperEntity/Schema.d.ts +1 -2
- package/lib/base-app-domain/Path/Schema.d.ts +1 -2
- package/lib/base-app-domain/Relation/Schema.d.ts +1 -2
- package/lib/base-app-domain/RelationAuth/Schema.d.ts +1 -2
- package/lib/base-app-domain/User/Action.d.ts +1 -0
- package/lib/base-app-domain/User/Action.js +3 -3
- package/lib/base-app-domain/User/Schema.d.ts +1 -2
- package/lib/base-app-domain/UserEntityClaim/Schema.d.ts +1 -2
- package/lib/base-app-domain/UserEntityGrant/Action.d.ts +5 -5
- package/lib/base-app-domain/UserEntityGrant/Action.js +5 -5
- package/lib/base-app-domain/UserEntityGrant/Schema.d.ts +1 -2
- package/lib/base-app-domain/UserRelation/Schema.d.ts +1 -2
- package/lib/compiler/localeBuilder.d.ts +27 -27
- package/lib/compiler/localeBuilder.js +215 -215
- package/lib/compiler/routerBuilder.js +263 -263
- package/lib/compiler/schemalBuilder.js +1058 -460
- package/lib/entities/Oper.d.ts +2 -1
- package/lib/entities/Oper.js +16 -1
- package/lib/entities/User.d.ts +10 -0
- package/lib/entities/User.js +3 -2
- package/lib/index.d.ts +0 -22
- package/lib/index.js +1 -35
- package/lib/store/AsyncRowStore.d.ts +2 -1
- package/lib/store/AsyncRowStore.js +180 -180
- package/lib/store/CascadeStore.js +7 -31
- package/lib/store/RelationAuth.d.ts +103 -103
- package/lib/store/RelationAuth.js +1209 -1343
- package/lib/store/SyncRowStore.d.ts +1 -1
- package/lib/store/TriggerExecutor.js +1 -1
- package/lib/store/actionDef.js +4 -4
- package/lib/store/checker.js +487 -487
- package/lib/triggers/index.d.ts +2 -2
- package/lib/triggers/index.js +21 -1
- package/lib/types/Aspect.d.ts +1 -1
- package/lib/types/Configuration.d.ts +42 -0
- package/lib/types/Configuration.js +3 -0
- package/lib/types/Connector.d.ts +1 -1
- package/lib/types/Context.d.ts +1 -1
- package/lib/types/Entity.d.ts +4 -4
- package/lib/types/Environment.d.ts +3 -0
- package/lib/types/Exception.d.ts +155 -155
- package/lib/types/Exception.js +436 -436
- package/lib/types/Sync.d.ts +48 -0
- package/lib/types/Sync.js +8 -0
- package/lib/types/index.d.ts +1 -0
- package/lib/types/index.js +1 -0
- package/lib/utils/SimpleConnector.d.ts +1 -1
- package/lib/utils/SimpleConnector.js +1 -1
- package/lib/utils/assert.d.ts +0 -1
- package/lib/utils/projection.d.ts +1 -1
- package/lib/utils/relationPath.d.ts +31 -0
- package/lib/utils/relationPath.js +202 -0
- package/package.json +51 -51
- package/src/entities/ActionAuth.ts +41 -41
- package/src/entities/I18n.ts +45 -45
- package/src/entities/Modi.ts +69 -69
- package/src/entities/ModiEntity.ts +26 -26
- package/src/entities/Oper.ts +48 -32
- package/src/entities/OperEntity.ts +27 -27
- package/src/entities/Path.ts +43 -43
- package/src/entities/Relation.ts +43 -43
- package/src/entities/RelationAuth.ts +44 -44
- package/src/entities/User.ts +48 -48
- package/src/entities/UserEntityClaim.ts +29 -29
- package/src/entities/UserEntityGrant.ts +24 -24
- package/src/entities/UserRelation.ts +50 -50
|
@@ -1,215 +1,215 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const tslib_1 = require("tslib");
|
|
4
|
-
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
5
|
-
const ts = tslib_1.__importStar(require("typescript"));
|
|
6
|
-
const { factory } = ts;
|
|
7
|
-
const path_1 = require("path");
|
|
8
|
-
const crypto_1 = require("crypto");
|
|
9
|
-
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
10
|
-
const env_1 = require("./env");
|
|
11
|
-
const string_1 = require("../utils/string");
|
|
12
|
-
/**
|
|
13
|
-
* 将一个object展开编译为一棵语法树,只有string和object两种键值对象
|
|
14
|
-
* @param data
|
|
15
|
-
*/
|
|
16
|
-
function transferObjectToObjectLiteral(data) {
|
|
17
|
-
return factory.createObjectLiteralExpression(Object.keys(data).map((k) => {
|
|
18
|
-
const type = typeof data[k];
|
|
19
|
-
if (type === 'string') {
|
|
20
|
-
return factory.createPropertyAssignment(factory.createStringLiteral(k), factory.createStringLiteral(data[k]));
|
|
21
|
-
}
|
|
22
|
-
(0, assert_1.default)(type === 'object');
|
|
23
|
-
return factory.createPropertyAssignment(factory.createStringLiteral(k), transferObjectToObjectLiteral(data[k]));
|
|
24
|
-
}), true);
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* 这个类的作用是把项目和所有相关的模块下的locales编译成为src/data/i18n中的数据
|
|
28
|
-
*/
|
|
29
|
-
class LocaleBuilder {
|
|
30
|
-
asLib;
|
|
31
|
-
dependencies;
|
|
32
|
-
pwd;
|
|
33
|
-
locales; // key: namespace, value: [module, position, language, data]
|
|
34
|
-
hash;
|
|
35
|
-
constructor(asLib) {
|
|
36
|
-
const pwd = process.cwd();
|
|
37
|
-
this.pwd = pwd;
|
|
38
|
-
this.asLib = !!asLib;
|
|
39
|
-
const dependencyFile = (0, env_1.OAK_EXTERNAL_LIBS_FILEPATH)((0, path_1.join)(pwd, 'src'));
|
|
40
|
-
if (fs_1.default.existsSync(dependencyFile)) {
|
|
41
|
-
this.dependencies = require(dependencyFile);
|
|
42
|
-
}
|
|
43
|
-
else {
|
|
44
|
-
this.dependencies = [];
|
|
45
|
-
}
|
|
46
|
-
this.locales = {};
|
|
47
|
-
this.hash = (0, crypto_1.createHash)('md5');
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* 将locales输出成为data/i18n.ts中的数据
|
|
51
|
-
* 如果有Dependency需要引出来
|
|
52
|
-
*/
|
|
53
|
-
outputDataFile() {
|
|
54
|
-
const statements = [
|
|
55
|
-
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("CreateOperationData"), factory.createIdentifier("I18n"))])), factory.createStringLiteral("../oak-app-domain/I18n/Schema"), undefined)
|
|
56
|
-
];
|
|
57
|
-
// 改为在初始化时合并
|
|
58
|
-
/* if (this.dependencies) {
|
|
59
|
-
this.dependencies.forEach(
|
|
60
|
-
(ele, idx) => statements.push(
|
|
61
|
-
factory.createImportDeclaration(
|
|
62
|
-
undefined,
|
|
63
|
-
factory.createImportClause(
|
|
64
|
-
false,
|
|
65
|
-
factory.createIdentifier(`i18ns${idx}`),
|
|
66
|
-
undefined
|
|
67
|
-
),
|
|
68
|
-
factory.createStringLiteral(`${ele}/lib/data/i18n`),
|
|
69
|
-
undefined
|
|
70
|
-
)
|
|
71
|
-
)
|
|
72
|
-
)
|
|
73
|
-
} */
|
|
74
|
-
statements.push(factory.createVariableStatement(undefined, factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier("i18ns"), undefined, factory.createArrayTypeNode(factory.createTypeReferenceNode(factory.createIdentifier("I18n"), undefined)), factory.createArrayLiteralExpression(Object.keys(this.locales).map((k) => {
|
|
75
|
-
const [module, position, language, data] = this.locales[k];
|
|
76
|
-
// 用哈希计算来保证id唯一性
|
|
77
|
-
const h = this.hash.copy();
|
|
78
|
-
h.update(`${k}-${language}`);
|
|
79
|
-
const id = h.digest('hex');
|
|
80
|
-
(0, assert_1.default)(id.length <= 36);
|
|
81
|
-
return factory.createObjectLiteralExpression([
|
|
82
|
-
factory.createPropertyAssignment(factory.createIdentifier("id"), factory.createStringLiteral(id)),
|
|
83
|
-
factory.createPropertyAssignment(factory.createIdentifier("namespace"), factory.createStringLiteral(k)),
|
|
84
|
-
factory.createPropertyAssignment(factory.createIdentifier("language"), factory.createStringLiteral(language)),
|
|
85
|
-
factory.createPropertyAssignment(factory.createIdentifier("module"), factory.createStringLiteral(module)),
|
|
86
|
-
factory.createPropertyAssignment(factory.createIdentifier("position"), factory.createStringLiteral(position)),
|
|
87
|
-
factory.createPropertyAssignment(factory.createIdentifier("data"), transferObjectToObjectLiteral(data))
|
|
88
|
-
], true);
|
|
89
|
-
}), true))], ts.NodeFlags.Const)));
|
|
90
|
-
/* if (this.dependencies.length > 0) {
|
|
91
|
-
statements.push(
|
|
92
|
-
factory.createExportAssignment(
|
|
93
|
-
undefined,
|
|
94
|
-
undefined,
|
|
95
|
-
factory.createCallExpression(
|
|
96
|
-
factory.createPropertyAccessExpression(
|
|
97
|
-
factory.createIdentifier("i18ns"),
|
|
98
|
-
factory.createIdentifier("concat")
|
|
99
|
-
),
|
|
100
|
-
undefined,
|
|
101
|
-
this.dependencies.map(
|
|
102
|
-
(ele, idx) => factory.createIdentifier(`i18ns${idx}`)
|
|
103
|
-
)
|
|
104
|
-
)
|
|
105
|
-
)
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
else { */
|
|
109
|
-
statements.push(factory.createExportAssignment(undefined, undefined, factory.createIdentifier("i18ns")));
|
|
110
|
-
/* } */
|
|
111
|
-
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
112
|
-
const result = printer.printList(ts.ListFormat.SourceFileStatements, factory.createNodeArray(statements), ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, false, ts.ScriptKind.TS));
|
|
113
|
-
const filename = (0, path_1.join)(this.pwd, 'src', 'data', 'i18n.ts');
|
|
114
|
-
const result2 = (0, string_1.unescapeUnicode)(`// 本文件为自动编译产生,请勿直接修改\n\n${result}`);
|
|
115
|
-
fs_1.default.writeFileSync(filename, result2, { flag: 'w' });
|
|
116
|
-
}
|
|
117
|
-
/**
|
|
118
|
-
* 这里不能直接用require, webpack貌似有缓存
|
|
119
|
-
* @param filepath
|
|
120
|
-
*/
|
|
121
|
-
readLocaleFileContent(filepath) {
|
|
122
|
-
(0, assert_1.default)(filepath.endsWith('.json'));
|
|
123
|
-
const content = fs_1.default.readFileSync(filepath, {
|
|
124
|
-
encoding: 'utf-8',
|
|
125
|
-
});
|
|
126
|
-
return JSON.parse(content);
|
|
127
|
-
}
|
|
128
|
-
parseFile(module, namespace, position, filename, filepath, watch) {
|
|
129
|
-
const language = (filename.split('.')[0]).replace('_', '-'); // 历史原因,会命名成zh_CN.json
|
|
130
|
-
const data = this.readLocaleFileContent(filepath);
|
|
131
|
-
const ns = module ? `${module}-${namespace}` : (0, string_1.firstLetterLowerCase)(namespace);
|
|
132
|
-
this.locales[ns] = [module, position.replace(/\\/g, '/'), language, data];
|
|
133
|
-
if (watch) {
|
|
134
|
-
fs_1.default.watch(filepath, () => {
|
|
135
|
-
const data = this.readLocaleFileContent(filepath);
|
|
136
|
-
this.locales[ns] = [module, position.replace(/\\/g, '/'), language, data];
|
|
137
|
-
this.outputDataFile();
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
traverse(module, nsPrefix, position, dirPath, inLocale, localeFolderName, watch) {
|
|
142
|
-
const files = fs_1.default.readdirSync(dirPath);
|
|
143
|
-
files.forEach((file) => {
|
|
144
|
-
const filepath = (0, path_1.join)(dirPath, file);
|
|
145
|
-
const stat = fs_1.default.statSync(filepath);
|
|
146
|
-
if (stat.isFile() && inLocale && file.endsWith('.json')) {
|
|
147
|
-
this.parseFile(module, nsPrefix, position, file, filepath, watch);
|
|
148
|
-
}
|
|
149
|
-
else if (stat.isDirectory() && !inLocale) {
|
|
150
|
-
const nsPrefix2 = nsPrefix ? `${nsPrefix}-${file}` : file;
|
|
151
|
-
const isLocaleFolder = file === localeFolderName;
|
|
152
|
-
this.traverse(module, isLocaleFolder ? nsPrefix : nsPrefix2, isLocaleFolder ? position : (0, path_1.join)(position, file), (0, path_1.join)(dirPath, file), isLocaleFolder, localeFolderName, watch);
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
buildProject(root, src, watch) {
|
|
157
|
-
const packageJson = (0, path_1.join)(root, 'package.json');
|
|
158
|
-
const { name } = require(packageJson);
|
|
159
|
-
const pagePath = (0, path_1.join)(src ? 'src' : 'es', 'pages');
|
|
160
|
-
const pageAbsolutePath = (0, path_1.join)(root, pagePath); //编译i18时font中的componentPath缺少根目录导致编译不出
|
|
161
|
-
if (fs_1.default.existsSync(pageAbsolutePath)) {
|
|
162
|
-
this.traverse(name, 'p', pagePath, pageAbsolutePath, false, 'locales', watch);
|
|
163
|
-
}
|
|
164
|
-
const componentPath = (0, path_1.join)(src ? 'src' : 'es', 'components');
|
|
165
|
-
const componentAbsolutePath = (0, path_1.join)(root, componentPath);
|
|
166
|
-
if (fs_1.default.existsSync(componentAbsolutePath)) {
|
|
167
|
-
this.traverse(name, 'c', componentPath, componentAbsolutePath, false, 'locales', watch);
|
|
168
|
-
}
|
|
169
|
-
const localePath = (0, path_1.join)(root, src ? 'src' : 'es', 'locales');
|
|
170
|
-
if (fs_1.default.existsSync(localePath)) {
|
|
171
|
-
const files = fs_1.default.readdirSync(localePath);
|
|
172
|
-
files.forEach((file) => {
|
|
173
|
-
const filepath = (0, path_1.join)(localePath, file);
|
|
174
|
-
const stat = fs_1.default.statSync(filepath);
|
|
175
|
-
if (stat.isDirectory()) {
|
|
176
|
-
this.traverse(name, `l-${file}`, (0, path_1.join)('locales', file), (0, path_1.join)(localePath, file), true, file, watch);
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
if (!this.asLib && src) {
|
|
181
|
-
// 不是lib的话将oak-app-domain中的对象的locale也收集起来
|
|
182
|
-
const domainPath = (0, path_1.join)(root, 'src', 'oak-app-domain');
|
|
183
|
-
if (fs_1.default.existsSync(domainPath)) {
|
|
184
|
-
this.traverse('', '', 'oak-app-domain', domainPath, false, 'locales', watch);
|
|
185
|
-
}
|
|
186
|
-
// 还有web和wechatMp的目录
|
|
187
|
-
const webSrcPath = (0, path_1.join)('web', 'src');
|
|
188
|
-
this.traverse(name, 'w', webSrcPath, (0, path_1.join)(root, webSrcPath), false, 'locales', watch);
|
|
189
|
-
// 小程序可能有多于一个,按规范用wechatMp, wechatMp2这样命名
|
|
190
|
-
const wechatMpSrcPath = (0, path_1.join)('wechatMp', 'src');
|
|
191
|
-
this.traverse(name, 'wmp', wechatMpSrcPath, (0, path_1.join)(root, webSrcPath), false, 'locales', watch);
|
|
192
|
-
let iter = 1;
|
|
193
|
-
while (true) {
|
|
194
|
-
const mpSrcPath = `${wechatMpSrcPath}${iter}`;
|
|
195
|
-
if (fs_1.default.existsSync((0, path_1.join)(root, mpSrcPath))) {
|
|
196
|
-
this.traverse(name, `wmp${iter}`, mpSrcPath, (0, path_1.join)(root, mpSrcPath), false, 'locales', watch);
|
|
197
|
-
iter++;
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
break;
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
build(watch) {
|
|
206
|
-
this.buildProject(this.pwd, true, watch);
|
|
207
|
-
if (!this.asLib) {
|
|
208
|
-
// 如果不是lib,把front里的数据也处理掉
|
|
209
|
-
const fbPath = (0, path_1.join)(this.pwd, 'node_modules', 'oak-frontend-base');
|
|
210
|
-
this.buildProject(fbPath, false, watch);
|
|
211
|
-
}
|
|
212
|
-
this.outputDataFile();
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
exports.default = LocaleBuilder;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const assert_1 = tslib_1.__importDefault(require("assert"));
|
|
5
|
+
const ts = tslib_1.__importStar(require("typescript"));
|
|
6
|
+
const { factory } = ts;
|
|
7
|
+
const path_1 = require("path");
|
|
8
|
+
const crypto_1 = require("crypto");
|
|
9
|
+
const fs_1 = tslib_1.__importDefault(require("fs"));
|
|
10
|
+
const env_1 = require("./env");
|
|
11
|
+
const string_1 = require("../utils/string");
|
|
12
|
+
/**
|
|
13
|
+
* 将一个object展开编译为一棵语法树,只有string和object两种键值对象
|
|
14
|
+
* @param data
|
|
15
|
+
*/
|
|
16
|
+
function transferObjectToObjectLiteral(data) {
|
|
17
|
+
return factory.createObjectLiteralExpression(Object.keys(data).map((k) => {
|
|
18
|
+
const type = typeof data[k];
|
|
19
|
+
if (type === 'string') {
|
|
20
|
+
return factory.createPropertyAssignment(factory.createStringLiteral(k), factory.createStringLiteral(data[k]));
|
|
21
|
+
}
|
|
22
|
+
(0, assert_1.default)(type === 'object');
|
|
23
|
+
return factory.createPropertyAssignment(factory.createStringLiteral(k), transferObjectToObjectLiteral(data[k]));
|
|
24
|
+
}), true);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 这个类的作用是把项目和所有相关的模块下的locales编译成为src/data/i18n中的数据
|
|
28
|
+
*/
|
|
29
|
+
class LocaleBuilder {
|
|
30
|
+
asLib;
|
|
31
|
+
dependencies;
|
|
32
|
+
pwd;
|
|
33
|
+
locales; // key: namespace, value: [module, position, language, data]
|
|
34
|
+
hash;
|
|
35
|
+
constructor(asLib) {
|
|
36
|
+
const pwd = process.cwd();
|
|
37
|
+
this.pwd = pwd;
|
|
38
|
+
this.asLib = !!asLib;
|
|
39
|
+
const dependencyFile = (0, env_1.OAK_EXTERNAL_LIBS_FILEPATH)((0, path_1.join)(pwd, 'src'));
|
|
40
|
+
if (fs_1.default.existsSync(dependencyFile)) {
|
|
41
|
+
this.dependencies = require(dependencyFile);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.dependencies = [];
|
|
45
|
+
}
|
|
46
|
+
this.locales = {};
|
|
47
|
+
this.hash = (0, crypto_1.createHash)('md5');
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 将locales输出成为data/i18n.ts中的数据
|
|
51
|
+
* 如果有Dependency需要引出来
|
|
52
|
+
*/
|
|
53
|
+
outputDataFile() {
|
|
54
|
+
const statements = [
|
|
55
|
+
factory.createImportDeclaration(undefined, factory.createImportClause(false, undefined, factory.createNamedImports([factory.createImportSpecifier(false, factory.createIdentifier("CreateOperationData"), factory.createIdentifier("I18n"))])), factory.createStringLiteral("../oak-app-domain/I18n/Schema"), undefined)
|
|
56
|
+
];
|
|
57
|
+
// 改为在初始化时合并
|
|
58
|
+
/* if (this.dependencies) {
|
|
59
|
+
this.dependencies.forEach(
|
|
60
|
+
(ele, idx) => statements.push(
|
|
61
|
+
factory.createImportDeclaration(
|
|
62
|
+
undefined,
|
|
63
|
+
factory.createImportClause(
|
|
64
|
+
false,
|
|
65
|
+
factory.createIdentifier(`i18ns${idx}`),
|
|
66
|
+
undefined
|
|
67
|
+
),
|
|
68
|
+
factory.createStringLiteral(`${ele}/lib/data/i18n`),
|
|
69
|
+
undefined
|
|
70
|
+
)
|
|
71
|
+
)
|
|
72
|
+
)
|
|
73
|
+
} */
|
|
74
|
+
statements.push(factory.createVariableStatement(undefined, factory.createVariableDeclarationList([factory.createVariableDeclaration(factory.createIdentifier("i18ns"), undefined, factory.createArrayTypeNode(factory.createTypeReferenceNode(factory.createIdentifier("I18n"), undefined)), factory.createArrayLiteralExpression(Object.keys(this.locales).map((k) => {
|
|
75
|
+
const [module, position, language, data] = this.locales[k];
|
|
76
|
+
// 用哈希计算来保证id唯一性
|
|
77
|
+
const h = this.hash.copy();
|
|
78
|
+
h.update(`${k}-${language}`);
|
|
79
|
+
const id = h.digest('hex');
|
|
80
|
+
(0, assert_1.default)(id.length <= 36);
|
|
81
|
+
return factory.createObjectLiteralExpression([
|
|
82
|
+
factory.createPropertyAssignment(factory.createIdentifier("id"), factory.createStringLiteral(id)),
|
|
83
|
+
factory.createPropertyAssignment(factory.createIdentifier("namespace"), factory.createStringLiteral(k)),
|
|
84
|
+
factory.createPropertyAssignment(factory.createIdentifier("language"), factory.createStringLiteral(language)),
|
|
85
|
+
factory.createPropertyAssignment(factory.createIdentifier("module"), factory.createStringLiteral(module)),
|
|
86
|
+
factory.createPropertyAssignment(factory.createIdentifier("position"), factory.createStringLiteral(position)),
|
|
87
|
+
factory.createPropertyAssignment(factory.createIdentifier("data"), transferObjectToObjectLiteral(data))
|
|
88
|
+
], true);
|
|
89
|
+
}), true))], ts.NodeFlags.Const)));
|
|
90
|
+
/* if (this.dependencies.length > 0) {
|
|
91
|
+
statements.push(
|
|
92
|
+
factory.createExportAssignment(
|
|
93
|
+
undefined,
|
|
94
|
+
undefined,
|
|
95
|
+
factory.createCallExpression(
|
|
96
|
+
factory.createPropertyAccessExpression(
|
|
97
|
+
factory.createIdentifier("i18ns"),
|
|
98
|
+
factory.createIdentifier("concat")
|
|
99
|
+
),
|
|
100
|
+
undefined,
|
|
101
|
+
this.dependencies.map(
|
|
102
|
+
(ele, idx) => factory.createIdentifier(`i18ns${idx}`)
|
|
103
|
+
)
|
|
104
|
+
)
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
else { */
|
|
109
|
+
statements.push(factory.createExportAssignment(undefined, undefined, factory.createIdentifier("i18ns")));
|
|
110
|
+
/* } */
|
|
111
|
+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
112
|
+
const result = printer.printList(ts.ListFormat.SourceFileStatements, factory.createNodeArray(statements), ts.createSourceFile("someFileName.ts", "", ts.ScriptTarget.Latest, false, ts.ScriptKind.TS));
|
|
113
|
+
const filename = (0, path_1.join)(this.pwd, 'src', 'data', 'i18n.ts');
|
|
114
|
+
const result2 = (0, string_1.unescapeUnicode)(`// 本文件为自动编译产生,请勿直接修改\n\n${result}`);
|
|
115
|
+
fs_1.default.writeFileSync(filename, result2, { flag: 'w' });
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* 这里不能直接用require, webpack貌似有缓存
|
|
119
|
+
* @param filepath
|
|
120
|
+
*/
|
|
121
|
+
readLocaleFileContent(filepath) {
|
|
122
|
+
(0, assert_1.default)(filepath.endsWith('.json'));
|
|
123
|
+
const content = fs_1.default.readFileSync(filepath, {
|
|
124
|
+
encoding: 'utf-8',
|
|
125
|
+
});
|
|
126
|
+
return JSON.parse(content);
|
|
127
|
+
}
|
|
128
|
+
parseFile(module, namespace, position, filename, filepath, watch) {
|
|
129
|
+
const language = (filename.split('.')[0]).replace('_', '-'); // 历史原因,会命名成zh_CN.json
|
|
130
|
+
const data = this.readLocaleFileContent(filepath);
|
|
131
|
+
const ns = module ? `${module}-${namespace}` : (0, string_1.firstLetterLowerCase)(namespace);
|
|
132
|
+
this.locales[ns] = [module, position.replace(/\\/g, '/'), language, data];
|
|
133
|
+
if (watch) {
|
|
134
|
+
fs_1.default.watch(filepath, () => {
|
|
135
|
+
const data = this.readLocaleFileContent(filepath);
|
|
136
|
+
this.locales[ns] = [module, position.replace(/\\/g, '/'), language, data];
|
|
137
|
+
this.outputDataFile();
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
traverse(module, nsPrefix, position, dirPath, inLocale, localeFolderName, watch) {
|
|
142
|
+
const files = fs_1.default.readdirSync(dirPath);
|
|
143
|
+
files.forEach((file) => {
|
|
144
|
+
const filepath = (0, path_1.join)(dirPath, file);
|
|
145
|
+
const stat = fs_1.default.statSync(filepath);
|
|
146
|
+
if (stat.isFile() && inLocale && file.endsWith('.json')) {
|
|
147
|
+
this.parseFile(module, nsPrefix, position, file, filepath, watch);
|
|
148
|
+
}
|
|
149
|
+
else if (stat.isDirectory() && !inLocale) {
|
|
150
|
+
const nsPrefix2 = nsPrefix ? `${nsPrefix}-${file}` : file;
|
|
151
|
+
const isLocaleFolder = file === localeFolderName;
|
|
152
|
+
this.traverse(module, isLocaleFolder ? nsPrefix : nsPrefix2, isLocaleFolder ? position : (0, path_1.join)(position, file), (0, path_1.join)(dirPath, file), isLocaleFolder, localeFolderName, watch);
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
buildProject(root, src, watch) {
|
|
157
|
+
const packageJson = (0, path_1.join)(root, 'package.json');
|
|
158
|
+
const { name } = require(packageJson);
|
|
159
|
+
const pagePath = (0, path_1.join)(src ? 'src' : 'es', 'pages');
|
|
160
|
+
const pageAbsolutePath = (0, path_1.join)(root, pagePath); //编译i18时font中的componentPath缺少根目录导致编译不出
|
|
161
|
+
if (fs_1.default.existsSync(pageAbsolutePath)) {
|
|
162
|
+
this.traverse(name, 'p', pagePath, pageAbsolutePath, false, 'locales', watch);
|
|
163
|
+
}
|
|
164
|
+
const componentPath = (0, path_1.join)(src ? 'src' : 'es', 'components');
|
|
165
|
+
const componentAbsolutePath = (0, path_1.join)(root, componentPath);
|
|
166
|
+
if (fs_1.default.existsSync(componentAbsolutePath)) {
|
|
167
|
+
this.traverse(name, 'c', componentPath, componentAbsolutePath, false, 'locales', watch);
|
|
168
|
+
}
|
|
169
|
+
const localePath = (0, path_1.join)(root, src ? 'src' : 'es', 'locales');
|
|
170
|
+
if (fs_1.default.existsSync(localePath)) {
|
|
171
|
+
const files = fs_1.default.readdirSync(localePath);
|
|
172
|
+
files.forEach((file) => {
|
|
173
|
+
const filepath = (0, path_1.join)(localePath, file);
|
|
174
|
+
const stat = fs_1.default.statSync(filepath);
|
|
175
|
+
if (stat.isDirectory()) {
|
|
176
|
+
this.traverse(name, `l-${file}`, (0, path_1.join)('locales', file), (0, path_1.join)(localePath, file), true, file, watch);
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (!this.asLib && src) {
|
|
181
|
+
// 不是lib的话将oak-app-domain中的对象的locale也收集起来
|
|
182
|
+
const domainPath = (0, path_1.join)(root, 'src', 'oak-app-domain');
|
|
183
|
+
if (fs_1.default.existsSync(domainPath)) {
|
|
184
|
+
this.traverse('', '', 'oak-app-domain', domainPath, false, 'locales', watch);
|
|
185
|
+
}
|
|
186
|
+
// 还有web和wechatMp的目录
|
|
187
|
+
const webSrcPath = (0, path_1.join)('web', 'src');
|
|
188
|
+
this.traverse(name, 'w', webSrcPath, (0, path_1.join)(root, webSrcPath), false, 'locales', watch);
|
|
189
|
+
// 小程序可能有多于一个,按规范用wechatMp, wechatMp2这样命名
|
|
190
|
+
const wechatMpSrcPath = (0, path_1.join)('wechatMp', 'src');
|
|
191
|
+
this.traverse(name, 'wmp', wechatMpSrcPath, (0, path_1.join)(root, webSrcPath), false, 'locales', watch);
|
|
192
|
+
let iter = 1;
|
|
193
|
+
while (true) {
|
|
194
|
+
const mpSrcPath = `${wechatMpSrcPath}${iter}`;
|
|
195
|
+
if (fs_1.default.existsSync((0, path_1.join)(root, mpSrcPath))) {
|
|
196
|
+
this.traverse(name, `wmp${iter}`, mpSrcPath, (0, path_1.join)(root, mpSrcPath), false, 'locales', watch);
|
|
197
|
+
iter++;
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
build(watch) {
|
|
206
|
+
this.buildProject(this.pwd, true, watch);
|
|
207
|
+
if (!this.asLib) {
|
|
208
|
+
// 如果不是lib,把front里的数据也处理掉
|
|
209
|
+
const fbPath = (0, path_1.join)(this.pwd, 'node_modules', 'oak-frontend-base');
|
|
210
|
+
this.buildProject(fbPath, false, watch);
|
|
211
|
+
}
|
|
212
|
+
this.outputDataFile();
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
exports.default = LocaleBuilder;
|