neo-cmp-cli 1.8.7 → 1.8.8

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.
Files changed (124) hide show
  1. package/bin/index.js +2 -2
  2. package/dist/_virtual/_commonjsHelpers.js +12 -0
  3. package/dist/_virtual/array-set.js +7 -0
  4. package/dist/_virtual/base64-vlq.js +7 -0
  5. package/dist/_virtual/base64.js +7 -0
  6. package/dist/_virtual/binary-search.js +7 -0
  7. package/dist/_virtual/mapping-list.js +7 -0
  8. package/dist/_virtual/quick-sort.js +7 -0
  9. package/dist/_virtual/source-map-consumer.js +7 -0
  10. package/dist/_virtual/source-map-generator.js +7 -0
  11. package/dist/_virtual/source-map-support.js +7 -0
  12. package/dist/_virtual/source-map.js +7 -0
  13. package/dist/_virtual/source-node.js +7 -0
  14. package/dist/_virtual/typescript.js +7 -0
  15. package/dist/_virtual/util.js +7 -0
  16. package/dist/config/auth.config.js +43 -0
  17. package/dist/config/default.config.js +192 -0
  18. package/dist/config/index.js +26 -0
  19. package/dist/main.js +9 -0
  20. package/dist/main2.js +259 -0
  21. package/dist/module/inspect.js +63 -0
  22. package/dist/module/neoInit.js +75 -0
  23. package/dist/module/neoInitByCopy.js +83 -0
  24. package/dist/neo/neoLogin.js +590 -0
  25. package/dist/neo/neoRequire.js +123 -0
  26. package/dist/neo/neoService.js +898 -0
  27. package/dist/node_modules/buffer-from/index.js +86 -0
  28. package/dist/node_modules/source-map/lib/array-set.js +138 -0
  29. package/dist/node_modules/source-map/lib/base64-vlq.js +157 -0
  30. package/dist/node_modules/source-map/lib/base64.js +83 -0
  31. package/dist/node_modules/source-map/lib/binary-search.js +129 -0
  32. package/dist/node_modules/source-map/lib/mapping-list.js +96 -0
  33. package/dist/node_modules/source-map/lib/quick-sort.js +130 -0
  34. package/dist/node_modules/source-map/lib/source-map-consumer.js +1166 -0
  35. package/dist/node_modules/source-map/lib/source-map-generator.js +445 -0
  36. package/dist/node_modules/source-map/lib/source-node.js +431 -0
  37. package/dist/node_modules/source-map/lib/util.js +506 -0
  38. package/dist/node_modules/source-map/source-map.js +27 -0
  39. package/dist/node_modules/source-map-support/source-map-support.js +646 -0
  40. package/dist/node_modules/typescript/lib/typescript.js +174130 -0
  41. package/dist/oss/publish2oss.js +331 -0
  42. package/dist/plugins/AddNeoRequirePlugin.js +195 -0
  43. package/dist/utils/autoEntryRootDir.js +103 -0
  44. package/dist/utils/cmpUtils/createCmpByTemplate.js +82 -0
  45. package/dist/utils/cmpUtils/createCmpByZip.js +433 -0
  46. package/dist/utils/cmpUtils/createCommonModulesCode.js +139 -0
  47. package/dist/utils/cmpUtils/deleteCmp.js +81 -0
  48. package/dist/utils/cmpUtils/getCmpModelRegisterCode.js +47 -0
  49. package/dist/utils/cmpUtils/getCmpPreviewCode.js +60 -0
  50. package/dist/utils/cmpUtils/getCmpRegisterCode.js +47 -0
  51. package/dist/utils/cmpUtils/getCmpTypeByDir.js +59 -0
  52. package/dist/utils/cmpUtils/hasCmpTypeByDir.js +27 -0
  53. package/dist/utils/cmpUtils/previewCmp.js +75 -0
  54. package/dist/utils/cmpUtils/pullCmp.js +126 -0
  55. package/dist/utils/cmpUtils/pushCmp.js +254 -0
  56. package/dist/utils/common.js +125 -0
  57. package/dist/utils/configureNeoBuild.js +129 -0
  58. package/dist/utils/generateEntries.js +80 -0
  59. package/dist/utils/neoConfigInit.js +30 -0
  60. package/dist/utils/neoParams.js +26 -0
  61. package/dist/utils/pathUtils.js +40 -0
  62. package/dist/utils/projectNameValidator.js +90 -0
  63. package/dist/utils/projectUtils/createCmpProjectByTemplate.js +81 -0
  64. package/dist/utils/projectUtils/createCmpProjectZip.js +141 -0
  65. package/dist/utils/projectUtils/getEntries.js +99 -0
  66. package/dist/utils/projectUtils/getEntriesWithAutoRegister.js +129 -0
  67. package/dist/utils/projectUtils/hasNeoProject.js +34 -0
  68. package/dist/utils/projectUtils/openProject.js +117 -0
  69. package/dist/utils/projectUtils/updatePublishLog.js +47 -0
  70. package/dist/utils/replaceInFilesByMap.js +71 -0
  71. package/dist/utils/replaceInPackage.js +151 -0
  72. package/dist/utils/resetPackageVersion.js +132 -0
  73. package/package.json +6 -8
  74. package/test/demo.js +0 -2
  75. package/src/config/auth.config.js +0 -27
  76. package/src/config/default.config.js +0 -176
  77. package/src/config/index.js +0 -9
  78. package/src/initData/defaultTemplate.html +0 -13
  79. package/src/initData/neo.config.js +0 -138
  80. package/src/main.js +0 -221
  81. package/src/module/inspect.js +0 -41
  82. package/src/module/neoInit.js +0 -55
  83. package/src/module/neoInitByCopy.js +0 -61
  84. package/src/neo/NeoUMDContent.js +0 -30
  85. package/src/neo/neoLogin.js +0 -565
  86. package/src/neo/neoRequire.js +0 -125
  87. package/src/neo/neoService.js +0 -874
  88. package/src/neo/webpack.mf.js +0 -60
  89. package/src/neo/wrapperContent.js +0 -16
  90. package/src/oss/publish2oss.js +0 -348
  91. package/src/plugins/AddNeoRequirePlugin-v1.js +0 -47
  92. package/src/plugins/AddNeoRequirePlugin.js +0 -179
  93. package/src/plugins/README.md +0 -109
  94. package/src/utils/autoEntryRootDir.js +0 -85
  95. package/src/utils/cmpUtils/createCmpByTemplate.js +0 -60
  96. package/src/utils/cmpUtils/createCmpByZip.js +0 -408
  97. package/src/utils/cmpUtils/createCommonModulesCode.js +0 -121
  98. package/src/utils/cmpUtils/deleteCmp.js +0 -63
  99. package/src/utils/cmpUtils/getCmpModelRegisterCode.js +0 -31
  100. package/src/utils/cmpUtils/getCmpPreviewCode.js +0 -43
  101. package/src/utils/cmpUtils/getCmpRegisterCode.js +0 -31
  102. package/src/utils/cmpUtils/getCmpTypeByDir.js +0 -41
  103. package/src/utils/cmpUtils/hasCmpTypeByDir.js +0 -11
  104. package/src/utils/cmpUtils/previewCmp.js +0 -55
  105. package/src/utils/cmpUtils/pullCmp.js +0 -104
  106. package/src/utils/cmpUtils/pushCmp.js +0 -230
  107. package/src/utils/common.js +0 -107
  108. package/src/utils/configureNeoBuild.js +0 -109
  109. package/src/utils/generateEntries.js +0 -63
  110. package/src/utils/neoConfigInit.js +0 -13
  111. package/src/utils/neoParams.js +0 -12
  112. package/src/utils/pathUtils.js +0 -23
  113. package/src/utils/projectNameValidator.js +0 -76
  114. package/src/utils/projectUtils/createCmpProjectByTemplate.js +0 -59
  115. package/src/utils/projectUtils/createCmpProjectZip.js +0 -120
  116. package/src/utils/projectUtils/getEntries.js +0 -80
  117. package/src/utils/projectUtils/getEntriesWithAutoRegister.js +0 -108
  118. package/src/utils/projectUtils/hasNeoProject.js +0 -17
  119. package/src/utils/projectUtils/openProject.js +0 -96
  120. package/src/utils/projectUtils/updatePublishLog.js +0 -30
  121. package/src/utils/replaceInFiles.js +0 -47
  122. package/src/utils/replaceInFilesByMap.js +0 -54
  123. package/src/utils/replaceInPackage.js +0 -134
  124. package/src/utils/resetPackageVersion.js +0 -115
