neo-cmp-cli 1.5.5 → 1.5.6

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
@@ -37,6 +37,9 @@ neo init -t=react-ts
37
37
  # 创建自定义组件项目
38
38
  neo createProject
39
39
 
40
+ # 也可以根据模板创建自定义组件项目
41
+ neo init
42
+
40
43
  # 在当前项目中创建一个自定义组件
41
44
  neo createCmp
42
45
 
@@ -79,10 +82,12 @@ npm run pushCmp
79
82
 
80
83
  ## 常用命令说明
81
84
  - **neo init**: 交互式创建自定义组件(支持 -t、--name)。
82
- - **neo preview**: 本地预览自定义组件内容,默认支持热更新与接口代理。
85
+ - **neo createProject**: 创建自定义组件项目(支持 --name)。
86
+ - **neo createCmp**: 在当前项目中创建一个自定义组件(支持 --name)。
87
+ - **neo preview**: 本地预览自定义组件内容(支持 --name),默认支持热更新与接口代理。
83
88
  - **neo linkDebug**: 外链调试模式,在平台端页面设计器中调试自定义组件。
84
- - **neo publish2oss**: 构建并上传到对象存储(可自定义配置对象存储)。
85
- - **neo pushCmp**: 构建并发布到NeoCRM平台(需自行添加授权配置)。
89
+ - **neo publish2oss**: 构建并上传到对象存储(支持 --name,可自定义配置对象存储)。
90
+ - **neo pushCmp**: 构建并发布到NeoCRM平台(支持 --name,需自行添加授权配置)。
86
91
 
87
92
  ## 开发须知
88
93
  #### 1)默认自动识别自定义组件
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "neo-cmp-cli",
3
- "version": "1.5.5",
3
+ "version": "1.5.6",
4
4
  "description": "前端脚手架:自定义组件开发工具,支持react 和 vue2.0技术栈。",
