nsgm-cli 2.1.28 → 2.1.30

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/README.md CHANGED
@@ -146,6 +146,7 @@ Your application will be available at `http://localhost:3000` with:
146
146
  | `nsgm create` | Generate controller with CRUD | Interactive/CLI | `nsgm create user` |
147
147
  | `nsgm delete` | Remove controller and files | Interactive/CLI | `nsgm delete product` |
148
148
  | `nsgm create-config` | Batch create from config file | CLI | `nsgm create-config config/modules.json` |
149
+ | `nsgm delete-config` | Batch delete from config file | CLI | `nsgm delete-config config/modules.json` |
149
150
  | `nsgm dev` | Start development server | CLI | `nsgm dev` |
150
151
  | `nsgm build` | Build for production | CLI | `nsgm build` |
151
152
  | `nsgm start` | Start production server | CLI | `nsgm start` |
@@ -161,6 +162,12 @@ nsgm create-config config/modules.json # Create all modules from co
161
162
  nsgm create-config config/modules.json --module category # Create specific module
162
163
  nsgm create-config config/modules.json --dry-run # Preview without creating
163
164
 
165
+ # Batch module deletion
166
+ nsgm delete-config config/modules.json # Delete all modules from config
167
+ nsgm delete-config config/modules.json --module category # Delete specific module
168
+ nsgm delete-config config/modules.json --db # Delete modules + database tables
169
+ nsgm delete-config config/modules.json --dry-run # Preview without deleting
170
+
164
171
  # Project maintenance
165
172
  nsgm upgrade # Upgrade project base files
166
173
  nsgm export # Export static pages
@@ -38,9 +38,12 @@ export const getLocalApiPrefix = () => {
38
38
  return localApiPrefix;
39
39
  };
40
40
 