@@ -0,0 +1,433 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$0 = require('fs-extra');
6
+ const require$$1 = require('node:path');
7
+ const require$$0$1 = require('axios');
8
+ const require$$3 = require('ora');
9
+ const require$$2 = require('adm-zip');
10
+ const require$$2$1 = require('lodash');
11
+ const neoParams = require('../neoParams.js');
12
+ const hasCmpTypeByDir = require('./hasCmpTypeByDir.js');
13
+ const hasNeoProject = require('../projectUtils/hasNeoProject.js');
14
+ const common = require('../common.js');
15
+
16
+ var createCmpByZip_1;
17
+ var hasRequiredCreateCmpByZip;
18
+
19
+ function requireCreateCmpByZip () {
20
+ if (hasRequiredCreateCmpByZip) return createCmpByZip_1;
21
+ hasRequiredCreateCmpByZip = 1;
22
+ const fs = require$$0;
23
+ const path = require$$1;
24
+ const axios = require$$0$1;
25
+ const ora = require$$3;
26
+ const AdmZip = require$$2;
27
+ const _ = require$$2$1;
28
+ const { consoleTag } = neoParams.__require(); // 输出标记
29
+ const hasCmpTypeByDir$1 = hasCmpTypeByDir.__require();
30
+ const hasNeoProject$1 = hasNeoProject.__require();
31
+ const { errorLog, warningLog, successLog, parseTsConfigWithTypeScript } = common.__require();
32
+
33
+ /**
34
+ * 检查对象键名是否需要加引号
35
+ * @param {string} key 键名
36
+ * @returns {boolean} 是否需要加引号
37
+ */
38
+ function needsQuotes(key) {
39
+ // 如果键名包含特殊字符(如 @、-、空格等)或不是有效的 JavaScript 标识符,需要加引号
40
+ // 有效的 JavaScript 标识符:以字母、$、_ 开头,后续可以是字母、数字、$、_
41
+ const validIdentifierRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
42
+ return !validIdentifierRegex.test(key);
43
+ }
44
+
45
+ /**
46
+ * 格式化对象键名,如果需要则加引号
47
+ * @param {string} key 键名
48
+ * @returns {string} 格式化后的键名
49
+ */
50
+ function formatKey(key) {
51
+ if (needsQuotes(key)) {
52
+ return JSON.stringify(key);
53
+ }
54
+ return key;
55
+ }
56
+
57
+ /**
58
+ * 将配置对象格式化为 JavaScript 代码字符串
59
+ * @param {*} obj 配置对象
60
+ * @param {number} indent 缩进级别
61
+ * @param {string} fileDir 配置文件所在目录路径,用于将绝对路径转换回相对路径(resolve() 基于 __dirname)
62
+ * @returns {string} 格式化后的字符串
63
+ */
64
+ function formatConfigObject(obj, indent = 0, fileDir = '') {
65
+ const indentStr = ' '.repeat(indent);
66
+ const nextIndentStr = ' '.repeat(indent + 1);
67
+
68
+ if (obj === null) {
69
+ return 'null';
70
+ }
71
+
72
+ if (obj === undefined) {
73
+ return 'undefined';
74
+ }
75
+
76
+ if (typeof obj === 'string') {
77
+ // 尝试将绝对路径转换回 resolve() 调用
78
+ if (fileDir && path.isAbsolute(obj)) {
79
+ try {
80
+ const relativePath = path.relative(fileDir, obj).replace(/\\/g, '/');
81
+ // 如果路径看起来像是通过 resolve() 生成的,转换回 resolve() 调用
82
+ // 排除包含 .. 的路径(这些可能是外部路径)
83
+ if (relativePath && !relativePath.startsWith('..') && relativePath !== '') {
84
+ // 确保路径使用正斜杠,并添加 ./ 前缀(如果需要)
85
+ const normalizedPath = relativePath.startsWith('.') ? relativePath : './' + relativePath;
86
+ return `resolve(${JSON.stringify(normalizedPath)})`;
87
+ }
88
+ } catch (e) {
89
+ // 如果路径转换失败,继续使用原始字符串
90
+ }
91
+ }
92
+ // 检查是否包含 resolve() 或 auth. 调用(从原始文件中提取的)
93
+ if (obj.includes('resolve(') || obj.includes('auth.')) {
94
+ return obj;
95
+ }
96
+ // 转义字符串中的特殊字符
97
+ return JSON.stringify(obj);
98
+ }
99
+
100
+ if (typeof obj === 'number' || typeof obj === 'boolean') {
101
+ return String(obj);
102
+ }
103
+
104
+ if (Array.isArray(obj)) {
105
+ if (obj.length === 0) {
106
+ return '[]';
107
+ }
108
+ const items = obj.map((item) => {
109
+ const formatted = formatConfigObject(item, indent + 1, fileDir);
110
+ return nextIndentStr + formatted;
111
+ });
112
+ return '[\n' + items.join(',\n') + '\n' + indentStr + ']';
113
+ }
114
+
115
+ if (typeof obj === 'object') {
116
+ const keys = Object.keys(obj);
117
+ if (keys.length === 0) {
118
+ return '{}';
119
+ }
120
+ const items = keys.map((key) => {
121
+ const value = obj[key];
122
+ const formattedValue = formatConfigObject(value, indent + 1, fileDir);
123
+ return nextIndentStr + formatKey(key) + ': ' + formattedValue;
124
+ });
125
+ return '{\n' + items.join(',\n') + '\n' + indentStr + '}';
126
+ }
127
+
128
+ // 对于函数或其他类型,尝试转换为字符串
129
+ return String(obj);
130
+ }
131
+
132
+ /**
133
+ * 从 zip 包中创建自定义组件
134
+ * @param {*} cmpZipUrl 自定义组件源码文件地址(zip包地址)
135
+ * @param {*} cmpName 自定义组件名称
136
+ * @param {*} componentBaseDir 自定义组件目录
137
+ *
138
+ * 拉取线上自定义组件源码 规则:
139
+ * 1、对于 组件源码中 src/components 中的所有文件 copy 至 当前项目 src/components,非覆盖式(copy 前判断是否存在);
140
+ * 2、对于 组件源码中的 配置文件(neo.config.js)内容 和 当前项目配置 进行 非覆盖式 merge 处理;
141
+ * 3、对于 组件源码中的 tsconfig.json 文件内容 和 当前项目 tsconfig  进行 非覆盖式 merge 处理;
142
+ * 4、对于 组件源码中的 package.json 文件内容 和 当前项目 package.json  进行 非覆盖式 merge 处理,并识别新增 依赖,提示用户 重新安装依赖
143
+ */
144
+ async function createCmpByZip(cmpZipUrl, option = {}) {
145
+ const { token, cmpName, componentBaseDir = './src/components' } = option || {};
146
+ const finalCmpName = cmpName;
147
+
148
+ if (!hasNeoProject$1()) {
149
+ errorLog(
150
+ `当前(${process.cwd()})还不是自定义组件项目,请先创建一个自定义组件项目(neo init / neo create project)。`
151
+ );
152
+ process.exit(1);
153
+ }
154
+
155
+ if (hasCmpTypeByDir$1(finalCmpName)) {
156
+ errorLog(`当前项目已经存在${finalCmpName}自定义组件。`);
157
+ process.exit(1);
158
+ }
159
+
160
+ // 创建临时目录
161
+ const tempDir = path.join(process.cwd(), '.neo-cli', 'zip-source');
162
+
163
+ const spinner = ora(`${consoleTag}正在下载组件源码...`).start();
164
+
165
+ // 下载源码文件并解析到 src/components 目录下
166
+ try {
167
+ await fs.ensureDir(tempDir); // 如果目录不存在,会自动创建(包括所有必要的父目录)
168
+
169
+ const zipFilePath = path.join(tempDir, `${finalCmpName}.zip`);
170
+
171
+ // 检查是否存在同名 zip 包,如果存在则先删除
172
+ if (await fs.pathExists(zipFilePath)) {
173
+ await fs.remove(zipFilePath);
174
+ }
175
+
176
+ // 下载 zip 文件
177
+ let response;
178
+ try {
179
+ response = await axios.get(cmpZipUrl, {
180
+ headers: {
181
+ Authorization: `Bearer ${token}`,
182
+ 'xsy-inner-source': 'bff'
183
+ },
184
+ responseType: 'arraybuffer',
185
+ timeout: 60000, // 60秒超时
186
+ maxContentLength: Infinity,
187
+ maxBodyLength: Infinity,
188
+ maxRedirects: 5, // 支持重定向
189
+ validateStatus: function (status) {
190
+ // 接受 2xx 和 3xx 状态码(重定向)
191
+ return status >= 200 && status < 400;
192
+ }
193
+ });
194
+ } catch (axiosError) {
195
+ const errorMessage = axiosError.response
196
+ ? `下载文件失败: HTTP ${axiosError.response.status} - ${axiosError.message}`
197
+ : `下载文件失败: ${axiosError.message}`;
198
+ errorLog(errorMessage, spinner);
199
+ throw axiosError;
200
+ }
201
+
202
+ // 保存 zip 文件到临时目录
203
+ try {
204
+ let buffer;
205
+ // 处理不同的数据类型
206
+ if (Buffer.isBuffer(response.data)) {
207
+ // 如果已经是 Buffer,直接使用
208
+ buffer = response.data;
209
+ } else if (response.data instanceof ArrayBuffer) {
210
+ // 如果是 ArrayBuffer,转换为 Buffer
211
+ buffer = Buffer.from(response.data);
212
+ } else if (response.data.buffer instanceof ArrayBuffer) {
213
+ // 如果是 TypedArray (如 Uint8Array),使用其 buffer
214
+ buffer = Buffer.from(response.data.buffer);
215
+ } else {
216
+ // 尝试直接转换
217
+ buffer = Buffer.from(response.data);
218
+ }
219
+
220
+ await fs.writeFile(zipFilePath, buffer);
221
+ } catch (writeError) {
222
+ errorLog(`读取组件源码文件失败: ${writeError.message}`, spinner);
223
+ throw writeError;
224
+ }
225
+
226
+ // 解压 zip 文件
227
+ spinner.info(`${consoleTag}正在解压组件源码...`);
228
+
229
+ const zip = new AdmZip(zipFilePath);
230
+ // 解压后的目录名称为组件名称:.neo-cli/zip-source/xxCmp
231
+ const extractPath = path.join(tempDir, finalCmpName);
232
+ zip.extractAllTo(extractPath, true); // 解压到临时目录
233
+
234
+ // 查找解压后的 src/components 目录
235
+ const cmpSourcePath = path.join(extractPath, componentBaseDir, finalCmpName);
236
+
237
+ if (!fs.existsSync(cmpSourcePath)) {
238
+ errorLog(`解压后的 zip 包中未找到 ${finalCmpName} 组件源码目录。`, spinner);
239
+ // 只删除解压目录,保留 zip 源文件
240
+ await fs.remove(extractPath);
241
+ return false;
242
+ }
243
+
244
+ // 确保目标目录存在
245
+ const targetComponentsDir = path.resolve(process.cwd(), componentBaseDir, finalCmpName);
246
+ await fs.ensureDir(targetComponentsDir);
247
+
248
+ // 复制 src/components 目录下的所有内容到目标目录
249
+ try {
250
+ await fs.copy(cmpSourcePath, targetComponentsDir);
251
+ } catch (err) {
252
+ errorLog(`自定义组件模板下载失败:${err.message || err}`, spinner);
253
+ // 只删除解压目录,保留 zip 源文件
254
+ await fs.remove(extractPath);
255
+ return false;
256
+ }
257
+
258
+ // 处理源码中其他文件
259
+ try {
260
+ // 标准化 componentBaseDir,去掉开头的 ./ 并统一路径分隔符
261
+ const normalizedComponentBaseDir = componentBaseDir.replace(/^\.\//, '').replace(/\\/g, '/');
262
+
263
+ /**
264
+ * 递归复制文件,排除 componentBaseDir 目录
265
+ * @param {string} sourceDir 源目录
266
+ * @param {string} targetRoot 目标根目录(项目根目录)
267
+ * @param {string} excludeDir 要排除的目录(相对于源目录)
268
+ */
269
+ const copyOtherFiles = async (sourceDir, targetRoot, excludeDir) => {
270
+ const items = await fs.readdir(sourceDir);
271
+
272
+ for (const item of items) {
273
+ const sourcePath = path.join(sourceDir, item); // 当前源文件路径
274
+ const stat = await fs.stat(sourcePath); // 当前源文件状态
275
+ const fileName = path.basename(sourcePath); // 当前源文件名称
276
+
277
+ // 计算相对于解压根目录的路径
278
+ const relativePath = path.relative(extractPath, sourcePath);
279
+ const normalizedRelativePath = relativePath.replace(/\\/g, '/');
280
+
281
+ // 跳过 componentBaseDir 目录及其子目录
282
+ if (
283
+ normalizedRelativePath.startsWith(excludeDir + '/') ||
284
+ normalizedRelativePath === excludeDir
285
+ ) {
286
+ continue;
287
+ }
288
+
289
+ const targetPath = path.join(targetRoot, relativePath); // 目标文件路径
290
+
291
+ if (stat.isDirectory()) {
292
+ // 如果是目录,递归处理
293
+ await copyOtherFiles(sourcePath, targetRoot, excludeDir);
294
+ } else if (stat.isFile()) {
295
+ // 如果是文件,检查目标文件是否存在
296
+ const targetExists = await fs.pathExists(targetPath);
297
+ if (!targetExists) {
298
+ // 确保目标目录存在
299
+ await fs.ensureDir(path.dirname(targetPath));
300
+ // 复制文件
301
+ await fs.copy(sourcePath, targetPath);
302
+ } else if (fileName === 'package.json') {
303
+ // 处理 package.json 文件(当目标文件已存在时)
304
+ const sourcePackageJson = await fs.readJson(sourcePath);
305
+ const targetPackageJson = await fs.readJson(targetPath);
306
+
307
+ const newPackageJsonDeps = _.omit(
308
+ sourcePackageJson.dependencies,
309
+ Object.keys(targetPackageJson.dependencies)
310
+ );
311
+ const newDeps = Object.keys(newPackageJsonDeps);
312
+ if (newDeps.length > 0) {
313
+ warningLog(
314
+ `检测到 package.json 中新增了 ${newDeps.length} 个依赖包:${newDeps.join(', ')}`
315
+ );
316
+ warningLog(
317
+ `为确保组件正常运行,请执行以下命令安装依赖:npm install 或 yarn install`
318
+ );
319
+ }
320
+ // 合并 package.json:将源文件的配置合并到目标文件中
321
+ // 注意:_.merge 会修改第一个参数,所以先克隆目标对象
322
+ const mergedPackageJson = _.merge({}, sourcePackageJson, targetPackageJson);
323
+ await fs.writeJson(targetPath, mergedPackageJson, { spaces: 2 });
324
+ } else if (fileName === 'neo.config.js') {
325
+ // 处理 neo.config.js 文件(当目标文件已存在时)
326
+ try {
327
+ // 清除 require 缓存,确保读取最新内容
328
+ const sourceResolvedPath = require.resolve(sourcePath);
329
+ const targetResolvedPath = require.resolve(targetPath);
330
+ delete require.cache[sourceResolvedPath];
331
+ delete require.cache[targetResolvedPath];
332
+
333
+ // 读取源文件和目标文件的原始内容
334
+ const sourceContent = await fs.readFile(sourcePath, 'utf8');
335
+ const targetContent = await fs.readFile(targetPath, 'utf8');
336
+
337
+ // 读取源文件和目标文件的配置对象(函数调用会被执行)
338
+ const sourceConfig = require(sourcePath);
339
+ const targetConfig = require(targetPath);
340
+
341
+ // 合并配置:源配置合并到目标配置(目标配置优先级更高)
342
+ const mergedConfig = _.merge({}, sourceConfig, targetConfig);
343
+
344
+ // 提取目标文件的头部(module.exports 之前的内容)
345
+ const moduleExportMatch = targetContent.match(
346
+ /^([\s\S]*?)(module\.exports\s*=\s*\{[\s\S]*\};?\s*)$/
347
+ );
348
+ let fileHeader = '';
349
+ if (moduleExportMatch) {
350
+ fileHeader = moduleExportMatch[1];
351
+ } else {
352
+ // 如果没有找到 module.exports,尝试提取到最后一个大括号之前的内容
353
+ const lastBraceIndex = targetContent.lastIndexOf('module.exports');
354
+ if (lastBraceIndex > 0) {
355
+ fileHeader = targetContent.substring(0, lastBraceIndex);
356
+ } else {
357
+ // 如果都没有找到,使用默认头部
358
+ fileHeader =
359
+ "'use strict';\nconst path = require('path');\n\n// 统一路径解析\nfunction resolve(dir) {\n return path.resolve(__dirname, dir);\n}\n\n";
360
+ }
361
+ }
362
+
363
+ // 获取目标文件的目录,用于路径转换(resolve() 基于 __dirname)
364
+ const targetFileDir = path.dirname(targetPath);
365
+
366
+ // 将合并后的配置对象转换为格式化的字符串
367
+ const configString = formatConfigObject(mergedConfig, 0, targetFileDir);
368
+
369
+ // 组合完整的文件内容
370
+ const mergedContent = fileHeader + 'module.exports = ' + configString + ';\n';
371
+
372
+ // 写回文件
373
+ await fs.writeFile(targetPath, mergedContent, 'utf8');
374
+ } catch (configError) {
375
+ spinner.warn(
376
+ `${consoleTag}合并 neo.config.js 配置文件时出现警告:${
377
+ configError.message || configError
378
+ }`
379
+ );
380
+ }
381
+ } else if (fileName === 'tsconfig.json') {
382
+ // 处理 tsconfig.json 文件(当目标文件已存在时)
383
+ try {
384
+ const sourceTsconfigJson = parseTsConfigWithTypeScript(sourcePath);
385
+ // 先判断 targetPath 是否存在,如果存在则合并,否则直接写入
386
+ if (await fs.pathExists(targetPath)) {
387
+ const targetTsconfigJson = parseTsConfigWithTypeScript(targetPath);
388
+ // 合并配置:源配置合并到目标配置(目标配置优先级更高)
389
+ const mergedTsconfigJson = _.merge({}, sourceTsconfigJson, targetTsconfigJson);
390
+ // 使用 writeJson 写入,确保 JSON 格式正确(所有键名都会被正确序列化)
391
+ await fs.writeJson(targetPath, mergedTsconfigJson, { spaces: 2 });
392
+ } else {
393
+ await fs.writeJson(targetPath, sourceTsconfigJson, { spaces: 2 });
394
+ }
395
+ } catch (tsconfigError) {
396
+ spinner.warn(
397
+ `${consoleTag}合并 tsconfig.json 配置文件时出现警告:${
398
+ tsconfigError.message || tsconfigError
399
+ }`
400
+ );
401
+ }
402
+ }
403
+ }
404
+ }
405
+ };
406
+ // 开始处理源码中其他文件
407
+ await copyOtherFiles(extractPath, process.cwd(), normalizedComponentBaseDir);
408
+ } catch (err) {
409
+ spinner.warn(`${consoleTag}处理源码文件出现警告:${err.message || err}`);
410
+ }
411
+
412
+ // 清理解压目录,保留 zip 源文件
413
+ await fs.remove(extractPath);
414
+ successLog(`已成功从 zip 包解析自定义组件(${finalCmpName})!`, spinner);
415
+
416
+ return true;
417
+ } catch (error) {
418
+ errorLog(`从 zip 包创建自定义组件失败(${finalCmpName}):${error.message || error}`, spinner);
419
+ // 清理解压目录,保留 zip 源文件
420
+ const extractPath = path.join(tempDir, finalCmpName);
421
+ if (await fs.pathExists(extractPath)) {
422
+ await fs.remove(extractPath);
423
+ }
424
+
425
+ return false;
426
+ }
427
+ }
428
+
429
+ createCmpByZip_1 = createCmpByZip;
430
+ return createCmpByZip_1;
431
+ }
432
+
433
+ exports.__require = requireCreateCmpByZip;
@@ -0,0 +1,139 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$0 = require('node:fs');
6
+ const pathUtils = require('../pathUtils.js');
7
+ const require$$2 = require('lodash');
8
+
9
+ var createCommonModulesCode_1;
10
+ var hasRequiredCreateCommonModulesCode;
11
+
12
+ function requireCreateCommonModulesCode () {
13
+ if (hasRequiredCreateCommonModulesCode) return createCommonModulesCode_1;
14
+ hasRequiredCreateCommonModulesCode = 1;
15
+ const fs = require$$0;
16
+ const { resolveToCurrentRoot } = pathUtils.__require();
17
+ const { isPlainObject } = require$$2;
18
+
19
+ /**
20
+ * 获取组件预览代码
21
+ * @param {*} cmpsDir 自定义组件目录
22
+ * @param {*} cmpName 自定义组件名称
23
+ * @returns 组件预览代码
24
+ */
25
+ const createCommonModulesCode = (neoCommonModule, cmpTypes) => {
26
+ const { exports: exports$1, remoteDeps } = neoCommonModule;
27
+
28
+ if (!exports$1 && !remoteDeps) {
29
+ return '';
30
+ }
31
+ // 记录当前自定义组件共享出去的模块
32
+ const CustomCmpCommonModules = {};
33
+ // 记录当前自定义组件需要的远程依赖组件
34
+ const CustomCmpRemoteDeps = {};
35
+
36
+ // 根据 exports 获取共享的依赖模块
37
+ if (Array.isArray(exports$1) && exports$1.length > 0) {
38
+ exports$1.forEach((module) => {
39
+ CustomCmpCommonModules[module] = require(module);
40
+ });
41
+ } else if (isPlainObject(exports$1) && Object.keys(exports$1).length > 0) {
42
+ Object.keys(exports$1).forEach((moduleId) => {
43
+ CustomCmpCommonModules[moduleId] = `require('${exports$1[moduleId]}')`;
44
+ });
45
+ } else if (exports$1) {
46
+ console.error(
47
+ 'exports 格式不正确,请检查 neo.config.js 文件中的 neoCommonModule / exports 配置'
48
+ );
49
+ process.exit(1);
50
+ }
51
+
52
+ // 根据 cmpTypes 和 remoteDeps 设置远程依赖组件信息
53
+ if (Array.isArray(remoteDeps) && remoteDeps.length > 0) {
54
+ cmpTypes.forEach((cmpType) => {
55
+ CustomCmpRemoteDeps[cmpType] = remoteDeps;
56
+ });
57
+ }
58
+
59
+ // 构建 CustomCmpCommonModules 对象的代码字符串
60
+ let customCmpCommonModulesCode = '{\n';
61
+ const moduleEntries = Object.entries(CustomCmpCommonModules);
62
+ moduleEntries.forEach(([moduleId, moduleValue], index) => {
63
+ const isLast = index === moduleEntries.length - 1;
64
+ customCmpCommonModulesCode += ` "${moduleId}": ${moduleValue}${isLast ? '' : ','}\n`;
65
+ });
66
+ customCmpCommonModulesCode += '}';
67
+
68
+ const commonModulesCode = `
69
+ /**
70
+ * 自定义组件 共享出来的依赖模块
71
+ * 备注:可在其他模块中通过 neoRequire 中使用
72
+ */
73
+ import { isPlainObject } from 'lodash';
74
+ const CustomCmpCommonModules = ${customCmpCommonModulesCode};
75
+ const CustomCmpRemoteDeps = ${JSON.stringify(CustomCmpRemoteDeps)};
76
+
77
+ // 用于添加共享的依赖模块
78
+ const addNeoCommonModules = (modules) => {
79
+ if (!window.__NeoCommonModules) {
80
+ window.__NeoCommonModules = {}
81
+ }
82
+ if (isPlainObject(modules)) {
83
+ const moduleIds = Object.keys(modules)
84
+ moduleIds.forEach((moduleId) => {
85
+ const curModule = modules[moduleId];
86
+ const curCommonModule = window.__NeoCommonModules[moduleId];
87
+
88
+ if (curCommonModule && Object.keys(curCommonModule).length < 3) {
89
+ window.__NeoCommonModules[moduleId] = Object.assign(window.__NeoCommonModules[moduleId], curModule);
90
+
91
+ // 处理模块中的特殊属性
92
+ if (curModule.__esModule !== undefined) {
93
+ window.__NeoCommonModules[moduleId].__esModule = curModule.__esModule;
94
+ }
95
+ if (curModule.default !== undefined) {
96
+ window.__NeoCommonModules[moduleId].default = curModule.default;
97
+ }
98
+ } else {
99
+ window.__NeoCommonModules[moduleId] = curModule;
100
+ }
101
+ })
102
+ }
103
+ }
104
+
105
+ // 用于添加自定义组件的远程依赖组件(关联使用)
106
+ const addNeoRemoteDeps = (remoteDeps) => {
107
+ if (!window.__NeoCommonModules) {
108
+ window.__NeoCommonModules = {}
109
+ }
110
+ if (!window.__NeoCommonModules.__neoRemoteDeps) {
111
+ window.__NeoCommonModules.__neoRemoteDeps = {}
112
+ }
113
+ if (isPlainObject(remoteDeps)) {
114
+ window.__NeoCommonModules.__neoRemoteDeps = Object.assign(window.__NeoCommonModules.__neoRemoteDeps, remoteDeps)
115
+ }
116
+ }
117
+
118
+ addNeoCommonModules(CustomCmpCommonModules);
119
+ addNeoRemoteDeps(CustomCmpRemoteDeps);
120
+ `;
121
+
122
+ // 创建存放 cli 的临时目录
123
+ const cliTempDir = resolveToCurrentRoot('./.neo-cli');
124
+ if (!fs.existsSync(cliTempDir)) {
125
+ fs.mkdirSync(cliTempDir);
126
+ }
127
+
128
+ // 将 commonModulesCode 写入到临时目录中
129
+ const commonModulesFilePath = `${cliTempDir}/commonModules.js`;
130
+ fs.writeFileSync(commonModulesFilePath, commonModulesCode);
131
+
132
+ return commonModulesFilePath;
133
+ };
134
+
135
+ createCommonModulesCode_1 = createCommonModulesCode;
136
+ return createCommonModulesCode_1;
137
+ }
138
+
139
+ exports.__require = requireCreateCommonModulesCode;
@@ -0,0 +1,81 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$3 = require('ora');
6
+ const neoService = require('../../neo/neoService.js');
7
+ const common = require('../common.js');
8
+
9
+ var deleteCmp_1;
10
+ var hasRequiredDeleteCmp;
11
+
12
+ function requireDeleteCmp () {
13
+ if (hasRequiredDeleteCmp) return deleteCmp_1;
14
+ hasRequiredDeleteCmp = 1;
15
+ const ora = require$$3;
16
+ const NeoService = neoService.__require();
17
+ const { errorLog, successLog } = common.__require();
18
+
19
+ /**
20
+ * 从 NeoCRM 删除自定义组件
21
+ * @param {string} cmpType 自定义组件类型
22
+ * @param {object} authConfig 授权配置
23
+ * @param {object} _neoService 可选的 NeoService 实例(用于复用已获取的组件列表)
24
+ *
25
+ * 删除流程说明
26
+ * 1. 获取当前租户下的自定义组件列表
27
+ * 2. 验证组件是否存在
28
+ * 3. 调用删除 API 删除组件
29
+ */
30
+ const deleteCmp = async (cmpType, authConfig, _neoService) => {
31
+ if (!authConfig) {
32
+ errorLog('未找到 NeoCRM 平台授权配置(neo.config.js / authConfig)。');
33
+ return;
34
+ }
35
+
36
+ const spinner = ora('正在删除组件...').start();
37
+
38
+ try {
39
+ let neoService = _neoService;
40
+ let cmpList = [];
41
+ let cmpInfo = null;
42
+
43
+ if (_neoService) {
44
+ // 使用传入的 neoService 实例
45
+ cmpList = _neoService.cmpList || [];
46
+ } else {
47
+ // 创建新的 neoService 实例
48
+ neoService = new NeoService(authConfig);
49
+
50
+ // 获取自定义组件列表
51
+ spinner.info('正在获取自定义组件列表...');
52
+ cmpList = await neoService.getCustomCmpList();
53
+ }
54
+
55
+ if (cmpList.length === 0) {
56
+ errorLog('删除失败,当前租户暂无任何自定义组件。', spinner);
57
+ process.exit(1);
58
+ }
59
+
60
+ // 获取自定义组件信息
61
+ cmpInfo = neoService.getCmpInfoByCmpType(cmpType);
62
+ if (!cmpInfo) {
63
+ errorLog(`删除失败,当前租户不存在${cmpType}自定义组件。`, spinner);
64
+ process.exit(1);
65
+ }
66
+
67
+ // 调用删除 API
68
+ spinner.info(`正在删除${cmpType}自定义组件...`);
69
+ await neoService.deleteCmp(cmpType);
70
+ successLog(`已成功删除${cmpType}自定义组件!\n`, spinner);
71
+ } catch (error) {
72
+ errorLog(`删除自定义组件失败: ${error.message}`, spinner);
73
+ process.exit(1);
74
+ }
75
+ };
76
+
77
+ deleteCmp_1 = deleteCmp;
78
+ return deleteCmp_1;
79
+ }
80
+
81
+ exports.__require = requireDeleteCmp;