@domain.js/main 0.1.6 → 0.1.7
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 +1 -1
- package/dist/cli/index.js +43 -68
- package/dist/deps/request/index.js +1 -1
- package/dist/utils/index.js +1 -1
- package/package.json +1 -1
package/.husky/pre-commit
CHANGED
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) {
|
|
@@ -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 = {};
|
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 或者负数 */
|