41
- export const handleXSS = (content: string) => {
42
- content = content || "";
43
- return content.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
41
+ export const handleXSS = (content: any) => {
42
+ if (content === null || content === undefined) {
43
+ return "";
44
+ }
45
+ const str = String(content);
46
+ return str.replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#x27;");
44
47
  };
45
48
 
46
49
  export const checkModalObj = (modalObj: {}, ignoreKeys: any = []) => {
@@ -37,6 +37,7 @@
37
37
  | `npm run create` | 创建模板页面 |
38
38
  | `npm run delete` | 删除模板页面 |
39
39
  | `npm run create-config` | 从配置文件批量创建模块 |
40
+ | `npm run delete-config` | 从配置文件批量删除模块 |
40
41
 
41
42
  ### 项目维护命令
42
43
 
@@ -14,6 +14,7 @@
14
14
  "delete": "nsgm delete",
15
15
  "deletedb": "nsgm deletedb",
16
16
  "create-config": "nsgm create-config config/modules.json",
17
+ "delete-config": "nsgm delete-config config/modules.json",
17
18
  "generate-password": "node scripts/generate-password-hash.js",
18
19
  "postversion": "git push && git push --tags",
19
20
  "test": "jest",
@@ -0,0 +1,5 @@
1
+ import { Command } from "../types";
2
+ /**
3
+ * 从配置文件删除命令
4
+ */
5
+ export declare const deleteConfigCommand: Command;
@@ -0,0 +1,190 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.deleteConfigCommand = void 0;
7
+ const utils_1 = require("../utils");
8
+ const generate_1 = require("../../generate");
9
+ const fs_1 = __importDefault(require("fs"));
10
+ /**
11
+ * 从配置文件删除命令
12
+ */
13
+ exports.deleteConfigCommand = {
14
+ name: "delete-config",
15
+ aliases: ["-dc", "--delete-config"],
16
+ description: "从配置文件批量删除模块",
17
+ usage: "nsgm delete-config <config-file> [options]",
18
+ examples: [
19
+ "nsgm delete-config config/modules.json",
20
+ "nsgm delete-config config/modules.json --module product",
21
+ "nsgm delete-config config/modules.json --all",
22
+ ],
23
+ options: [
24
+ {
25
+ name: "module",
26
+ description: "指定要删除的模块名称(如果不指定则删除所有)",
27
+ required: false,
28
+ type: "string",
29
+ },
30
+ {
31
+ name: "all",
32
+ description: "删除配置文件中的所有模块",
33
+ required: false,
34
+ type: "boolean",
35
+ },
36
+ {
37
+ name: "db",
38
+ description: "同时删除数据库表",
39
+ required: false,
40
+ type: "boolean",
41
+ },
42
+ {
43
+ name: "dry-run",
44
+ description: "预览模式,只显示将要删除的模块而不实际删除",
45
+ required: false,
46
+ type: "boolean",
47
+ },
48
+ {
49
+ name: "force",
50
+ description: "强制删除,不提示确认",
51
+ required: false,
52
+ type: "boolean",
53
+ },
54
+ ],
55
+ execute: async (options) => {
56
+ try {
57
+ // 获取配置文件路径
58
+ const args = process.argv.slice(2);
59
+ // 跳过命令名和别名
60
+ let configPathIndex = 0;
61
+ const commandNames = ["delete-config", "-dc", "--delete-config"];
62
+ while (configPathIndex < args.length && commandNames.includes(args[configPathIndex])) {
63
+ configPathIndex++;
64
+ }
65
+ if (configPathIndex >= args.length || args[configPathIndex].startsWith("-")) {
66
+ utils_1.Console.error("请指定配置文件路径");
67
+ utils_1.Console.info("使用方法:");
68
+ utils_1.Console.info(" nsgm delete-config <config-file> [--module <name>]");
69
+ utils_1.Console.info(" nsgm delete-config <config-file> [--all] [--db]");
70
+ utils_1.Console.info("");
71
+ utils_1.Console.info("示例:");
72
+ utils_1.Console.info(" nsgm delete-config config/modules.json");
73
+ utils_1.Console.info(" nsgm delete-config config/modules.json --module category");
74
+ utils_1.Console.info(" nsgm delete-config config/modules.json --all --db");
75
+ process.exit(1);
76
+ }
77
+ const configPath = args[configPathIndex];
78
+ // 检查配置文件是否存在
79
+ if (!fs_1.default.existsSync(configPath)) {
80
+ utils_1.Console.error(`配置文件不存在: ${configPath}`);
81
+ process.exit(1);
82
+ }
83
+ // 读取配置文件
84
+ const configContent = fs_1.default.readFileSync(configPath, "utf8");
85
+ const config = JSON.parse(configContent);
86
+ // 验证配置格式
87
+ if (!Array.isArray(config)) {
88
+ utils_1.Console.error("配置文件格式错误:必须是一个数组");
89
+ process.exit(1);
90
+ }
91
+ // 解析目标模块
92
+ let targetModules = [];
93
+ if (options.module && typeof options.module === "string") {
94
+ // 删除指定模块
95
+ const targetModule = config.find((m) => m.controller === options.module);
96
+ if (!targetModule) {
97
+ utils_1.Console.error(`未找到模块: ${options.module}`);
98
+ utils_1.Console.info(`可用的模块: ${config.map((m) => m.controller).join(", ")}`);
99
+ process.exit(1);
100
+ }
101
+ targetModules = [targetModule];
102
+ }
103
+ else {
104
+ // 删除所有模块
105
+ targetModules = config;
106
+ }
107
+ // 预览模式
108
+ if (options.dryRun) {
109
+ utils_1.Console.title("📋 预览模式");
110
+ utils_1.Console.newLine();
111
+ utils_1.Console.info("将要删除的模块:");
112
+ utils_1.Console.newLine();
113
+ targetModules.forEach((module, index) => {
114
+ utils_1.Console.highlight(`${index + 1}. ${module.controller}`);
115
+ utils_1.Console.info(` 操作: ${module.action || "manage"}`);
116
+ utils_1.Console.info(` 目录: ${module.dictionary || "./"}`);
117
+ utils_1.Console.info(` 删除数据库: ${options.db ? "是" : "否"}`);
118
+ utils_1.Console.newLine();
119
+ });
120
+ utils_1.Console.separator();
121
+ utils_1.Console.info(`总计: ${targetModules.length} 个模块`);
122
+ utils_1.Console.newLine();
123
+ utils_1.Console.info("移除 --dry-run 参数以实际删除模块");
124
+ return;
125
+ }
126
+ // 确认删除
127
+ utils_1.Console.title(`🗑️ 准备删除 ${targetModules.length} 个模块`);
128
+ utils_1.Console.newLine();
129
+ targetModules.forEach((module, index) => {
130
+ utils_1.Console.info(`${index + 1}. ${module.controller}${options.db ? " (含数据库表)" : ""}`);
131
+ });
132
+ utils_1.Console.newLine();
133
+ if (!options.force) {
134
+ const confirmed = await utils_1.Prompt.confirm("⚠️ 确认删除这些模块?此操作不可恢复!", false);
135
+ if (!confirmed) {
136
+ utils_1.Console.warning("删除已取消");
137
+ process.exit(0);
138
+ }
139
+ }
140
+ // 逐个删除模块
141
+ let successCount = 0;
142
+ let failureCount = 0;
143
+ const failures = [];
144
+ for (const module of targetModules) {
145
+ utils_1.Console.separator();
146
+ utils_1.Console.highlight(`🗑️ 删除模块 ${successCount + failureCount + 1}/${targetModules.length}: ${module.controller}`);
147
+ 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++;
154
+ utils_1.Console.newLine();
155
+ }
156
+ catch (error) {
157
+ const errorMessage = error instanceof Error ? error.message : String(error);
158
+ utils_1.Console.error(`错误: ${errorMessage}`);
159
+ failures.push({ module: module.controller, error: errorMessage });
160
+ failureCount++;
161
+ utils_1.Console.newLine();
162
+ }
163
+ }
164
+ // 显示总结
165
+ utils_1.Console.separator();
166
+ utils_1.Console.title("🎉 删除完成");
167
+ utils_1.Console.newLine();
168
+ utils_1.Console.highlight(`✅ 成功: ${successCount} 个`);
169
+ utils_1.Console.highlight(`❌ 失败: ${failureCount} 个`);
170
+ if (failures.length > 0) {
171
+ utils_1.Console.newLine();
172
+ utils_1.Console.error("失败的模块:");
173
+ failures.forEach(({ module, error }) => {
174
+ utils_1.Console.error(` - ${module}: ${error}`);
175
+ });
176
+ }
177
+ utils_1.Console.newLine();
178
+ if (options.db) {
179
+ utils_1.Console.box("删除已完成,代码和数据库表已清理!", "success");
180
+ }
181
+ else {
182
+ utils_1.Console.box("删除已完成,代码文件已清理!\n\n如需删除数据库表,请使用 --db 参数", "success");
183
+ }
184
+ }
185
+ catch (error) {
186
+ utils_1.Console.error(`执行失败: ${error}`);
187
+ process.exit(1);
188
+ }
189
+ },
190
+ };
@@ -9,6 +9,7 @@ export * from "./commands/build";
9
9
  export * from "./commands/create";
10
10
  export * from "./commands/create-config";
11
11
  export * from "./commands/delete";
12
+ export * from "./commands/delete-config";
12
13
  export * from "./commands/export";
13
14
  export * from "./commands/help";
14
15
  export * from "./commands/init";
package/lib/cli/index.js CHANGED
@@ -26,6 +26,7 @@ __exportStar(require("./commands/build"), exports);
26
26
  __exportStar(require("./commands/create"), exports);
27
27
  __exportStar(require("./commands/create-config"), exports);
28
28
  __exportStar(require("./commands/delete"), exports);
29
+ __exportStar(require("./commands/delete-config"), exports);
29
30
  __exportStar(require("./commands/export"), exports);
30
31
  __exportStar(require("./commands/help"), exports);
31
32
  __exportStar(require("./commands/init"), exports);
@@ -5,6 +5,7 @@ const build_1 = require("./commands/build");
5
5
  const create_1 = require("./commands/create");
6
6
  const create_config_1 = require("./commands/create-config");
7
7
  const delete_1 = require("./commands/delete");
8
+ const delete_config_1 = require("./commands/delete-config");
8
9
  const export_1 = require("./commands/export");
9
10
  const help_1 = require("./commands/help");
10
11
  const init_1 = require("./commands/init");
@@ -30,6 +31,7 @@ class CommandRegistry {
30
31
  create_config_1.createConfigCommand,
31
32
  delete_1.deleteCommand,
32
33
  delete_1.deleteDbCommand,
34
+ delete_config_1.deleteConfigCommand,
33
35
  export_1.exportCommand,
34
36
  help_1.helpCommand,
35
37
  init_1.initCommand,
@@ -4,5 +4,9 @@ import { BaseGenerator } from "./base-generator";
4
4
  * 专门负责生成数据库表结构
5
5
  */
6
6
  export declare class SQLGenerator extends BaseGenerator {
7
+ /**
8
+ * 从目标项目的 mysql.config.js 读取数据库名称
9
+ */
10
+ private getDatabaseName;
7
11
  generate(): string;
8
12
  }
@@ -1,12 +1,66 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
36
  exports.SQLGenerator = void 0;
4
37
  const base_generator_1 = require("./base-generator");
38
+ const path = __importStar(require("path"));
39
+ const fs = __importStar(require("fs"));
5
40
  /**
6
41
  * SQL生成器
7
42
  * 专门负责生成数据库表结构
8
43
  */
9
44
  class SQLGenerator extends base_generator_1.BaseGenerator {
45
+ /**
46
+ * 从目标项目的 mysql.config.js 读取数据库名称
47
+ */
48
+ getDatabaseName() {
49
+ try {
50
+ // 尝试从目标项目读取配置文件
51
+ const configPath = path.join(process.cwd(), "mysql.config.js");
52
+ if (fs.existsSync(configPath)) {
53
+ const config = require(configPath);
54
+ if (config?.mysqlOptions?.database) {
55
+ return config.mysqlOptions.database;
56
+ }
57
+ }
58
+ }
59
+ catch {
60
+ // 读取失败时使用默认值
61
+ }
62
+ return "crm_demo";
63
+ }
10
64
  generate() {
11
65
  const fieldDefinitions = this.fields.map((field) => {
12
66
  let sql = ` \`${field.name}\``;
@@ -41,7 +95,8 @@ class SQLGenerator extends base_generator_1.BaseGenerator {
41
95
  });
42
96
  const primaryKeyField = this.fields.find((f) => f.isPrimaryKey);
43
97
  const primaryKey = primaryKeyField ? ` PRIMARY KEY (\`${primaryKeyField.name}\`)` : "";
44
- return `use crm_demo;
98
+ const databaseName = this.getDatabaseName();
99
+ return `use ${databaseName};
45
100
 
46
101
  CREATE TABLE \`${this.controller}\` (
47
102
  ${fieldDefinitions.join(",\n")},