@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 CHANGED
@@ -4,4 +4,4 @@ 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/ ts && npm run lint-staged && npm run build
7
+ ts-node src/cli/index.ts loadDeps ./src/deps ts && npm run lint-staged && npm run build
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 = process.cwd(), ext = "js") => {
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
- const deepLoadDir = (root, parent, files = []) => {
127
- const paths = {};
128
- const dir = path.resolve(root, parent);
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 file = path.resolve(dir, x);
134
- const stat = fs.statSync(file);
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
- if (stat.isDirectory()) {
153
- paths[moduleName] = deepLoadDir(root, relativeFilePath, files);
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 deepLoadModule = async (rootDir, targetFile) => {
159
- const files = [];
160
- const paths = deepLoadDir(rootDir, "./", files);
161
- const { ext } = path.parse(targetFile);
162
- // 按字典排序,后续有变动的时候不容易冲突
163
- files.sort();
164
- const relative = path.relative(path.dirname(targetFile), rootDir);
165
- const isTS = ext === ".ts";
166
- const content = ["// domain-cli 自动生成"];
167
- for (let i = 0; i < files.length; i += 1) {
168
- const name = files[i];
169
- const _path = `./${path.join(relative, name)}`;
170
- const variable = filePath2Var(name);
171
- if (isTS) {
172
- content.push(`import * as ${variable} from "${_path}"`);
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
- fs.writeFileSync(targetFile, content.join("\n"));
191
- await codeStyleFormat(targetFile);
192
- console.log(`Completed: ${targetFile}`);
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, deepLoadModule };
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 || r.statusText];
22
+ return [d.code || r.status, d.message || JSON.stringify(d)];
23
23
  })().join("\t");
24
24
  if (!cnf.axios)
25
25
  cnf.axios = {};
@@ -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 或者负数 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@domain.js/main",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "description": "DDD framework",
5
5
  "main": "dist/index.js",
6
6
  "bin": {