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,331 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$0 = require('akfun');
6
+ const require$$0$1 = require('node:fs');
7
+ const require$$1 = require('node:path');
8
+ const require$$2 = require('lodash');
9
+ const require$$3 = require('ora');
10
+ const pathUtils = require('../utils/pathUtils.js');
11
+ const updatePublishLog = require('../utils/projectUtils/updatePublishLog.js');
12
+ const common = require('../utils/common.js');
13
+
14
+ var publish2oss_1;
15
+ var hasRequiredPublish2oss;
16
+
17
+ function requirePublish2oss () {
18
+ if (hasRequiredPublish2oss) return publish2oss_1;
19
+ hasRequiredPublish2oss = 1;
20
+ const { aliBOS, baiduBOS } = require$$0;
21
+ const fs = require$$0$1;
22
+ const path = require$$1;
23
+ const _ = require$$2;
24
+ const ora = require$$3;
25
+ const { getConfigObj } = require$$0;
26
+ const { catchCurPackageJson } = pathUtils.__require();
27
+ const updatePublishLog$1 = updatePublishLog.__require();
28
+ const { errorLog, warningLog, successLog } = common.__require();
29
+
30
+ // 获取当前项目的package文件
31
+ const currentPackageJsonDir = catchCurPackageJson();
32
+ const currentPackageJson = getConfigObj(currentPackageJsonDir);
33
+
34
+ // 根据 ossType 获取对应的 BosClient
35
+ const getBosClient = (ossType, ossConfig) => {
36
+ if (ossType === 'baidu') {
37
+ return new baiduBOS(
38
+ ossConfig.endpoint,
39
+ ossConfig.bucket,
40
+ ossConfig.AccessKeyId ||
41
+ ossConfig.AccessKeyID ||
42
+ ossConfig.accessKeyId ||
43
+ ossConfig.accessKeyID,
44
+ ossConfig.AccessKeySecret
45
+ );
46
+ } else if (ossType === 'ali') {
47
+ return new aliBOS(
48
+ ossConfig.endpoint,
49
+ ossConfig.bucket,
50
+ ossConfig.AccessKeyId ||
51
+ ossConfig.AccessKeyID ||
52
+ ossConfig.accessKeyId ||
53
+ ossConfig.accessKeyID,
54
+ ossConfig.AccessKeySecret
55
+ );
56
+ }
57
+ };
58
+
59
+ const getFilePath = (ossType, bucket, objectKey) => {
60
+ const regions = {
61
+ baidu: 'bj.bcebos.com',
62
+ ali: 'oss-cn-beijing.aliyuncs.com'
63
+ };
64
+ const region = regions[ossType];
65
+ if (!region) {
66
+ throw new Error(`不支持的oss类型: ${ossType}`);
67
+ }
68
+ return `https://${bucket}.${region}/${objectKey}`;
69
+ };
70
+
71
+ // 按组件名称进行分类
72
+ const getResultFilesByWidgetName = (files) => {
73
+ const widgetFilesMap = {};
74
+ files.forEach((file) => {
75
+ if (!file || !file.widgetName) {
76
+ return;
77
+ }
78
+ let widgetName = file.widgetName;
79
+ const curCmpInfo = {
80
+ cmpType: _.kebabCase(widgetName)
81
+ };
82
+
83
+ if (widgetName.includes('Model')) {
84
+ widgetName = widgetName.replace('Model', '');
85
+ curCmpInfo.cmpType = _.kebabCase(widgetName);
86
+ curCmpInfo.modelAsset = !file.success ? `${file.error}[${file.ossPath}]` : file.ossPath;
87
+ } else if (file.fileName.endsWith('.css')) {
88
+ curCmpInfo.cssAsset = !file.success ? `${file.error}[${file.ossPath}]` : file.ossPath;
89
+ } else {
90
+ curCmpInfo.asset = !file.success ? `${file.error}[${file.ossPath}]` : file.ossPath;
91
+ }
92
+
93
+ if (widgetFilesMap[widgetName]) {
94
+ widgetFilesMap[widgetName] = Object.assign(widgetFilesMap[widgetName], curCmpInfo);
95
+ } else {
96
+ // widgetFilesMap[widgetName] = [getResultFile(file)];
97
+ widgetFilesMap[widgetName] = curCmpInfo;
98
+ }
99
+ });
100
+
101
+ return widgetFilesMap;
102
+ };
103
+
104
+ // 文件名中添加当前项目版本号
105
+ function addVersionToFilename(
106
+ filename,
107
+ version = currentPackageJson.version,
108
+ projectName = currentPackageJson.name
109
+ ) {
110
+ // 找到最后一个点的位置,用于分离文件名和扩展名
111
+ let lastDotIndex = filename.lastIndexOf('.umd');
112
+ if (lastDotIndex === -1) {
113
+ lastDotIndex = filename.lastIndexOf('.esm');
114
+ }
115
+ if (lastDotIndex === -1) {
116
+ lastDotIndex = filename.lastIndexOf('.lib');
117
+ }
118
+ if (lastDotIndex === -1) {
119
+ lastDotIndex = filename.lastIndexOf('.');
120
+ }
121
+ // 如果没有扩展名(没有点),直接在文件名后添加版本号
122
+ if (lastDotIndex === -1) {
123
+ return `${projectName}/${filename}-${version}`;
124
+ }
125
+
126
+ // 分离文件名的基础部分和扩展名
127
+ const baseName = filename.substring(0, lastDotIndex);
128
+ const extension = filename.substring(lastDotIndex);
129
+
130
+ // 组合新的文件名:基础名-版本号.扩展名
131
+ return `${projectName}/${baseName}-${version}${extension}`;
132
+ }
133
+
134
+ /**
135
+ * 上传单个文件到 OSS
136
+ * @param {object} bosClient OSS 客户端实例
137
+ * @param {string} filePath 文件路径
138
+ * @param {string} objectKey OSS 对象键
139
+ * @param {string} ossType OSS 类型
140
+ * @param {object} ossConfig OSS 配置
141
+ * @param {object} fileInfo 文件信息
142
+ * @returns {Promise<object>} 上传结果
143
+ */
144
+ async function uploadSingleFile(bosClient, filePath, objectKey, ossType, ossConfig, fileInfo) {
145
+ const fileName = path.basename(filePath);
146
+ const fileSizeKB = (fs.statSync(filePath).size / 1024).toFixed(2);
147
+ const spinner = ora(`正在上传文件: ${fileName} (${fileSizeKB}KB)...`).start();
148
+
149
+ try {
150
+ // 判断线上是否存在重名文件,避免被覆盖
151
+ const historyResult = await bosClient.get(objectKey);
152
+ if (historyResult && historyResult.url) {
153
+ spinner.warn(`文件已存在,跳过上传: ${fileName}`);
154
+ return {
155
+ success: false,
156
+ status: '文件上传失败',
157
+ widgetName: fileInfo.name,
158
+ fileName: fileName,
159
+ filepath: filePath,
160
+ ossPath: historyResult.url,
161
+ error: '线上存在重名文件。'
162
+ };
163
+ }
164
+
165
+ // 上传文件
166
+ const result = await bosClient.upload(objectKey, filePath);
167
+ const ossPath = getFilePath(ossType, ossConfig.bucket, objectKey);
168
+ successLog(`文件上传成功: ${fileName} -> ${ossPath}`, spinner);
169
+
170
+ return {
171
+ success: true,
172
+ status: '文件上传成功',
173
+ fileName: fileName,
174
+ widgetName: fileInfo.name,
175
+ filepath: filePath,
176
+ ossPath: ossPath,
177
+ resultMsg: result
178
+ };
179
+ } catch (error) {
180
+ errorLog(`文件上传失败: ${fileName}`, spinner);
181
+
182
+ // 输出详细的错误信息
183
+ errorLog(`\n========== 上传文件详情 ==========`);
184
+ errorLog(`文件路径: ${filePath}`);
185
+ errorLog(`OSS 对象键: ${objectKey}`);
186
+ errorLog(`错误信息: ${error.message}`);
187
+ if (error.stack) {
188
+ errorLog(`错误堆栈: ${error.stack}`);
189
+ }
190
+ errorLog(`==================================\n`);
191
+
192
+ return {
193
+ success: false,
194
+ status: '文件上传失败',
195
+ widgetName: fileInfo.name,
196
+ fileName: fileName,
197
+ filepath: filePath,
198
+ error: error.message
199
+ };
200
+ }
201
+ }
202
+
203
+ /**
204
+ * 将构建产物上传到指定 oss 存储桶
205
+ *
206
+ * @param {string} ossType oss 类型:baidu、ali
207
+ * @param {object} ossConfig oss 配置
208
+ * @param {string} assetsRoot 构建产物的目录
209
+ * @param {array} fileExtensions 需要上传的文件类型,默认 ['.js', '.css']
210
+ */
211
+ const publish2oss = async (ossType, ossConfig, assetsRoot, fileExtensions = ['.js', '.css']) => {
212
+ // 参数验证
213
+ if (ossType !== 'baidu' && ossType !== 'ali') {
214
+ errorLog(`不支持的oss类型: ${ossType}`);
215
+ return;
216
+ }
217
+
218
+ if (!ossConfig) {
219
+ errorLog('ossConfig 不能为空');
220
+ return;
221
+ }
222
+
223
+ if (!assetsRoot) {
224
+ errorLog('assetsRoot 不能为空');
225
+ return;
226
+ }
227
+
228
+ // 检查目录是否存在
229
+ if (!fs.existsSync(assetsRoot)) {
230
+ errorLog(`assetsRoot 不存在: ${assetsRoot}`);
231
+ return;
232
+ }
233
+
234
+ // 检查目录是否为目录
235
+ const assetsRootStat = fs.statSync(assetsRoot);
236
+ if (!assetsRootStat.isDirectory()) {
237
+ errorLog(`assetsRoot 不是目录: ${assetsRoot}`);
238
+ return;
239
+ }
240
+
241
+ // 创建 OSS 客户端
242
+ const bosClient = getBosClient(ossType, ossConfig);
243
+ if (!bosClient) {
244
+ errorLog(`无法创建 OSS 客户端,请检查 ossType 和 ossConfig 配置`);
245
+ return;
246
+ }
247
+
248
+ // 读取构建目录下的所有文件
249
+ const files = fs.readdirSync(assetsRoot);
250
+ if (files.length === 0) {
251
+ warningLog(`构建目录为空: ${assetsRoot}`);
252
+ return;
253
+ }
254
+
255
+ const spinner = ora('正在准备上传文件...').start();
256
+
257
+ try {
258
+ // 过滤出需要上传的文件
259
+ const filesToUpload = files.filter((file) => {
260
+ const filePath = path.join(assetsRoot, file);
261
+ try {
262
+ const fileStat = fs.statSync(filePath);
263
+ if (!fileStat.isFile()) {
264
+ return false;
265
+ }
266
+ const fileInfo = path.parse(filePath);
267
+ return fileExtensions.includes(fileInfo.ext);
268
+ } catch (error) {
269
+ warningLog(`无法读取文件状态: ${filePath} - ${error.message}`);
270
+ return false;
271
+ }
272
+ });
273
+
274
+ if (filesToUpload.length === 0) {
275
+ spinner.warn('未找到需要上传的文件');
276
+ return;
277
+ }
278
+
279
+ successLog(`找到 ${filesToUpload.length} 个文件需要上传`, spinner);
280
+
281
+ // 并行上传所有指定类型的文件
282
+ const uploadPromises = filesToUpload.map(async (file) => {
283
+ const filePath = path.join(assetsRoot, file);
284
+ const fileInfo = path.parse(filePath);
285
+ const objectKey = addVersionToFilename(file);
286
+
287
+ return await uploadSingleFile(bosClient, filePath, objectKey, ossType, ossConfig, fileInfo);
288
+ });
289
+
290
+ // 等待所有文件上传完成
291
+ const results = await Promise.all(uploadPromises);
292
+
293
+ // 过滤掉 undefined 结果(不符合条件的文件)
294
+ const validResults = results.filter((result) => result !== undefined);
295
+
296
+ // 处理上传结果
297
+ const widgetFilesMap = getResultFilesByWidgetName(validResults);
298
+ if (widgetFilesMap && Object.keys(widgetFilesMap).length > 0) {
299
+ console.info('\n上传至 OSS 的文件信息:\n', widgetFilesMap);
300
+ // 更新发布日志
301
+ updatePublishLog$1(widgetFilesMap);
302
+ } else {
303
+ warningLog('未生成有效的文件上传结果');
304
+ }
305
+
306
+ // 统计上传结果
307
+ const succeedCount = validResults.filter((r) => r && r.success).length;
308
+ const failCount = validResults.filter((r) => r && !r.success).length;
309
+
310
+ if (failCount > 0) {
311
+ warningLog(`\n上传完成:成功 ${succeedCount} 个,失败 ${failCount} 个`);
312
+ } else {
313
+ console.info(`\n✅ 所有文件上传成功!共 ${succeedCount} 个文件`);
314
+ }
315
+ } catch (error) {
316
+ errorLog('批量上传文件异常', spinner);
317
+ errorLog('\n========== 批量上传异常 ==========');
318
+ errorLog(`错误信息: ${error.message}`);
319
+ if (error.stack) {
320
+ errorLog(`错误堆栈: ${error.stack}`);
321
+ }
322
+ errorLog('==================================\n');
323
+ throw error;
324
+ }
325
+ };
326
+
327
+ publish2oss_1 = publish2oss;
328
+ return publish2oss_1;
329
+ }
330
+
331
+ exports.__require = requirePublish2oss;
@@ -0,0 +1,195 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$0 = require('webpack-sources');
6
+ require('node:fs');
7
+ require('node:path');
8
+
9
+ var AddNeoRequirePlugin_1;
10
+ var hasRequiredAddNeoRequirePlugin;
11
+
12
+ function requireAddNeoRequirePlugin () {
13
+ if (hasRequiredAddNeoRequirePlugin) return AddNeoRequirePlugin_1;
14
+ hasRequiredAddNeoRequirePlugin = 1;
15
+ const { ConcatSource } = require$$0;
16
+ /**
17
+ * 注入 neoRequire 函数
18
+ * 备注:用于实现和 Neo 平台共享依赖
19
+ */
20
+ class AddNeoRequirePlugin {
21
+ key = 'AddNeoRequirePlugin';
22
+
23
+ constructor(options = {}) {
24
+ this.options = {
25
+ // 是否启用详细日志
26
+ verbose: options.verbose || false,
27
+ // 是否跳过已注入的文件
28
+ skipInjected: options.skipInjected !== false,
29
+ // 需要跳过的文件扩展名
30
+ skipExtensions: options.skipExtensions || ['.css', '.map', '.txt', '.html'],
31
+ // 需要跳过的文件名模式
32
+ skipPatterns: options.skipPatterns || [/\.map$/, /\.LICENSE\.txt$/, /\.html$/],
33
+ ...options
34
+ };
35
+ }
36
+
37
+ /**
38
+ * 检查文件是否应该被跳过
39
+ */
40
+ shouldSkipFile(filename) {
41
+ // 检查扩展名
42
+ for (const ext of this.options.skipExtensions) {
43
+ if (filename.endsWith(ext)) {
44
+ return true;
45
+ }
46
+ }
47
+
48
+ // 检查文件名模式
49
+ for (const pattern of this.options.skipPatterns) {
50
+ if (pattern.test(filename)) {
51
+ return true;
52
+ }
53
+ }
54
+
55
+ return false;
56
+ }
57
+
58
+ /**
59
+ * 检查文件是否已经被注入过
60
+ */
61
+ isAlreadyInjected(content) {
62
+ if (!content || typeof content !== 'string') {
63
+ return false;
64
+ }
65
+
66
+ // 检查是否包含注入的标识
67
+ return (
68
+ content.includes('NeoCustomCmpFileFactory(window.neoRequire)') &&
69
+ content.includes('if (!window.neoRequire)')
70
+ );
71
+ }
72
+
73
+ /**
74
+ * 验证文件内容是否有效
75
+ */
76
+ isValidFile(content) {
77
+ if (!content || typeof content !== 'string') {
78
+ return false;
79
+ }
80
+
81
+ // 检查文件是否为空或只包含空白字符
82
+ if (content.trim().length === 0) {
83
+ return false;
84
+ }
85
+
86
+ return true;
87
+ }
88
+
89
+ /**
90
+ * 记录日志
91
+ */
92
+ log(message, level = 'info') {
93
+ if (this.options.verbose || level === 'error') {
94
+ const prefix = `[AddNeoRequirePlugin]`;
95
+ console[level](`${prefix} ${message}`);
96
+ }
97
+ }
98
+
99
+ apply(compiler) {
100
+ compiler.hooks.compilation.tap('AddNeoRequirePlugin', (compilation) => {
101
+ compilation.hooks.processAssets.tap(
102
+ {
103
+ name: 'AddNeoRequirePlugin',
104
+ stage: -100
105
+ },
106
+ (assets) => {
107
+ this.log('开始处理资源文件...');
108
+
109
+ let processedCount = 0;
110
+ let skippedCount = 0;
111
+ let errorCount = 0;
112
+
113
+ // 遍历所有资源文件
114
+ for (const [filename, asset] of Object.entries(assets)) {
115
+ try {
116
+ // 检查是否应该跳过此文件
117
+ if (this.shouldSkipFile(filename)) {
118
+ this.log(`跳过文件: ${filename} (匹配跳过规则)`);
119
+ skippedCount++;
120
+ continue;
121
+ }
122
+
123
+ // 获取文件内容
124
+ const source = asset.source();
125
+ const content = typeof source === 'string' ? source : source.toString();
126
+
127
+ // 验证文件内容
128
+ if (!this.isValidFile(content)) {
129
+ this.log(`跳过文件: ${filename} (内容无效)`, 'warn');
130
+ skippedCount++;
131
+ continue;
132
+ }
133
+
134
+ // 检查是否已经注入过
135
+ if (this.options.skipInjected && this.isAlreadyInjected(content)) {
136
+ this.log(`跳过文件: ${filename} (已注入过)`);
137
+ skippedCount++;
138
+ continue;
139
+ }
140
+
141
+ // 执行注入
142
+ this.injectNeoRequire(compilation, filename, content);
143
+ processedCount++;
144
+
145
+ this.log(`成功处理文件: ${filename}`);
146
+ } catch (error) {
147
+ errorCount++;
148
+ this.log(`处理文件失败: ${filename}, 错误: ${error.message}`, 'error');
149
+ console.error(`[AddNeoRequirePlugin] 详细错误:`, error);
150
+ }
151
+ }
152
+
153
+ // 输出处理统计
154
+ this.log(
155
+ `处理完成 - 成功: ${processedCount}, 跳过: ${skippedCount}, 错误: ${errorCount}`
156
+ );
157
+
158
+ if (errorCount > 0) {
159
+ this.log(`警告: 有 ${errorCount} 个文件处理失败`, 'warn');
160
+ }
161
+ }
162
+ );
163
+ });
164
+ }
165
+
166
+ /**
167
+ * 执行 neoRequire 注入
168
+ */
169
+ injectNeoRequire(compilation, filename, content) {
170
+ const Header = `
171
+ (function(NeoCustomCmpFileFactory) {
172
+ if (!window.neoRequire) {
173
+ throw new Error('neoRequire 不存在,请在 NeoCRM 平台中加载此脚本。');
174
+ }
175
+ NeoCustomCmpFileFactory(window.neoRequire);
176
+ })(function(require) {
177
+ `;
178
+
179
+ // const NeoUMDContent = fs.readFileSync(path.join(__dirname, '../neo/NeoUMDContent.js'), 'utf8');
180
+ // const Footer = `}); \n${NeoUMDContent}`;
181
+ const Footer = `});`;
182
+
183
+ // 创建新的资源
184
+ const newSource = new ConcatSource(Header, content, Footer);
185
+
186
+ // 更新资源
187
+ compilation.updateAsset(filename, newSource);
188
+ }
189
+ }
190
+
191
+ AddNeoRequirePlugin_1 = AddNeoRequirePlugin;
192
+ return AddNeoRequirePlugin_1;
193
+ }
194
+
195
+ exports.__require = requireAddNeoRequirePlugin;
@@ -0,0 +1,103 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ const require$$1 = require('node:path');
6
+ const require$$0 = require('node:fs');
7
+ const openProject = require('./projectUtils/openProject.js');
8
+
9
+ var autoEntryRootDir;
10
+ var hasRequiredAutoEntryRootDir;
11
+
12
+ function requireAutoEntryRootDir () {
13
+ if (hasRequiredAutoEntryRootDir) return autoEntryRootDir;
14
+ hasRequiredAutoEntryRootDir = 1;
15
+ const path = require$$1;
16
+ const fs = require$$0;
17
+ const openProject$1 = openProject.__require();
18
+
19
+ /**
20
+ * 自动进入指定项目的根目录
21
+ *
22
+ * ⚠️ 重要说明:子进程无法直接改变父 shell 的工作目录
23
+ *
24
+ * 原因:
25
+ * 1. 进程隔离:每个进程都有独立的工作目录,子进程无法修改父进程的工作目录
26
+ * 2. Shell 限制:cd 是 shell 的内置命令,必须在父 shell 进程中执行
27
+ * 3. 安全机制:操作系统禁止子进程修改父进程的工作目录,防止安全问题
28
+ *
29
+ * 解决方案:
30
+ * 1. 使用 process.chdir() 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
31
+ * 2. 输出 shell 命令让用户执行以改变 shell 的工作目录
32
+ *
33
+ * @param {string} projectPath 项目路径(可以是相对路径或绝对路径)
34
+ * @param {object} options 配置选项
35
+ * @param {boolean} options.outputShellCommand 是否输出 shell 命令让用户执行(默认:true)
36
+ * @returns {boolean} 是否成功切换目录
37
+ */
38
+ autoEntryRootDir = function (projectPath, options = {}) {
39
+ const { outputShellCommand = true } = options;
40
+
41
+ if (!projectPath) {
42
+ console.error('运行异常:未找到可用的项目路径。');
43
+ return false;
44
+ }
45
+
46
+ // 将路径解析为绝对路径
47
+ const absolutePath = path.isAbsolute(projectPath)
48
+ ? projectPath
49
+ : path.resolve(process.cwd(), projectPath);
50
+
51
+ // 检查目录是否存在
52
+ if (!fs.existsSync(absolutePath)) {
53
+ console.error(`运行异常:${projectPath} 目录不存在。`);
54
+ return false;
55
+ }
56
+
57
+ // 检查是否为目录
58
+ const stats = fs.statSync(absolutePath);
59
+ if (!stats.isDirectory()) {
60
+ console.error(`运行异常:${projectPath} 不是目录。`);
61
+ return false;
62
+ }
63
+
64
+ const projectName = path.relative(process.cwd(), absolutePath);
65
+
66
+ let success = false;
67
+
68
+ // 改变 Node.js 进程的工作目录(仅对后续 Node.js 操作有效)
69
+ try {
70
+ process.chdir(absolutePath);
71
+ success = true;
72
+ } catch (error) {
73
+ console.error(`错误:无法切换到目录 ${projectName}: ${error.message}`);
74
+ return false;
75
+ }
76
+
77
+ // 输出 shell 命令让用户执行(这样可以真正改变 shell 的工作目录)
78
+ if (outputShellCommand) {
79
+ // 输出提示信息
80
+ console.log('\n' + '='.repeat(60));
81
+ console.log('💡 提示:要切换到新建项目目录,请执行以下命令:');
82
+ console.log(`\n cd ${projectName} \n`);
83
+ console.log('='.repeat(60));
84
+ console.log('\n📝 说明1:组件开发工具(neo-cmp-cli)无法直接改变当前命令窗口工作目录,');
85
+ console.log(` 您需要手动执行上述命令才能进入刚创建的自定义组件项目(${projectName})。`);
86
+ console.log(
87
+ `\n📝 说明2:组件开发工具(neo-cmp-cli)默认自动打开编辑器(cursor 或 vscode): neo open -n ${projectName}。\n`
88
+ );
89
+
90
+ // 自动打开 IDE编辑器(异步执行,不需要等待)
91
+ openProject$1('auto', projectPath).catch((error) => {
92
+ // 错误已在 openProject 内部处理,这里只是防止未处理的 Promise 警告
93
+ console.error(`自动打开编辑器出错(neo open -n ${projectName}): ${error.message}`);
94
+ console.error(`请手动执行命令 neo open -n ${projectName} 打开编辑器。`);
95
+ });
96
+ }
97
+
98
+ return success;
99
+ };
100
+ return autoEntryRootDir;
101
+ }
102
+
103
+ exports.__require = requireAutoEntryRootDir;
@@ -0,0 +1,82 @@
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$$2 = require('lodash');
8
+ const neoParams = require('../neoParams.js');
9
+ const replaceInFilesByMap = require('../replaceInFilesByMap.js');
10
+ const hasCmpTypeByDir = require('./hasCmpTypeByDir.js');
11
+ const hasNeoProject = require('../projectUtils/hasNeoProject.js');
12
+
13
+ var createCmpByTemplate;
14
+ var hasRequiredCreateCmpByTemplate;
15
+
16
+ function requireCreateCmpByTemplate () {
17
+ if (hasRequiredCreateCmpByTemplate) return createCmpByTemplate;
18
+ hasRequiredCreateCmpByTemplate = 1;
19
+ const fs = require$$0;
20
+ const path = require$$1;
21
+ const _ = require$$2;
22
+ const { consoleTag } = neoParams.__require(); // 输出标记
23
+ const replaceInFilesByMap$1 = replaceInFilesByMap.__require();
24
+ const hasCmpTypeByDir$1 = hasCmpTypeByDir.__require();
25
+ const hasNeoProject$1 = hasNeoProject.__require();
26
+
27
+ // 自定义组件内容模板信息
28
+ const curCmpTemplate = {
29
+ // 需要替换掉的字段信息(模板中的字段)
30
+ widgetInfo: {
31
+ cmpName: 'CustomCmp',
32
+ modelName: 'CmpModel',
33
+ cmpClassName: 'custom-cmp-container',
34
+ cmpType: 'xx-custom-cmp',
35
+ cmpLabel: 'xx组件'
36
+ },
37
+ dir: path.resolve(__dirname, '../../../template/empty-cmp')
38
+ };
39
+
40
+ /**
41
+ * 创建自定义组件
42
+ * @param {*} cmpName 自定义组件名称
43
+ */
44
+ createCmpByTemplate = function (cmpName, componentBaseDir = './src/components') {
45
+ const currentTemplateDir = curCmpTemplate.dir;
46
+ const finalCmpName = cmpName || 'neoCustomCmp';
47
+ const finalCmpPath = path.resolve(process.cwd(), componentBaseDir, finalCmpName);
48
+
49
+ if (!hasNeoProject$1()) {
50
+ console.error(
51
+ `${consoleTag}当前(${process.cwd()})还不是自定义组件项目,请先创建一个自定义组件项目(neo init / neo create project)。`
52
+ );
53
+ process.exit(1);
54
+ }
55
+
56
+ if (hasCmpTypeByDir$1(finalCmpName)) {
57
+ console.error(`${consoleTag}创建自定义组件失败,当前项目已经存在${finalCmpName}自定义组件。`);
58
+ process.exit(1);
59
+ }
60
+
61
+ fs.copy(currentTemplateDir, finalCmpPath)
62
+ .then(() => {
63
+ const curCmpName = _.camelCase(finalCmpName);
64
+ const cmpType = _.kebabCase(finalCmpName);
65
+
66
+ // 替换文件中的内容
67
+ replaceInFilesByMap$1(finalCmpPath, {
68
+ [curCmpTemplate.widgetInfo.modelName]: `${curCmpName}Model`,
69
+ [curCmpTemplate.widgetInfo.cmpName]: curCmpName,
70
+ [curCmpTemplate.widgetInfo.cmpClassName]: `${cmpType}-container`,
71
+ [curCmpTemplate.widgetInfo.cmpType]: cmpType,
72
+ [curCmpTemplate.widgetInfo.cmpLabel]: `${cmpType}组件`
73
+ });
74
+
75
+ console.log(`${consoleTag}已创建自定义组件(${finalCmpName})!`);
76
+ })
77
+ .catch((err) => console.error(`${consoleTag}自定义组件创建失败(${finalCmpName}):`, err));
78
+ };
79
+ return createCmpByTemplate;
80
+ }
81
+
82
+ exports.__require = requireCreateCmpByTemplate;