nsgm-cli 2.1.31 → 2.1.33

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.
@@ -35,7 +35,7 @@ interface MenuItem {
35
35
  // 安全获取 prefix 的辅助函数
36
36
  const getPrefix = () => {
37
37
  try {
38
- if (typeof process !== "undefined" && process.env && process.env.NEXT_PUBLIC_PREFIX) {
38
+ if (typeof process !== "undefined" && process.env?.NEXT_PUBLIC_PREFIX) {
39
39
  return process.env.NEXT_PUBLIC_PREFIX;
40
40
  }
41
41
  } catch {
@@ -4,7 +4,7 @@ export const getLocalEnv = () => {
4
4
  // 安全地访问 process.env,避免在浏览器中直接访问 process 对象
5
5
  let env = "uat";
6
6
  try {
7
- if (typeof process !== "undefined" && process.env && process.env.NEXT_PUBLIC_ENV) {
7
+ if (typeof process !== "undefined" && process.env?.NEXT_PUBLIC_ENV) {
8
8
  env = process.env.NEXT_PUBLIC_ENV;
9
9
  }
10
10
  } catch {
@@ -5,8 +5,121 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.deleteConfigCommand = void 0;
7
7
  const utils_1 = require("../utils");
8
- const generate_1 = require("../../generate");
9
8
  const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const shelljs_1 = __importDefault(require("shelljs"));
11
+ const utils_2 = require("../../utils");
12
+ const constants_1 = require("../../constants");
13
+ /**
14
+ * 生成删除路径
15
+ */
16
+ const generateDeletePaths = (controller, dictionary) => {
17
+ const basePath = dictionary || process.cwd();
18
+ return {
19
+ destPagesController: path_1.default.join(basePath, "pages", controller),
20
+ destClientReduxController: path_1.default.join(basePath, "client", "redux", controller),
21
+ destClientServiceController: path_1.default.join(basePath, "client", "service", controller),
22
+ destClientStyledController: path_1.default.join(basePath, "client", "styled", controller),
23
+ destServerModulesController: path_1.default.join(basePath, "server", "modules", controller),
24
+ destServerApisController: path_1.default.join(basePath, "server", "apis", `${controller}.js`),
25
+ destServerSqlController: path_1.default.join(basePath, "server", "sql", `${controller}.sql`),
26
+ destServerDataLoader: path_1.default.join(basePath, "server", "dataloaders", `${controller}-dataloader.ts`),
27
+ destClientReduxReducersAllPath: path_1.default.join(basePath, "client", "redux", "reducers.ts"),
28
+ destServerRestPath: path_1.default.join(basePath, "server", "rest.js"),
29
+ destClientUtilsMenuPath: path_1.default.join(basePath, "client", "utils", "menu.tsx"),
30
+ destI18nZhCN: path_1.default.join(basePath, "public", "locales", "zh-CN", `${controller}.json`),
31
+ destI18nEnUS: path_1.default.join(basePath, "public", "locales", "en-US", `${controller}.json`),
32
+ destI18nJaJP: path_1.default.join(basePath, "public", "locales", "ja-JP", `${controller}.json`),
33
+ };
34
+ };
35
+ /**
36
+ * 删除文件和目录
37
+ */
38
+ const deleteModuleFiles = (paths) => {
39
+ const directoriesToDelete = [
40
+ paths.destPagesController,
41
+ paths.destClientReduxController,
42
+ paths.destClientServiceController,
43
+ paths.destClientStyledController,
44
+ paths.destServerModulesController,
45
+ ];
46
+ const filesToDelete = [
47
+ paths.destServerApisController,
48
+ paths.destServerSqlController,
49
+ paths.destServerDataLoader,
50
+ paths.destI18nZhCN,
51
+ paths.destI18nEnUS,
52
+ paths.destI18nJaJP,
53
+ ];
54
+ directoriesToDelete.forEach((dir) => (0, utils_2.rmdirSync)(dir));
55
+ filesToDelete.forEach((file) => (0, utils_2.rmFileSync)(file));
56
+ console.log(`✅ 已删除 ${directoriesToDelete.length} 个目录和 ${filesToDelete.length} 个文件`);
57
+ };
58
+ /**
59
+ * 清理 reducers 配置
60
+ */
61
+ const cleanupReducers = (controller, reducersPath) => {
62
+ // 删除 import 语句
63
+ shelljs_1.default.sed("-i", new RegExp(`^.*import.*from.*['"].*\\/${controller}\\/.*['"].*$`, "gm"), "", reducersPath);
64
+ // 删除 export 对象中的属性行
65
+ shelljs_1.default.sed("-i", new RegExp(`^\\s*${controller}\\w*:\\s*${controller}\\w*Reducer,?\\s*$`, "gm"), "", reducersPath);
66
+ // 修复连续逗号
67
+ shelljs_1.default.sed("-i", /,,+/g, ",", reducersPath);
68
+ // 修复对象末尾的逗号
69
+ shelljs_1.default.sed("-i", /,(\s*\n\s*\})/, "$1", reducersPath);
70
+ // 移除空对象中的逗号
71
+ shelljs_1.default.sed("-i", /\{\s*,\s*\}/, "{}", reducersPath);
72
+ // 标准化空行
73
+ shelljs_1.default.sed("-i", /\n\s*\n\s*\n/g, "\n\n", reducersPath);
74
+ console.log(`✅ 已清理 reducers 配置: ${controller}`);
75
+ };
76
+ /**
77
+ * 清理菜单配置
78
+ */
79
+ const cleanupMenu = (controller, menuPath) => {
80
+ // 删除所有匹配的菜单项(匹配多行结构)
81
+ shelljs_1.default.sed("-i", new RegExp(`,?\\s*\\{\\s*//\\s*${controller}_\\w+_start[\\s\\S]*?//\\s*${controller}_\\w+_end[\\s\\S]*?\\n\\s*\\}\\s*,?`, "gm"), "", menuPath);
82
+ // 修复连续逗号
83
+ shelljs_1.default.sed("-i", /,,+/g, ",", menuPath);
84
+ // 修复对象前多余的逗号
85
+ shelljs_1.default.sed("-i", /\n\s*,\s*\{/gm, "\n {", menuPath);
86
+ // 修复数组中缺失的逗号
87
+ shelljs_1.default.sed("-i", /(\})\s*(\{)/gm, "$1,\n $2", menuPath);
88
+ // 清理缩进问题
89
+ shelljs_1.default.sed("-i", /^[ ]{0,2}\/\*\{/gm, " /*{", menuPath);
90
+ shelljs_1.default.sed("-i", /^[ ]{0,4}key:/gm, " key:", menuPath);
91
+ shelljs_1.default.sed("-i", /^[ ]{0,4}text:/gm, " text:", menuPath);
92
+ shelljs_1.default.sed("-i", /^[ ]{0,4}url:/gm, " url:", menuPath);
93
+ shelljs_1.default.sed("-i", /^[ ]{0,4}icon:/gm, " icon:", menuPath);
94
+ shelljs_1.default.sed("-i", /^[ ]{0,4}subMenus:/gm, " subMenus:", menuPath);
95
+ shelljs_1.default.sed("-i", /^[ ]{0,2}\}\*\//gm, " }*/", menuPath);
96
+ console.log(`✅ 已清理菜单配置: ${controller}`);
97
+ };
98
+ /**
99
+ * 清理 REST API 配置
100
+ */
101
+ const cleanupRestApi = (controller, restPath) => {
102
+ // 删除 require 语句
103
+ shelljs_1.default.sed("-i", new RegExp(`^const\\s+${controller}\\s*=\\s*require.*${controller}['"].*$`, "gm"), "", restPath);
104
+ // 删除 router.use 语句
105
+ shelljs_1.default.sed("-i", new RegExp(`^router\\.use\\(['"]\\/${controller}['"]\\s*,\\s*${controller}\\)\\s*$`, "gm"), "", restPath);
106
+ console.log(`✅ 已清理 REST API 配置: ${controller}`);
107
+ };
108
+ /**
109
+ * 删除数据库表
110
+ */
111
+ const dropDatabaseTable = (controller) => {
112
+ try {
113
+ // 创建临时的 DROP TABLE SQL
114
+ const dropSql = `DROP TABLE IF EXISTS \`${controller}\`;`;
115
+ const mysqlCommand = `mysql -u${constants_1.mysqlUser} -p${constants_1.mysqlPassword} -h${constants_1.mysqlHost} -P${constants_1.mysqlPort} -e "${dropSql}"`;
116
+ shelljs_1.default.exec(mysqlCommand);
117
+ utils_1.Console.info(`已删除数据库表: ${controller}`);
118
+ }
119
+ catch (error) {
120
+ utils_1.Console.error(`删除数据库表失败: ${error}`);
121
+ }
122
+ };
10
123
  /**
11
124
  * 从配置文件删除命令
12
125
  */
@@ -144,18 +257,30 @@ exports.deleteConfigCommand = {
144
257
  for (const module of targetModules) {
145
258
  utils_1.Console.separator();
146
259
  utils_1.Console.highlight(`🗑️ 删除模块 ${successCount + failureCount + 1}/${targetModules.length}: ${module.controller}`);
260
+ utils_1.Console.newLine();
147
261
  try {
148
- const spinner = utils_1.Console.spinner("正在删除文件...", "red");
149
- spinner.start();
150
- // 调用核心删除函数
151
- (0, generate_1.deleteFiles)(module.controller, module.action || "manage", options.db === true, module.dictionary || ".");
152
- spinner.succeed("删除完成!");
153
- successCount++;
262
+ // 生成删除路径
263
+ const paths = generateDeletePaths(module.controller, module.dictionary);
264
+ // 1. 删除文件和目录
265
+ utils_1.Console.info("📁 正在删除文件和目录...");
266
+ deleteModuleFiles(paths);
267
+ // 2. 清理配置文件
268
+ utils_1.Console.info("🔧 正在清理配置文件...");
269
+ cleanupReducers(module.controller, paths.destClientReduxReducersAllPath);
270
+ cleanupRestApi(module.controller, paths.destServerRestPath);
271
+ cleanupMenu(module.controller, paths.destClientUtilsMenuPath);
272
+ // 3. 删除数据库表(如果指定)
273
+ if (options.db) {
274
+ utils_1.Console.info("💾 正在删除数据库表...");
275
+ dropDatabaseTable(module.controller);
276
+ }
277
+ utils_1.Console.success(`✅ ${module.controller} 删除完成!`);
154
278
  utils_1.Console.newLine();
279
+ successCount++;
155
280
  }
156
281
  catch (error) {
157
282
  const errorMessage = error instanceof Error ? error.message : String(error);
158
- utils_1.Console.error(`错误: ${errorMessage}`);
283
+ utils_1.Console.error(`❌ 错误: ${errorMessage}`);
159
284
  failures.push({ module: module.controller, error: errorMessage });
160
285
  failureCount++;
161
286
  utils_1.Console.newLine();
@@ -96,9 +96,11 @@ class SQLGenerator extends base_generator_1.BaseGenerator {
96
96
  const primaryKeyField = this.fields.find((f) => f.isPrimaryKey);
97
97
  const primaryKey = primaryKeyField ? ` PRIMARY KEY (\`${primaryKeyField.name}\`)` : "";
98
98
  const databaseName = this.getDatabaseName();
99
- return `use ${databaseName};
99
+ return `CREATE DATABASE IF NOT EXISTS ${databaseName} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
100
100
 
101
- CREATE TABLE \`${this.controller}\` (
101
+ use ${databaseName};
102
+
103
+ CREATE TABLE IF NOT EXISTS \`${this.controller}\` (
102
104
  ${fieldDefinitions.join(",\n")},
103
105
  ${primaryKey}
104
106
  ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;`;