@domain.js/main 0.1.6 → 0.1.10
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/.husky/pre-commit +2 -1
- package/dist/cli/index.js +43 -68
- package/dist/deps/cron/index.d.ts +1 -1
- package/dist/deps/cron/index.js +3 -3
- package/dist/deps/request/index.js +1 -1
- package/dist/deps/typeorm/index.d.ts +14 -0
- package/dist/deps/typeorm/index.js +16 -0
- package/dist/http/defines.d.ts +2 -0
- package/dist/http/router.d.ts +8 -4
- package/dist/http/utils.js +1 -0
- package/dist/utils/index.js +1 -1
- package/package.json +1 -1
package/.husky/pre-commit
CHANGED
|
@@ -4,4 +4,5 @@ if [ ! -f "$(dirname "$0")/_/husky.sh" ]; then
|
|
|
4
4
|
fi
|
|
5
5
|
. "$(dirname "$0")/_/husky.sh"
|
|
6
6
|
|
|
7
|
-
ts-node src/cli/index.ts loadDeps ./src/deps
|
|
7
|
+
ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
|
|
8
|
+
npm tst
|
package/dist/cli/index.js
CHANGED
|
@@ -31,10 +31,11 @@ const codeStyleFormat = (targetFile) => new Promise((resolve, reject) => {
|
|
|
31
31
|
});
|
|
32
32
|
});
|
|
33
33
|
const makeDefineFile = async (modules, targetFile, isTS) => {
|
|
34
|
+
const dirname = path.dirname(targetFile);
|
|
34
35
|
const content = ["// domain-cli 自动生成"];
|
|
35
36
|
const _exports = [];
|
|
36
37
|
for (let i = 0; i < modules.length; i += 1) {
|
|
37
|
-
const name = modules[i];
|
|
38
|
+
const name = path.relative(dirname, modules[i]);
|
|
38
39
|
const variable = filePath2Var(name);
|
|
39
40
|
if (isTS) {
|
|
40
41
|
content.push(`import * as ${variable} from "./${name}"`);
|
|
@@ -76,7 +77,7 @@ const checkHookExport = (_dir) => {
|
|
|
76
77
|
}
|
|
77
78
|
}
|
|
78
79
|
};
|
|
79
|
-
const loadDeps = async (rootDir
|
|
80
|
+
const loadDeps = async (rootDir, ext = "js") => {
|
|
80
81
|
const isTS = ext === "ts";
|
|
81
82
|
const modules = [];
|
|
82
83
|
const dir = path.resolve(rootDir, "./");
|
|
@@ -90,7 +91,7 @@ const loadDeps = async (rootDir = process.cwd(), ext = "js") => {
|
|
|
90
91
|
if (!stat.isDirectory())
|
|
91
92
|
continue;
|
|
92
93
|
checkHookExport(_dir);
|
|
93
|
-
modules.push(x);
|
|
94
|
+
modules.push(path.join(dir, x));
|
|
94
95
|
}
|
|
95
96
|
// 按字典排序,后续有变动的时候不容易冲突
|
|
96
97
|
const targetFile = path.resolve(rootDir, `./defines.${ext}`);
|
|
@@ -106,7 +107,7 @@ const checkService = (_dir) => {
|
|
|
106
107
|
const loadServices = async (rootDir = process.cwd(), ext = "js") => {
|
|
107
108
|
const isTS = ext === "ts";
|
|
108
109
|
const modules = [];
|
|
109
|
-
const dir = path.resolve(rootDir, "domain/services/");
|
|
110
|
+
const dir = path.resolve(rootDir, "src/domain/services/");
|
|
110
111
|
for (const x of fs.readdirSync(dir)) {
|
|
111
112
|
// 忽略隐藏目录, 忽略私有目录
|
|
112
113
|
if (x[0] === "." || x[0] === "_")
|
|
@@ -117,81 +118,55 @@ const loadServices = async (rootDir = process.cwd(), ext = "js") => {
|
|
|
117
118
|
if (!stat.isDirectory())
|
|
118
119
|
continue;
|
|
119
120
|
checkService(_dir);
|
|
120
|
-
modules.push(x);
|
|
121
|
+
modules.push(path.join(dir, x));
|
|
121
122
|
}
|
|
122
123
|
// 按字典排序,后续有变动的时候不容易冲突
|
|
123
|
-
const targetFile = path.resolve(rootDir, `domain/services/defines.${ext}`);
|
|
124
|
+
const targetFile = path.resolve(rootDir, `src/domain/services/defines.${ext}`);
|
|
124
125
|
await makeDefineFile(modules.sort(), targetFile, isTS);
|
|
125
126
|
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
127
|
+
/**
|
|
128
|
+
* 尝试读取目录下的文件
|
|
129
|
+
* @param dir 目录路径
|
|
130
|
+
* @param list 读取到schema后压入改列表
|
|
131
|
+
*/
|
|
132
|
+
const tryReadSchemas = (dir, list, isTS = false) => {
|
|
133
|
+
if (!fs.existsSync(dir))
|
|
134
|
+
return;
|
|
135
|
+
const stat = fs.statSync(dir);
|
|
136
|
+
if (stat.isFile())
|
|
137
|
+
return;
|
|
129
138
|
for (const x of fs.readdirSync(dir)) {
|
|
130
|
-
|
|
131
|
-
if (x[0] === ".")
|
|
139
|
+
if (x.startsWith("."))
|
|
132
140
|
continue;
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
const { name, ext } = path.parse(x);
|
|
136
|
-
const relativeFilePath = `./${path.join(parent, name)}`;
|
|
137
|
-
const moduleName = file2Module(name);
|
|
138
|
-
// 如果是文件则记录
|
|
139
|
-
if (stat.isFile()) {
|
|
140
|
-
if (ext === ".ts") {
|
|
141
|
-
const JSFile = path.resolve(dir, `${name}.js`);
|
|
142
|
-
// 对应的js文件存在,则ts文件忽略
|
|
143
|
-
if (fs.existsSync(JSFile))
|
|
144
|
-
continue;
|
|
145
|
-
// 对应的js文件不存在,抛出异常提示用户要先编译
|
|
146
|
-
throw Error(`请先编译ts文件: ${file}`);
|
|
147
|
-
}
|
|
148
|
-
files.push(relativeFilePath);
|
|
149
|
-
paths[moduleName] = relativeFilePath;
|
|
141
|
+
const ext = path.extname(x);
|
|
142
|
+
if (isTS && ext !== ".ts")
|
|
150
143
|
continue;
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
144
|
+
if (!isTS && ext !== ".js")
|
|
145
|
+
continue;
|
|
146
|
+
list.push(path.join(dir, path.basename(x, isTS ? ".ts" : ".js")));
|
|
155
147
|
}
|
|
156
|
-
return paths;
|
|
157
148
|
};
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
else {
|
|
175
|
-
content.push(`const ${variable} = require("${_path}")`);
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
// 处理导出
|
|
179
|
-
content.push("\n");
|
|
180
|
-
let _exports = JSON.stringify(paths, null, 2);
|
|
181
|
-
for (let i = 0; i < files.length; i += 1) {
|
|
182
|
-
_exports = _exports.replace(`"${files[i]}"`, filePath2Var(files[i]));
|
|
183
|
-
}
|
|
184
|
-
if (isTS) {
|
|
185
|
-
content.push(`export = ${_exports}`);
|
|
186
|
-
}
|
|
187
|
-
else {
|
|
188
|
-
content.push(`module.exports = ${_exports}`);
|
|
149
|
+
const loadSchemas = async (rootDir = process.cwd(), ext = "js") => {
|
|
150
|
+
const isTS = ext === "ts";
|
|
151
|
+
const modules = [];
|
|
152
|
+
const dir = path.resolve(rootDir, "src/domain/services/");
|
|
153
|
+
for (const x of fs.readdirSync(dir)) {
|
|
154
|
+
// 忽略隐藏目录, 忽略私有目录
|
|
155
|
+
if (x[0] === "." || x[0] === "_")
|
|
156
|
+
continue;
|
|
157
|
+
const _dir = path.resolve(dir, x);
|
|
158
|
+
const stat = fs.statSync(_dir);
|
|
159
|
+
// 非目录忽略,模块必须是目录
|
|
160
|
+
if (!stat.isDirectory())
|
|
161
|
+
continue;
|
|
162
|
+
// 尝试读取子目录里的 schemas 目录
|
|
163
|
+
tryReadSchemas(path.join(_dir, "schemas"), modules, isTS);
|
|
189
164
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
165
|
+
// 按字典排序,后续有变动的时候不容易冲突
|
|
166
|
+
const targetFile = path.resolve(rootDir, `src/domain/services/schemas.${ext}`);
|
|
167
|
+
await makeDefineFile(modules.sort(), targetFile, isTS);
|
|
193
168
|
};
|
|
194
|
-
const actions = { loadDeps, loadServices,
|
|
169
|
+
const actions = { loadDeps, loadServices, loadSchemas };
|
|
195
170
|
const main = async (command) => {
|
|
196
171
|
const action = actions[command];
|
|
197
172
|
if (!action) {
|
|
@@ -16,7 +16,7 @@ interface callbackArg {
|
|
|
16
16
|
interface Deps {
|
|
17
17
|
cronParser: typeof cronParser;
|
|
18
18
|
humanInterval: typeof human;
|
|
19
|
-
|
|
19
|
+
myCia: {
|
|
20
20
|
regist: (name: string, validator: any, waiters: waiter[]) => void;
|
|
21
21
|
submit: (name: string, times: number, callback: (arg: callbackArg) => void) => void;
|
|
22
22
|
};
|
package/dist/deps/cron/index.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.Deps = exports.Main = void 0;
|
|
|
4
4
|
function Main(cnf, deps) {
|
|
5
5
|
const { cron = {} } = cnf;
|
|
6
6
|
const ciaTaskType = "cronJob";
|
|
7
|
-
const {
|
|
7
|
+
const { myCia, humanInterval: human, cronParser: parser } = deps;
|
|
8
8
|
const { tz = "Asia/Shanghai" } = cron;
|
|
9
9
|
// 注册信息
|
|
10
10
|
const registed = {};
|
|
@@ -40,7 +40,7 @@ function Main(cnf, deps) {
|
|
|
40
40
|
setTimeout(() => {
|
|
41
41
|
opt.times += 1;
|
|
42
42
|
opt.triggeredAt = Date.now();
|
|
43
|
-
|
|
43
|
+
myCia.submit(`Cron::${name}`, opt.times, ({ cronJob: [err, , totalMS] }) => {
|
|
44
44
|
if (err) {
|
|
45
45
|
opt.failds += 1;
|
|
46
46
|
}
|
|
@@ -72,7 +72,7 @@ function Main(cnf, deps) {
|
|
|
72
72
|
};
|
|
73
73
|
// 注册到cia上, 为了借助cia的能力自动下发任务
|
|
74
74
|
// 增加 Cron:: 前缀是为了避免和其他任务名称冲突
|
|
75
|
-
|
|
75
|
+
myCia.regist(`Cron::${name}`, null, [{ type: ciaTaskType }]);
|
|
76
76
|
};
|
|
77
77
|
const start = () => {
|
|
78
78
|
if (startedAt)
|
|
@@ -19,7 +19,7 @@ function Main(cnf, deps) {
|
|
|
19
19
|
const d = r.data;
|
|
20
20
|
if (typeof d === "string")
|
|
21
21
|
return [r.status, d];
|
|
22
|
-
return [d.code || r.status, d.message ||
|
|
22
|
+
return [d.code || r.status, d.message || JSON.stringify(d)];
|
|
23
23
|
})().join("\t");
|
|
24
24
|
if (!cnf.axios)
|
|
25
25
|
cnf.axios = {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as TypeORM from "typeorm";
|
|
2
|
+
interface Cnf {
|
|
3
|
+
typeorm: {
|
|
4
|
+
[propName: string]: Parameters<typeof TypeORM.createConnection>[0];
|
|
5
|
+
};
|
|
6
|
+
}
|
|
7
|
+
interface Deps {
|
|
8
|
+
TypeORM: typeof TypeORM;
|
|
9
|
+
}
|
|
10
|
+
export declare function Main(cnf: Cnf, deps: Deps): Promise<{
|
|
11
|
+
[propName: string]: TypeORM.Connection;
|
|
12
|
+
}>;
|
|
13
|
+
export declare const Deps: string[];
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Deps = exports.Main = void 0;
|
|
4
|
+
async function Main(cnf, deps) {
|
|
5
|
+
// 这里之所以要注入 Sequelize 是为了保证项目自身可以灵活选择自己的 Sequelize 版本, 这样改公共模块就会更加稳定, 避免频繁升级
|
|
6
|
+
const { typeorm: dbs } = cnf;
|
|
7
|
+
const { TypeORM } = deps;
|
|
8
|
+
const links = {};
|
|
9
|
+
for await (const k of Object.keys(dbs)) {
|
|
10
|
+
const db = dbs[k];
|
|
11
|
+
links[k] = await TypeORM.createConnection(db);
|
|
12
|
+
}
|
|
13
|
+
return links;
|
|
14
|
+
}
|
|
15
|
+
exports.Main = Main;
|
|
16
|
+
exports.Deps = ["TypeORM"];
|
package/dist/http/defines.d.ts
CHANGED
package/dist/http/router.d.ts
CHANGED
|
@@ -11,11 +11,15 @@ interface Deps {
|
|
|
11
11
|
apisRoute?: string;
|
|
12
12
|
swagger?: [any, any];
|
|
13
13
|
}
|
|
14
|
+
/** 对 params 的处理函数 */
|
|
15
|
+
declare type Handler = (params: any) => void;
|
|
16
|
+
/** 对执行结构的处理 */
|
|
17
|
+
declare type ResHandler = (results: any, res: restify.Response) => void;
|
|
14
18
|
export declare function Router(deps: Deps): {
|
|
15
|
-
get: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?:
|
|
16
|
-
post: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?:
|
|
17
|
-
put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?:
|
|
18
|
-
del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?:
|
|
19
|
+
get: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
20
|
+
post: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
21
|
+
put: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
22
|
+
del: (routePath: string, ctlAct: string, code?: number, isList?: boolean, handler?: Handler | undefined, resHandler?: ResHandler | undefined) => void;
|
|
19
23
|
} & {
|
|
20
24
|
collection: (res: string, _routePath?: string | undefined, controller?: string | undefined) => void;
|
|
21
25
|
model: (res: string, routePath?: string) => void;
|
package/dist/http/utils.js
CHANGED
package/dist/utils/index.js
CHANGED
|
@@ -17,7 +17,7 @@ const md5 = (str) => {
|
|
|
17
17
|
return hash.update(str.toString()).digest().toString("hex");
|
|
18
18
|
};
|
|
19
19
|
exports.md5 = md5;
|
|
20
|
-
function randStr(len, type) {
|
|
20
|
+
function randStr(len, type = "normal") {
|
|
21
21
|
const dict = type === "strong" || type === "normal" ? RAND_STR_DICT[type] : type;
|
|
22
22
|
const { length } = dict;
|
|
23
23
|
/** 随机字符串的长度不能等于 0 或者负数 */
|