cocos2d-cli 1.6.3 → 1.6.5

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 (42) hide show
  1. package/bin/cocos2d-cli.js +152 -152
  2. package/data/script_map.json +25 -25
  3. package/package.json +33 -33
  4. package/src/commands/add-component.js +112 -112
  5. package/src/commands/add.js +177 -177
  6. package/src/commands/build.js +78 -78
  7. package/src/commands/create-scene.js +181 -181
  8. package/src/commands/get.js +108 -108
  9. package/src/commands/prefab-create.js +110 -110
  10. package/src/commands/remove-component.js +110 -110
  11. package/src/commands/remove.js +99 -99
  12. package/src/commands/screenshot.js +108 -103
  13. package/src/commands/set-component.js +119 -119
  14. package/src/commands/set.js +107 -107
  15. package/src/commands/tree.js +28 -28
  16. package/src/lib/cc/CCButton.js +122 -122
  17. package/src/lib/cc/CCCamera.js +93 -93
  18. package/src/lib/cc/CCCanvas.js +54 -54
  19. package/src/lib/cc/CCColor.js +32 -32
  20. package/src/lib/cc/CCComponent.js +60 -60
  21. package/src/lib/cc/CCLabel.js +146 -146
  22. package/src/lib/cc/CCNode.js +255 -255
  23. package/src/lib/cc/CCObject.js +23 -23
  24. package/src/lib/cc/CCPrefab.js +242 -242
  25. package/src/lib/cc/CCRect.js +32 -32
  26. package/src/lib/cc/CCRichText.js +44 -44
  27. package/src/lib/cc/CCScene.js +42 -42
  28. package/src/lib/cc/CCSceneAsset.js +302 -302
  29. package/src/lib/cc/CCSize.js +26 -26
  30. package/src/lib/cc/CCSprite.js +94 -94
  31. package/src/lib/cc/CCTrs.js +74 -74
  32. package/src/lib/cc/CCVec2.js +26 -26
  33. package/src/lib/cc/CCVec3.js +29 -29
  34. package/src/lib/cc/CCWidget.js +98 -98
  35. package/src/lib/cc/index.js +42 -42
  36. package/src/lib/fire-utils.js +373 -373
  37. package/src/lib/json-parser.js +185 -185
  38. package/src/lib/node-utils.js +395 -395
  39. package/src/lib/screenshot/index.html +29 -29
  40. package/src/lib/screenshot-core.js +285 -286
  41. package/src/lib/templates.js +49 -49
  42. package/src/lib/utils.js +202 -202