5
5
  "keywords": [
6
6
  "neo-cli",
@@ -1,48 +1,17 @@
1
1
  const fs = require('fs');
2
2
  const path = require('path');
3
- const AdmZip = require('adm-zip');
4
3
  const _ = require('lodash');
5
4
  const { catchCurPackageJson } = require('../utils/pathUtils');
6
5
  const getConfigObj = require('../utils/getConfigObj');
7
6
  const ora = require('ora');
8
7
  const NeoService = require('../neo/neoService');
9
8
  const { consoleTag } = require('../utils/neoParams');
9
+ const createCmpProjectZip = require('../projectUtils/createCmpProjectZip');
10
10
 
11
11
  // 获取当前项目的package文件
12
12
  const currentPackageJsonDir = catchCurPackageJson();
13
13
  const currentPackageJson = getConfigObj(currentPackageJsonDir);
14
14
 
15
- /**
16
- * 将构建产物打包成 zip 文件
17
- * @param {string} assetsRoot 构建产物的目录
18
- * @returns {Promise<string>} zip 文件路径
19
- */
20
- const createZipPackage = async (assetsRoot) => {
21
- if (!fs.existsSync(assetsRoot)) {
22
- throw new Error(`assetsRoot 不存在: ${assetsRoot}`);
23
- }
24
-
25
- const files = fs.readdirSync(assetsRoot);
26
- const zip = new AdmZip();
27
-
28
- files.forEach((file) => {
29
- const filePath = path.join(assetsRoot, file);
30
- const fileStat = fs.statSync(filePath);
31
- if (fileStat.isFile()) {
32
- // 只添加 .js 文件
33
- if (file.endsWith('.js')) {
34
- zip.addLocalFile(filePath);
35
- }
36
- }
37
- });
38
-
39
- const zipPath = path.join(assetsRoot, `${currentPackageJson.name}.zip`);
40
- zip.writeZip(zipPath);
41
- console.info(`已创建 zip 文件: ${zipPath}`);
42
-
43
- return zipPath;
44
- };
45
-
46
15
  /**
47
16
  * 获取技术栈标识
48
17
  * 目的:兼容用户非标准写法
@@ -202,24 +171,22 @@ const pushCmp = async (config, cmpType) => {
202
171
  spinner.text = '发布自定义组件:初始化 NeoService...';
203
172
  const neoService = new NeoService(config);
204
173
 
205
- // 步骤 2: 上传构建后资源文件
174
+ // 步骤 2: 打包源码文件(打包单个自定义组件源码)
175
+ spinner.text = '发布自定义组件:打包源码文件(含单个自定义组件源码)...';
176
+ createCmpProjectZip(cmpType, process.cwd(), config.assetsRoot);
177
+
178
+ // 步骤 3: 上传构建后资源文件
206
179
  spinner.text = '发布自定义组件:上传自定义组件构建产物到 OSS...';
207
180
  const cmpInfo = await neoService.publish2oss(cmpType);
208
181
 
209
- /*
210
- // 步骤 3: 打包文件(打包单个自定义组件源码)
211
- spinner.text = '发布自定义组件:打包文件(打包单个自定义组件源码)...';
212
- const zipPath = await createZipPackage(assetsRoot);
213
- */
214
-
215
- // 步骤 4: 构建组件数据
182
+ // 步骤 5: 构建组件数据
216
183
  spinner.text = '发布自定义组件:构建组件数据...';
217
184
  const componentInfo = await buildComponentData(config.assetsRoot, cmpInfo);
218
185
  if (!componentInfo) {
219
186
  throw new Error(`构建组件数据失败,未获取到自定义组件模型信息。(${cmpType})`);
220
187
  }
221
188
 
222
- // 步骤 5: 保存组件信息
189
+ // 步骤 6: 保存组件信息
223
190
  spinner.text = '发布自定义组件:保存组件信息...';
224
191
  await neoService.updateCustomComponent(componentInfo);
225
192
 
@@ -334,9 +334,9 @@ class NeoService {
334
334
  * 将构建产物上传到 NeoCRM 平台端
335
335
  *
336
336
  * @param {object} cmpType 自定义组件名称
337
- * @param {array} fileExtensions 需要上传的文件类型,默认 ['.js', '.css']
337
+ * @param {array} fileExtensions 需要上传的文件类型,默认 ['.js', '.css', '.zip']
338
338
  */
339
- async publish2oss(cmpType, fileExtensions = ['.js', '.css']) {
339
+ async publish2oss(cmpType, fileExtensions = ['.js', '.css', '.zip']) {
340
340
  if (!cmpType) {
341
341
  console.error(`自定义组件名称不能为空: ${cmpType}`);
342
342
  return;
@@ -375,6 +375,8 @@ class NeoService {
375
375
  curCmpInfo.modelAsset = fileUrl;
376
376
  } else if (file.endsWith('.css')) {
377
377
  curCmpInfo.cssAsset = fileUrl;
378
+ } else if (file.endsWith('.zip')) {
379
+ curCmpInfo.codeLib = fileUrl;
378
380
  } else {
379
381
  curCmpInfo.asset = fileUrl;
380
382
  }
@@ -0,0 +1,122 @@
1
+ const fs = require('fs-extra');
2
+ const path = require('path');
3
+ const AdmZip = require('adm-zip');
4
+ const _ = require('lodash');
5
+ const { consoleTag } = require('../utils/neoParams'); // 输出标记
6
+ const hasNeoProject = require('./hasNeoProject');
7
+
8
+ /**
9
+ * 创建自定义组件源码 zip 包
10
+ * @param {*} cmpType 自定义组件名称
11
+ * @returns {string} zip 文件路径
12
+ */
13
+ module.exports = function (cmpType, _projectPath, assetsRoot) {
14
+ const projectRoot = _projectPath || process.cwd();
15
+ const finalAssetsRoot = assetsRoot || path.join(projectRoot, 'dist');
16
+
17
+ if (!hasNeoProject(_projectPath)) {
18
+ console.error(`${consoleTag}当前目录不是自定义组件项目,请在自定义组件项目目录下执行。`);
19
+ process.exit(1);
20
+ }
21
+
22
+ if (!cmpType) {
23
+ console.error(`${consoleTag}自定义组件名称不能为空`);
24
+ process.exit(1);
25
+ }
26
+
27
+ const zip = new AdmZip();
28
+
29
+ // 需要排除的目录和文件
30
+ const excludeDirs = ['node_modules', '.neo-cli', 'dist'];
31
+ const excludeFiles = ['.eslintcache', 'auth.config.js'];
32
+
33
+ /**
34
+ * 判断文件/目录是否应该被排除
35
+ * @param {string} filePath 文件路径
36
+ * @param {string} relativePath 相对路径
37
+ * @returns {boolean} 是否应该排除
38
+ */
39
+ const shouldExclude = (filePath, relativePath) => {
40
+ const name = path.basename(filePath);
41
+ const stat = fs.statSync(filePath);
42
+
43
+ // 排除指定的目录
44
+ if (stat.isDirectory() && excludeDirs.includes(name)) {
45
+ return true;
46
+ }
47
+
48
+ // 排除指定的文件
49
+ if (stat.isFile() && excludeFiles.includes(name)) {
50
+ return true;
51
+ }
52
+
53
+ // 对于 src/components 目录,只包含 cmpType 子目录
54
+ // 处理逻辑:如果路径是 src/components/xxx,且 xxx 不是 cmpType,则排除
55
+ if (relativePath.startsWith('src/components')) {
56
+ const relativeToComponents = path.relative('src/components', relativePath);
57
+ // 如果相对路径不为空,说明已经在 components 的子目录中
58
+ if (relativeToComponents && relativeToComponents !== '.') {
59
+ const firstPart = relativeToComponents.split(path.sep)[0];
60
+ // 如果第一个部分不是 cmpType,则排除
61
+ if (firstPart !== cmpType) {
62
+ return true;
63
+ }
64
+ }
65
+ }
66
+
67
+ return false;
68
+ };
69
+
70
+ /**
71
+ * 递归遍历目录并添加到 zip
72
+ * @param {string} dirPath 目录路径
73
+ * @param {string} relativePath 相对路径(用于 zip 中的路径)
74
+ */
75
+ const addDirectoryToZip = (dirPath, relativePath = '') => {
76
+ try {
77
+ const items = fs.readdirSync(dirPath);
78
+
79
+ items.forEach((item) => {
80
+ const itemPath = path.join(dirPath, item);
81
+ const itemRelativePath = relativePath ? path.join(relativePath, item) : item;
82
+
83
+ // 检查是否应该排除
84
+ if (shouldExclude(itemPath, itemRelativePath)) {
85
+ return;
86
+ }
87
+
88
+ const stat = fs.statSync(itemPath);
89
+
90
+ if (stat.isDirectory()) {
91
+ // 递归处理子目录
92
+ addDirectoryToZip(itemPath, itemRelativePath);
93
+ } else if (stat.isFile()) {
94
+ // 添加文件到 zip,保持相对路径结构
95
+ const fileContent = fs.readFileSync(itemPath);
96
+ zip.addFile(itemRelativePath, fileContent);
97
+ }
98
+ });
99
+ } catch (error) {
100
+ console.error(`${consoleTag}遍历目录失败 (${dirPath}):`, error.message);
101
+ }
102
+ };
103
+
104
+ // 开始遍历项目根目录
105
+ addDirectoryToZip(projectRoot);
106
+
107
+ // 生成 zip 文件名
108
+ const zipFileName = `${_.camelCase(cmpType)}Source.zip`;
109
+ const zipPath = path.join(finalAssetsRoot, zipFileName);
110
+
111
+ // 如果已存在同名 zip 文件,先删除
112
+ if (fs.existsSync(zipPath)) {
113
+ fs.removeSync(zipPath);
114
+ }
115
+
116
+ // 写入 zip 文件
117
+ zip.writeZip(zipPath);
118
+
119
+ console.log(`${consoleTag}已创建自定义组件源码 zip 包: ${zipFileName}`);
120
+
121
+ return zipPath;
122
+ };
@@ -5,9 +5,10 @@ const path = require('path');
5
5
  * 判断当前是否存在 neo 项目
6
6
  * 备注:neo.config.js 和 package.json 必须同时存在才算作 neo 项目
7
7
  */
8
- module.exports = function () {
9
- const neoConfigPath = path.resolve(process.cwd(), 'neo.config.js');
10
- const packagePath = path.resolve(process.cwd(), 'package.json');
8
+ module.exports = function (_projectPath) {
9
+ const projectPath = _projectPath || process.cwd();
10
+ const neoConfigPath = path.resolve(projectPath, 'neo.config.js');
11
+ const packagePath = path.resolve(projectPath, 'package.json');
11
12
 
12
13
  if (fs.existsSync(neoConfigPath) && fs.existsSync(packagePath)) {
13
14
  return true;
@@ -47,7 +47,7 @@
47
47
  "@commitlint/config-conventional": "^9.1.1",
48
48
  "@types/react": "^16.9.11",
49
49
  "@types/react-dom": "^16.9.15",
50
- "neo-cmp-cli": "^1.5.5",
50
+ "neo-cmp-cli": "^1.5.6",
51
51
  "husky": "^4.2.5",
52
52
  "lint-staged": "^10.2.9",
53
53
  "prettier": "^2.0.5"
@@ -47,7 +47,7 @@
47
47
  "@commitlint/config-conventional": "^9.1.1",
48
48
  "@types/react": "^16.9.11",
49
49
  "@types/react-dom": "^16.9.15",
50
- "neo-cmp-cli": "^1.5.5",
50
+ "neo-cmp-cli": "^1.5.6",
51
51
  "husky": "^4.2.5",
52
52
  "lint-staged": "^10.2.9",
53
53
  "prettier": "^2.0.5",
@@ -45,7 +45,7 @@
45
45
  "@commitlint/config-conventional": "^9.1.1",
46
46
  "@types/react": "^16.9.11",
47
47
  "@types/react-dom": "^16.9.15",
48
- "neo-cmp-cli": "^1.5.5",
48
+ "neo-cmp-cli": "^1.5.6",
49
49
  "husky": "^4.2.5",
50
50
  "lint-staged": "^10.2.9",
51
51
  "prettier": "^2.0.5"
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@commitlint/cli": "^8.3.5",
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
- "neo-cmp-cli": "^1.5.5",
47
+ "neo-cmp-cli": "^1.5.6",
48
48
  "husky": "^4.2.5",
49
49
  "lint-staged": "^10.2.9",
50
50
  "prettier": "^2.0.5"
@@ -46,7 +46,7 @@
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
47
  "@types/react": "^16.9.11",
48
48
  "@types/react-dom": "^16.9.15",
49
- "neo-cmp-cli": "^1.5.5",
49
+ "neo-cmp-cli": "^1.5.6",
50
50
  "husky": "^4.2.5",
51
51
  "lint-staged": "^10.2.9",
52
52
  "prettier": "^2.0.5"
@@ -44,7 +44,7 @@
44
44
  "devDependencies": {
45
45
  "@commitlint/cli": "^8.3.5",
46
46
  "@commitlint/config-conventional": "^9.1.1",
47
- "neo-cmp-cli": "^1.5.5",
47
+ "neo-cmp-cli": "^1.5.6",
48
48
  "husky": "^4.2.5",
49
49
  "lint-staged": "^10.2.9",
50
50
  "prettier": "^2.0.5",