@fastcar/cli 0.1.1 → 0.1.2

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/src/reverse.js CHANGED
@@ -1,6 +1,8 @@
1
1
  const path = require("path");
2
2
  const fs = require("fs");
3
3
  const process = require("process");
4
+ const yaml = require("yaml");
5
+ const inquirer = require("inquirer");
4
6
 
5
7
  // 默认配置文件模板
6
8
  const defaultConfig = {
@@ -25,18 +27,70 @@ const defaultConfig = {
25
27
  ignoreCamelcase: false,
26
28
  };
27
29
 
30
+ // 支持的配置文件名
31
+ const CONFIG_FILES = {
32
+ json: "reverse.config.json",
33
+ yml: "reverse.config.yml",
34
+ };
35
+
36
+ // 查找已存在的配置文件
37
+ function findExistingConfig(cwd) {
38
+ for (const ext of ["yml", "json"]) {
39
+ const configPath = path.join(cwd, CONFIG_FILES[ext]);
40
+ if (fs.existsSync(configPath)) {
41
+ return { ext, configPath };
42
+ }
43
+ }
44
+ return null;
45
+ }
46
+
47
+ // 读取配置文件
48
+ function readConfig(configPath, ext) {
49
+ const content = fs.readFileSync(configPath, "utf-8");
50
+ if (ext === "yml") {
51
+ return yaml.parse(content);
52
+ }
53
+ return JSON.parse(content);
54
+ }
55
+
56
+ // 写入配置文件
57
+ function writeConfig(configPath, ext, config) {
58
+ if (ext === "yml") {
59
+ fs.writeFileSync(configPath, yaml.stringify(config));
60
+ } else {
61
+ fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
62
+ }
63
+ }
64
+
28
65
  // 生成默认配置文件
29
- function initReverseConfig() {
66
+ async function initReverseConfig() {
30
67
  const cwd = process.cwd();
31
- const configPath = path.join(cwd, "reverse.config.json");
32
68
 
33
- // 检查是否已存在
34
- if (fs.existsSync(configPath)) {
35
- console.log("⚠️ reverse.config.json 配置文件已存在");
69
+ // 检查是否已存在任何格式的配置文件
70
+ const existing = findExistingConfig(cwd);
71
+ if (existing) {
72
+ console.log(`⚠️ ${path.basename(existing.configPath)} 配置文件已存在`);
36
73
  console.log(" 如需重新生成,请先删除现有文件");
37
74
  return;
38
75
  }
39
76
 
77
+ // 交互式选择配置格式
78
+ const answer = await inquirer.prompt([
79
+ {
80
+ type: "list",
81
+ name: "format",
82
+ message: "选择配置文件格式:",
83
+ choices: [
84
+ { name: "YAML (reverse.config.yml)", value: "yml" },
85
+ { name: "JSON (reverse.config.json)", value: "json" },
86
+ ],
87
+ default: "yml",
88
+ },
89
+ ]);
90
+
91
+ const ext = answer.format;
92
+ const configPath = path.join(cwd, CONFIG_FILES[ext]);
93
+
40
94
  // 填充默认路径
41
95
  const config = {
42
96
  ...defaultConfig,
@@ -45,8 +99,8 @@ function initReverseConfig() {
45
99
  };
46
100
 
47
101
  try {
48
- fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
49
- console.log("✅ 已生成 reverse.config.json 配置文件");
102
+ writeConfig(configPath, ext, config);
103
+ console.log(`✅ 已生成 ${CONFIG_FILES[ext]} 配置文件`);
50
104
  console.log("📁 文件路径:", configPath);
51
105
  console.log("\n💡 请根据需要修改以下配置:");
52
106
  console.log(" • tables: 要逆向生成的表名数组");
@@ -72,11 +126,31 @@ async function reverseGenerate(args = []) {
72
126
  }
73
127
 
74
128
  // 读取配置文件
75
- const configPath = path.join(cwd, "reverse.config.json");
76
- if (!fs.existsSync(configPath)) {
77
- console.log("❌ 未找到 reverse.config.json 配置文件");
129
+ const existing = findExistingConfig(cwd);
130
+ if (!existing) {
131
+ console.log("❌ 未找到 reverse.config.yml 或 reverse.config.json 配置文件");
78
132
  console.log(" 请在项目根目录创建该文件,格式如下:");
79
133
  console.log(`
134
+ YAML 格式 (reverse.config.yml):
135
+ tables: [test]
136
+ modelDir: ${cwd.replace(/\\/g, "/")}/src/models
137
+ mapperDir: ${cwd.replace(/\\/g, "/")}/src/mappers
138
+ dbConfig:
139
+ host: localhost
140
+ port: 3306
141
+ user: root
142
+ password: password
143
+ database: test_db
144
+ style:
145
+ tabWidth: 4
146
+ printWidth: 200
147
+ trailingComma: es5
148
+ useTabs: true
149
+ parser: typescript
150
+ endOfLine: crlf
151
+ ignoreCamelcase: false
152
+
153
+ JSON 格式 (reverse.config.json):
80
154
  {
81
155
  "tables": ["test"],
82
156
  "modelDir": "${cwd.replace(/\\/g, "/")}/src/models",
@@ -104,9 +178,9 @@ async function reverseGenerate(args = []) {
104
178
 
105
179
  let config;
106
180
  try {
107
- config = JSON.parse(fs.readFileSync(configPath, "utf-8"));
181
+ config = readConfig(existing.configPath, existing.ext);
108
182
  } catch (error) {
109
- console.log("❌ 配置文件解析失败:", error.message);
183
+ console.log(`❌ 配置文件 ${path.basename(existing.configPath)} 解析失败:`, error.message);
110
184
  return;
111
185
  }
112
186