@@ -1,152 +1,152 @@
1
- #!/usr/bin/env node
2
- /**
3
- * Cocos Creator CLI
4
- * Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes
5
- */
6
-
7
- const path = require('path');
8
-
9
- // 命令映射
10
- const commands = {
11
- tree: '../src/commands/tree',
12
- get: '../src/commands/get',
13
- set: '../src/commands/set',
14
- 'set-component': '../src/commands/set-component',
15
- add: '../src/commands/add',
16
- 'add-component': '../src/commands/add-component',
17
- 'remove-component': '../src/commands/remove-component',
18
- 'remove': '../src/commands/remove',
19
- build: '../src/commands/build',
20
- 'create-prefab': '../src/commands/prefab-create',
21
- 'create-scene': '../src/commands/create-scene',
22
- 'screenshot': '../src/commands/screenshot'
23
- };
24
-
25
- // 帮助信息
26
- function showHelp() {
27
- console.log(`
28
- Cocos Creator CLI - 场景/预制体操作工具集
29
-
30
- 用法:
31
- cocos2d-cli <command> [options]
32
-
33
- 命令:
34
- tree <场景.fire | 预制体.prefab> 查看节点树
35
- get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
36
- set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
37
- set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
38
- add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
39
- add-component <文件> <节点路径> <类型> 给节点添加组件
40
- remove-component <文件> <节点路径> <类型> 删除节点组件
41
- remove <文件> <节点路径> 删除节点
42
- build <项目目录> 构建组件映射
43
- create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
44
- create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
45
- screenshot <json文件> [选项] 渲染JSON并截图
46
-
47
- 节点路径格式:
48
- Canvas - 根节点下的 Canvas
49
- Canvas/MidNode - Canvas 下的 MidNode
50
- Canvas/GameScene/NodeA - 多层嵌套路径
51
-
52
- set 支持的节点属性:
53
- name 节点名称
54
- active 激活状态 (true/false)
55
- x X 坐标
56
- y Y 坐标
57
- width 宽度
58
- height 高度
59
- anchorX 锚点 X (0-1)
60
- anchorY 锚点 Y (0-1)
61
- opacity 透明度 (0-255)
62
- scaleX X 缩放
63
- scaleY Y 缩放
64
- rotation 旋转角度
65
-
66
- set-component 示例:
67
- cocos2d-cli set-component xxx.fire Canvas/Label Label string "Hello"
68
- cocos2d-cli set-component xxx.fire Canvas/Label Label fontSize 32
69
- cocos2d-cli set-component xxx.fire Canvas/Sprite Sprite sizeMode 0
70
-
71
- add 支持的选项:
72
- --x=<数值> 设置 X 坐标
73
- --y=<数值> 设置 Y 坐标
74
- --width=<数值> 设置宽度
75
- --height=<数值> 设置高度
76
- --scaleX=<数值> 设置 X 缩放
77
- --scaleY=<数值> 设置 Y 缩放
78
- --rotation=<角度> 设置旋转角度
79
- --active=true/false 设置激活状态
80
- --type=sprite/label/button 添加节点时指定组件类型
81
- --string=<文字> Label 文字内容 (配合 --type=label)
82
- --fontSize=<数值> Label 字体大小 (配合 --type=label)
83
-
84
- JSON 格式:
85
- 详见 SKILL.md 完整文档。简要示例:
86
- {
87
- "name": "Node",
88
- "width": 400, "height": 300,
89
- "x": 0, "y": 0,
90
- "anchorX": 0, "color": "#336699",
91
- "components": [
92
- { "type": "label", "string": "Hello", "horizontalAlign": "left" },
93
- { "type": "richText", "string": "<color=#3cb034>绿色</color>" }
94
- ],
95
- "children": [...]
96
- }
97
-
98
- 提示:配合 anchorX + horizontalAlign 实现靠左/靠右布局(见 SKILL.md)
99
-
100
- 示例:
101
- cocos2d-cli tree assets/main.fire
102
- cocos2d-cli get assets/main.fire Canvas/MidNode
103
- cocos2d-cli get assets/main.fire Canvas/MidNode x
104
- cocos2d-cli get assets/main.fire Canvas/MidNode Label
105
- cocos2d-cli set assets/main.fire Canvas/MidNode x 100
106
- cocos2d-cli set assets/main.fire Canvas/MidNode width 200
107
- cocos2d-cli set-component assets/main.fire Canvas/Label Label string "Hello World"
108
- cocos2d-cli add assets/main.fire Canvas NewSprite --type=sprite --x=100
109
- cocos2d-cli add-component assets/main.fire Canvas/MidNode label
110
- cocos2d-cli remove-component assets/main.fire Canvas/MidNode label
111
- cocos2d-cli remove assets/main.fire Canvas/MidNode
112
-
113
- # 从 JSON 文件创建场景
114
- cocos2d-cli create-scene scene.json assets/scene.fire
115
-
116
- # 从 JSON 文件创建预制体
117
- cocos2d-cli create-prefab panel.json assets/panel.prefab
118
-
119
- # 创建默认预制体(不传JSON)
120
- cocos2d-cli create-prefab assets/NewNode.prefab
121
-
122
- # 截图
123
- cocos2d-cli screenshot data.json
124
- cocos2d-cli screenshot data.json -o ./screenshots --width 1080 --height 1920
125
- `);
126
- }
127
-
128
- // 解析参数
129
- const args = process.argv.slice(2);
130
-
131
- if (args.length === 0 || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
132
- showHelp();
133
- process.exit(0);
134
- }
135
-
136
- const commandName = args[0];
137
- const commandPath = commands[commandName];
138
-
139
- if (!commandPath) {
140
- console.error(`未知命令: ${commandName}`);
141
- console.error('运行 cocos2d-cli --help 查看可用命令');
142
- process.exit(1);
143
- }
144
-
145
- // 加载并执行命令
146
- try {
147
- const command = require(commandPath);
148
- command.run(args.slice(1));
149
- } catch (err) {
150
- console.error(`命令执行失败: ${err.message}`);
151
- process.exit(1);
152
- }
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Cocos Creator CLI
4
+ * Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes
5
+ */
6
+
7
+ const path = require('path');
8
+
9
+ // 命令映射
10
+ const commands = {
11
+ tree: '../src/commands/tree',
12
+ get: '../src/commands/get',
13
+ set: '../src/commands/set',
14
+ 'set-component': '../src/commands/set-component',
15
+ add: '../src/commands/add',
16
+ 'add-component': '../src/commands/add-component',
17
+ 'remove-component': '../src/commands/remove-component',
18
+ 'remove': '../src/commands/remove',
19
+ build: '../src/commands/build',
20
+ 'create-prefab': '../src/commands/prefab-create',
21
+ 'create-scene': '../src/commands/create-scene',
22
+ 'screenshot': '../src/commands/screenshot'
23
+ };
24
+
25
+ // 帮助信息
26
+ function showHelp() {
27
+ console.log(`
28
+ Cocos Creator CLI - 场景/预制体操作工具集
29
+
30
+ 用法:
31
+ cocos2d-cli <command> [options]
32
+
33
+ 命令:
34
+ tree <场景.fire | 预制体.prefab> 查看节点树
35
+ get <场景.fire | 预制体.prefab> <节点路径> [属性名|组件类型] 获取节点或组件属性
36
+ set <场景.fire | 预制体.prefab> <节点路径> <属性名> <值> 修改节点属性
37
+ set-component <文件> <节点路径> <组件类型> <属性名> <值> 修改组件属性
38
+ add <场景.fire | 预制体.prefab> <父节点路径> <名称> 添加节点
39
+ add-component <文件> <节点路径> <类型> 给节点添加组件
40
+ remove-component <文件> <节点路径> <类型> 删除节点组件
41
+ remove <文件> <节点路径> 删除节点
42
+ build <项目目录> 构建组件映射
43
+ create-prefab [JSON文件路径] <输出.prefab> 创建预制体(不传JSON则创建默认)
44
+ create-scene [JSON文件路径] <输出.fire> 创建场景(不传JSON则创建默认)
45
+ screenshot <json文件> [选项] 渲染JSON并截图
46
+
47
+ 节点路径格式:
48
+ Canvas - 根节点下的 Canvas
49
+ Canvas/MidNode - Canvas 下的 MidNode
50
+ Canvas/GameScene/NodeA - 多层嵌套路径
51
+
52
+ set 支持的节点属性:
53
+ name 节点名称
54
+ active 激活状态 (true/false)
55
+ x X 坐标
56
+ y Y 坐标
57
+ width 宽度
58
+ height 高度
59
+ anchorX 锚点 X (0-1)
60
+ anchorY 锚点 Y (0-1)
61
+ opacity 透明度 (0-255)
62
+ scaleX X 缩放
63
+ scaleY Y 缩放
64
+ rotation 旋转角度
65
+
66
+ set-component 示例:
67
+ cocos2d-cli set-component xxx.fire Canvas/Label Label string "Hello"
68
+ cocos2d-cli set-component xxx.fire Canvas/Label Label fontSize 32
69
+ cocos2d-cli set-component xxx.fire Canvas/Sprite Sprite sizeMode 0
70
+
71
+ add 支持的选项:
72
+ --x=<数值> 设置 X 坐标
73
+ --y=<数值> 设置 Y 坐标
74
+ --width=<数值> 设置宽度
75
+ --height=<数值> 设置高度
76
+ --scaleX=<数值> 设置 X 缩放
77
+ --scaleY=<数值> 设置 Y 缩放
78
+ --rotation=<角度> 设置旋转角度
79
+ --active=true/false 设置激活状态
80
+ --type=sprite/label/button 添加节点时指定组件类型
81
+ --string=<文字> Label 文字内容 (配合 --type=label)
82
+ --fontSize=<数值> Label 字体大小 (配合 --type=label)
83
+
84
+ JSON 格式:
85
+ 详见 SKILL.md 完整文档。简要示例:
86
+ {
87
+ "name": "Node",
88
+ "width": 400, "height": 300,
89
+ "x": 0, "y": 0,
90
+ "anchorX": 0, "color": "#336699",
91
+ "components": [
92
+ { "type": "label", "string": "Hello", "horizontalAlign": "left" },
93
+ { "type": "richText", "string": "<color=#3cb034>绿色</color>" }
94
+ ],
95
+ "children": [...]
96
+ }
97
+
98
+ 提示:配合 anchorX + horizontalAlign 实现靠左/靠右布局(见 SKILL.md)
99
+
100
+ 示例:
101
+ cocos2d-cli tree assets/main.fire
102
+ cocos2d-cli get assets/main.fire Canvas/MidNode
103
+ cocos2d-cli get assets/main.fire Canvas/MidNode x
104
+ cocos2d-cli get assets/main.fire Canvas/MidNode Label
105
+ cocos2d-cli set assets/main.fire Canvas/MidNode x 100
106
+ cocos2d-cli set assets/main.fire Canvas/MidNode width 200
107
+ cocos2d-cli set-component assets/main.fire Canvas/Label Label string "Hello World"
108
+ cocos2d-cli add assets/main.fire Canvas NewSprite --type=sprite --x=100
109
+ cocos2d-cli add-component assets/main.fire Canvas/MidNode label
110
+ cocos2d-cli remove-component assets/main.fire Canvas/MidNode label
111
+ cocos2d-cli remove assets/main.fire Canvas/MidNode
112
+
113
+ # 从 JSON 文件创建场景
114
+ cocos2d-cli create-scene scene.json assets/scene.fire
115
+
116
+ # 从 JSON 文件创建预制体
117
+ cocos2d-cli create-prefab panel.json assets/panel.prefab
118
+
119
+ # 创建默认预制体(不传JSON)
120
+ cocos2d-cli create-prefab assets/NewNode.prefab
121
+
122
+ # 截图
123
+ cocos2d-cli screenshot data.json
124
+ cocos2d-cli screenshot data.json -o ./screenshots --width 1080 --height 1920
125
+ `);
126
+ }
127
+
128
+ // 解析参数
129
+ const args = process.argv.slice(2);
130
+
131
+ if (args.length === 0 || args[0] === '--help' || args[0] === '-h' || args[0] === 'help') {
132
+ showHelp();
133
+ process.exit(0);
134
+ }
135
+
136
+ const commandName = args[0];
137
+ const commandPath = commands[commandName];
138
+
139
+ if (!commandPath) {
140
+ console.error(`未知命令: ${commandName}`);
141
+ console.error('运行 cocos2d-cli --help 查看可用命令');
142
+ process.exit(1);
143
+ }
144
+
145
+ // 加载并执行命令
146
+ try {
147
+ const command = require(commandPath);
148
+ command.run(args.slice(1));
149
+ } catch (err) {
150
+ console.error(`命令执行失败: ${err.message}`);
151
+ process.exit(1);
152
+ }
@@ -1,25 +1,25 @@
1
- {
2
- "01e12Vix3xPZaV9rG8NNMhT": "CharacterAnimation",
3
- "0c1120T6UNNk5mllwLCpN0/": "GameScene",
4
- "0c5a3bepqdAUZOCSqDOe7tX": "StageManager",
5
- "166a2V/C3pMsLYQh5Cfhh0M": "Tile",
6
- "1777aIKiDVM2ayJgXQsFRbc": "InfoNode",
7
- "2000bQ16aVPOowsYmWzQHBl": "TilemapCamera",
8
- "25dfekurxJEpa4v3Hll7c7S": "PlayerController",
9
- "2a19dBOCe1GUpzKNfMsQQR7": "Pathfinder",
10
- "329ecFYmkpK+ZTeUQlUmyHq": "MapCharacter",
11
- "36c13QGxrxExoOv5anzjyRj": "EventManager",
12
- "394e4Gg6E1Dh6AedkHAFZH+": "index",
13
- "3cc7eQO1TZPTYTHr3v/2h1L": "SceneScript",
14
- "42cc8kWoTtFHqEMb8ClTUBk": "GameExpressionEvaluator",
15
- "7541bC/enRKeocYItojXgyK": "Gamepiece",
16
- "781adeySrdN27K4uANiWTCT": "MinimapRenderer",
17
- "9701aXPPNhO2IJ1bgr/25pJ": "PlayerManager",
18
- "a4e62KPZVNEbZO1mvkzyOQt": "FilmEffect",
19
- "a4ff8dbnCJNsLLBFaNYXrQp": "Player",
20
- "a89e2IVZx5Bn50oCifRT+GC": "Gameboard",
21
- "c4069o+d29NprrTzmS9V1Mb": "ResourceManager",
22
- "e0920R8VWpPOpxr0VnPlCqA": "GamepieceRegistry",
23
- "e24c1oXqIpLvqqgMFn+zl3A": "Tilemap",
24
- "f07feXxRJVMj7Vg6tTtIi+T": "index"
25
- }
1
+ {
2
+ "01e12Vix3xPZaV9rG8NNMhT": "CharacterAnimation",
3
+ "0c1120T6UNNk5mllwLCpN0/": "GameScene",
4
+ "0c5a3bepqdAUZOCSqDOe7tX": "StageManager",
5
+ "166a2V/C3pMsLYQh5Cfhh0M": "Tile",
6
+ "1777aIKiDVM2ayJgXQsFRbc": "InfoNode",
7
+ "2000bQ16aVPOowsYmWzQHBl": "TilemapCamera",
8
+ "25dfekurxJEpa4v3Hll7c7S": "PlayerController",
9
+ "2a19dBOCe1GUpzKNfMsQQR7": "Pathfinder",
10
+ "329ecFYmkpK+ZTeUQlUmyHq": "MapCharacter",
11
+ "36c13QGxrxExoOv5anzjyRj": "EventManager",
12
+ "394e4Gg6E1Dh6AedkHAFZH+": "index",
13
+ "3cc7eQO1TZPTYTHr3v/2h1L": "SceneScript",
14
+ "42cc8kWoTtFHqEMb8ClTUBk": "GameExpressionEvaluator",
15
+ "7541bC/enRKeocYItojXgyK": "Gamepiece",
16
+ "781adeySrdN27K4uANiWTCT": "MinimapRenderer",
17
+ "9701aXPPNhO2IJ1bgr/25pJ": "PlayerManager",
18
+ "a4e62KPZVNEbZO1mvkzyOQt": "FilmEffect",
19
+ "a4ff8dbnCJNsLLBFaNYXrQp": "Player",
20
+ "a89e2IVZx5Bn50oCifRT+GC": "Gameboard",
21
+ "c4069o+d29NprrTzmS9V1Mb": "ResourceManager",
22
+ "e0920R8VWpPOpxr0VnPlCqA": "GamepieceRegistry",
23
+ "e24c1oXqIpLvqqgMFn+zl3A": "Tilemap",
24
+ "f07feXxRJVMj7Vg6tTtIi+T": "index"
25
+ }
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "cocos2d-cli",
3
- "version": "1.6.3",
4
- "description": "Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes",
5
- "main": "bin/cocos2d-cli.js",
6
- "bin": {
7
- "cocos2d-cli": "bin/cocos2d-cli.js"
8
- },
9
- "files": [
10
- "bin",
11
- "src",
12
- "data"
13
- ],
14
- "scripts": {
15
- "test": "echo \"No tests specified\" && exit 1"
16
- },
17
- "keywords": [
18
- "cocos",
19
- "cocos-creator",
20
- "cli",
21
- "scene",
22
- "fire",
23
- "screenshot"
24
- ],
25
- "author": "",
26
- "license": "MIT",
27
- "engines": {
28
- "node": ">=14.0.0"
29
- },
30
- "dependencies": {
31
- "playwright": "^1.58.2"
32
- }
33
- }
1
+ {
2
+ "name": "cocos2d-cli",
3
+ "version": "1.6.5",
4
+ "description": "Command-line tools for AI to read and manipulate Cocos Creator 2.4.x project scenes",
5
+ "main": "bin/cocos2d-cli.js",
6
+ "bin": {
7
+ "cocos2d-cli": "bin/cocos2d-cli.js"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "src",
12
+ "data"
13
+ ],
14
+ "scripts": {
15
+ "test": "echo \"No tests specified\" && exit 1"
16
+ },
17
+ "keywords": [
18
+ "cocos",
19
+ "cocos-creator",
20
+ "cli",
21
+ "scene",
22
+ "fire",
23
+ "screenshot"
24
+ ],
25
+ "author": "",
26
+ "license": "MIT",
27
+ "engines": {
28
+ "node": ">=14.0.0"
29
+ },
30
+ "dependencies": {
31
+ "playwright": "^1.58.2"
32
+ }
33
+ }
@@ -1,112 +1,112 @@
1
- /**
2
- * add-component 命令 - 添加组件
3
- */
4
-
5
- const path = require('path');
6
- const fs = require('fs');
7
- const { CCSceneAsset, CCPrefab, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton, CCCamera } = require('../lib/cc');
8
- const { buildTree } = require('../lib/node-utils');
9
- const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
10
-
11
- /**
12
- * 创建组件
13
- */
14
- function createComponent(type) {
15
- switch (type.toLowerCase()) {
16
- case 'canvas': return new CCCanvas();
17
- case 'widget': return new CCWidget();
18
- case 'sprite': return new CCSprite();
19
- case 'label': return new CCLabel();
20
- case 'button': return new CCButton();
21
- case 'camera': return new CCCamera();
22
- default: return null;
23
- }
24
- }
25
-
26
- /**
27
- * 查找节点
28
- */
29
- function findNode(root, path) {
30
- if (!path) return root;
31
-
32
- const parts = path.split('/').filter(p => p);
33
- if (parts.length === 0) return root;
34
-
35
- let current = root;
36
-
37
- if (parts[0] === root._name) {
38
- parts.shift();
39
- }
40
-
41
- for (const part of parts) {
42
- if (!current._children || current._children.length === 0) return null;
43
- const found = current._children.find(c => c._name === part);
44
- if (!found) return null;
45
- current = found;
46
- }
47
-
48
- return current;
49
- }
50
-
51
- function run(args) {
52
- if (args.length < 3) {
53
- console.log(JSON.stringify({ error: '用法: cocos2d-cli add-component <场景.fire|预制体.prefab> <节点路径> <组件类型>' }));
54
- return;
55
- }
56
-
57
- const filePath = args[0];
58
- const nodePath = args[1];
59
- const compType = args[2];
60
-
61
- const ext = path.extname(filePath).toLowerCase();
62
-
63
- try {
64
- let root;
65
- let asset;
66
- const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
67
-
68
- if (ext === '.fire') {
69
- asset = CCSceneAsset.fromJSON(json);
70
- root = asset._scene;
71
- } else if (ext === '.prefab') {
72
- asset = CCPrefab.fromJSON(json);
73
- root = asset._root;
74
- } else {
75
- console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
76
- return;
77
- }
78
-
79
- const node = findNode(root, nodePath);
80
-
81
- if (!node) {
82
- console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
83
- return;
84
- }
85
-
86
- // 创建组件
87
- const comp = createComponent(compType);
88
- if (!comp) {
89
- console.log(JSON.stringify({ error: `不支持的组件类型: ${compType}` }));
90
- return;
91
- }
92
-
93
- comp.node = node;
94
- node._components = node._components || [];
95
- node._components.push(comp);
96
-
97
- // 保存
98
- const data = asset.toJSON();
99
- fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
100
-
101
- // 输出节点树
102
- const scriptMap = loadScriptMap(filePath);
103
- const prefab = isPrefab(data);
104
- const startIndex = prefab ? 0 : 1;
105
- console.log(buildTree(data, scriptMap, startIndex).trim());
106
-
107
- } catch (err) {
108
- console.log(JSON.stringify({ error: err.message }));
109
- }
110
- }
111
-
112
- module.exports = { run };
1
+ /**
2
+ * add-component 命令 - 添加组件
3
+ */
4
+
5
+ const path = require('path');
6
+ const fs = require('fs');
7
+ const { CCSceneAsset, CCPrefab, CCCanvas, CCWidget, CCSprite, CCLabel, CCButton, CCCamera } = require('../lib/cc');
8
+ const { buildTree } = require('../lib/node-utils');
9
+ const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
10
+
11
+ /**
12
+ * 创建组件
13
+ */
14
+ function createComponent(type) {
15
+ switch (type.toLowerCase()) {
16
+ case 'canvas': return new CCCanvas();
17
+ case 'widget': return new CCWidget();
18
+ case 'sprite': return new CCSprite();
19
+ case 'label': return new CCLabel();
20
+ case 'button': return new CCButton();
21
+ case 'camera': return new CCCamera();
22
+ default: return null;
23
+ }
24
+ }
25
+
26
+ /**
27
+ * 查找节点
28
+ */
29
+ function findNode(root, path) {
30
+ if (!path) return root;
31
+
32
+ const parts = path.split('/').filter(p => p);
33
+ if (parts.length === 0) return root;
34
+
35
+ let current = root;
36
+
37
+ if (parts[0] === root._name) {
38
+ parts.shift();
39
+ }
40
+
41
+ for (const part of parts) {
42
+ if (!current._children || current._children.length === 0) return null;
43
+ const found = current._children.find(c => c._name === part);
44
+ if (!found) return null;
45
+ current = found;
46
+ }
47
+
48
+ return current;
49
+ }
50
+
51
+ function run(args) {
52
+ if (args.length < 3) {
53
+ console.log(JSON.stringify({ error: '用法: cocos2d-cli add-component <场景.fire|预制体.prefab> <节点路径> <组件类型>' }));
54
+ return;
55
+ }
56
+
57
+ const filePath = args[0];
58
+ const nodePath = args[1];
59
+ const compType = args[2];
60
+
61
+ const ext = path.extname(filePath).toLowerCase();
62
+
63
+ try {
64
+ let root;
65
+ let asset;
66
+ const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
67
+
68
+ if (ext === '.fire') {
69
+ asset = CCSceneAsset.fromJSON(json);
70
+ root = asset._scene;
71
+ } else if (ext === '.prefab') {
72
+ asset = CCPrefab.fromJSON(json);
73
+ root = asset._root;
74
+ } else {
75
+ console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
76
+ return;
77
+ }
78
+
79
+ const node = findNode(root, nodePath);
80
+
81
+ if (!node) {
82
+ console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
83
+ return;
84
+ }
85
+
86
+ // 创建组件
87
+ const comp = createComponent(compType);
88
+ if (!comp) {
89
+ console.log(JSON.stringify({ error: `不支持的组件类型: ${compType}` }));
90
+ return;
91
+ }
92
+
93
+ comp.node = node;
94
+ node._components = node._components || [];
95
+ node._components.push(comp);
96
+
97
+ // 保存
98
+ const data = asset.toJSON();
99
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
100
+
101
+ // 输出节点树
102
+ const scriptMap = loadScriptMap(filePath);
103
+ const prefab = isPrefab(data);
104
+ const startIndex = prefab ? 0 : 1;
105
+ console.log(buildTree(data, scriptMap, startIndex).trim());
106
+
107
+ } catch (err) {
108
+ console.log(JSON.stringify({ error: err.message }));
109
+ }
110
+ }
111
+
112
+ module.exports = { run };