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.
- package/bin/cocos2d-cli.js +152 -152
- package/data/script_map.json +25 -25
- package/package.json +33 -33
- package/src/commands/add-component.js +112 -112
- package/src/commands/add.js +177 -177
- package/src/commands/build.js +78 -78
- package/src/commands/create-scene.js +181 -181
- package/src/commands/get.js +108 -108
- package/src/commands/prefab-create.js +110 -110
- package/src/commands/remove-component.js +110 -110
- package/src/commands/remove.js +99 -99
- package/src/commands/screenshot.js +108 -103
- package/src/commands/set-component.js +119 -119
- package/src/commands/set.js +107 -107
- package/src/commands/tree.js +28 -28
- package/src/lib/cc/CCButton.js +122 -122
- package/src/lib/cc/CCCamera.js +93 -93
- package/src/lib/cc/CCCanvas.js +54 -54
- package/src/lib/cc/CCColor.js +32 -32
- package/src/lib/cc/CCComponent.js +60 -60
- package/src/lib/cc/CCLabel.js +146 -146
- package/src/lib/cc/CCNode.js +255 -255
- package/src/lib/cc/CCObject.js +23 -23
- package/src/lib/cc/CCPrefab.js +242 -242
- package/src/lib/cc/CCRect.js +32 -32
- package/src/lib/cc/CCRichText.js +44 -44
- package/src/lib/cc/CCScene.js +42 -42
- package/src/lib/cc/CCSceneAsset.js +302 -302
- package/src/lib/cc/CCSize.js +26 -26
- package/src/lib/cc/CCSprite.js +94 -94
- package/src/lib/cc/CCTrs.js +74 -74
- package/src/lib/cc/CCVec2.js +26 -26
- package/src/lib/cc/CCVec3.js +29 -29
- package/src/lib/cc/CCWidget.js +98 -98
- package/src/lib/cc/index.js +42 -42
- package/src/lib/fire-utils.js +373 -373
- package/src/lib/json-parser.js +185 -185
- package/src/lib/node-utils.js +395 -395
- package/src/lib/screenshot/index.html +29 -29
- package/src/lib/screenshot-core.js +285 -286
- package/src/lib/templates.js +49 -49
- package/src/lib/utils.js +202 -202
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* prefab-create 命令 - 从 JSON 结构创建预制体文件
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const fs = require('fs');
|
|
6
|
-
const path = require('path');
|
|
7
|
-
const { outputError } = require('../lib/utils');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { createPrefab } = require('../lib/templates');
|
|
10
|
-
const { CCPrefab, CCPrefabInfo } = require('../lib/cc');
|
|
11
|
-
const { loadScriptMap, createPrefabMeta, saveMetaFile } = require('../lib/fire-utils');
|
|
12
|
-
const { fromJSON } = require('../lib/json-parser');
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* 为节点树添加 PrefabInfo
|
|
16
|
-
*/
|
|
17
|
-
function addPrefabInfo(node, isRoot = false) {
|
|
18
|
-
node._prefab = new CCPrefabInfo();
|
|
19
|
-
|
|
20
|
-
if (node._children) {
|
|
21
|
-
node._children.forEach(child => addPrefabInfo(child, false));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function run(args) {
|
|
26
|
-
if (args.length < 1) {
|
|
27
|
-
outputError({
|
|
28
|
-
message: '用法: cocos2d-cli create-prefab [JSON文件路径] <输出路径.prefab>',
|
|
29
|
-
hint: '不传 JSON 则创建默认预制体'
|
|
30
|
-
});
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
let jsonPath = null;
|
|
35
|
-
let outputPath;
|
|
36
|
-
|
|
37
|
-
if (args.length === 1) {
|
|
38
|
-
outputPath = args[0];
|
|
39
|
-
} else {
|
|
40
|
-
jsonPath = args[0];
|
|
41
|
-
outputPath = args[1];
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const prefabName = path.basename(outputPath, '.prefab');
|
|
45
|
-
|
|
46
|
-
try {
|
|
47
|
-
// 没有传 JSON,创建默认预制体
|
|
48
|
-
if (!jsonPath) {
|
|
49
|
-
if (fs.existsSync(outputPath)) {
|
|
50
|
-
outputError(`文件已存在: ${outputPath}`);
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const dir = path.dirname(outputPath);
|
|
55
|
-
if (!fs.existsSync(dir)) {
|
|
56
|
-
fs.mkdirSync(dir, { recursive: true });
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const prefab = createPrefab(prefabName);
|
|
60
|
-
const data = prefab.toJSON();
|
|
61
|
-
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
62
|
-
|
|
63
|
-
// 生成并保存 meta 文件
|
|
64
|
-
saveMetaFile(outputPath, createPrefabMeta());
|
|
65
|
-
|
|
66
|
-
const scriptMap = loadScriptMap(outputPath);
|
|
67
|
-
console.log(buildTree(data, scriptMap, 1).trim());
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// 从 JSON 文件创建
|
|
72
|
-
if (!fs.existsSync(jsonPath)) {
|
|
73
|
-
outputError(`JSON 文件不存在: ${jsonPath}`);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const input = fs.readFileSync(jsonPath, 'utf8');
|
|
78
|
-
const rootNode = fromJSON(input);
|
|
79
|
-
|
|
80
|
-
// 确保根节点名称
|
|
81
|
-
if (!rootNode._name || rootNode._name === 'Node') {
|
|
82
|
-
rootNode._name = prefabName;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// 为所有节点添加 PrefabInfo
|
|
86
|
-
addPrefabInfo(rootNode, true);
|
|
87
|
-
|
|
88
|
-
// 创建预制体
|
|
89
|
-
const prefab = new CCPrefab();
|
|
90
|
-
prefab._root = rootNode;
|
|
91
|
-
|
|
92
|
-
const outputDir = path.dirname(outputPath);
|
|
93
|
-
if (!fs.existsSync(outputDir)) {
|
|
94
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
const data = prefab.toJSON();
|
|
98
|
-
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
99
|
-
|
|
100
|
-
// 生成并保存 meta 文件
|
|
101
|
-
saveMetaFile(outputPath, createPrefabMeta());
|
|
102
|
-
|
|
103
|
-
const scriptMap = loadScriptMap(outputPath);
|
|
104
|
-
console.log(buildTree(data, scriptMap, 1).trim());
|
|
105
|
-
|
|
106
|
-
} catch (err) {
|
|
107
|
-
outputError(err.message);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
1
|
+
/**
|
|
2
|
+
* prefab-create 命令 - 从 JSON 结构创建预制体文件
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const fs = require('fs');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { outputError } = require('../lib/utils');
|
|
8
|
+
const { buildTree } = require('../lib/node-utils');
|
|
9
|
+
const { createPrefab } = require('../lib/templates');
|
|
10
|
+
const { CCPrefab, CCPrefabInfo } = require('../lib/cc');
|
|
11
|
+
const { loadScriptMap, createPrefabMeta, saveMetaFile } = require('../lib/fire-utils');
|
|
12
|
+
const { fromJSON } = require('../lib/json-parser');
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 为节点树添加 PrefabInfo
|
|
16
|
+
*/
|
|
17
|
+
function addPrefabInfo(node, isRoot = false) {
|
|
18
|
+
node._prefab = new CCPrefabInfo();
|
|
19
|
+
|
|
20
|
+
if (node._children) {
|
|
21
|
+
node._children.forEach(child => addPrefabInfo(child, false));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function run(args) {
|
|
26
|
+
if (args.length < 1) {
|
|
27
|
+
outputError({
|
|
28
|
+
message: '用法: cocos2d-cli create-prefab [JSON文件路径] <输出路径.prefab>',
|
|
29
|
+
hint: '不传 JSON 则创建默认预制体'
|
|
30
|
+
});
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let jsonPath = null;
|
|
35
|
+
let outputPath;
|
|
36
|
+
|
|
37
|
+
if (args.length === 1) {
|
|
38
|
+
outputPath = args[0];
|
|
39
|
+
} else {
|
|
40
|
+
jsonPath = args[0];
|
|
41
|
+
outputPath = args[1];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const prefabName = path.basename(outputPath, '.prefab');
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
// 没有传 JSON,创建默认预制体
|
|
48
|
+
if (!jsonPath) {
|
|
49
|
+
if (fs.existsSync(outputPath)) {
|
|
50
|
+
outputError(`文件已存在: ${outputPath}`);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const dir = path.dirname(outputPath);
|
|
55
|
+
if (!fs.existsSync(dir)) {
|
|
56
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const prefab = createPrefab(prefabName);
|
|
60
|
+
const data = prefab.toJSON();
|
|
61
|
+
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
62
|
+
|
|
63
|
+
// 生成并保存 meta 文件
|
|
64
|
+
saveMetaFile(outputPath, createPrefabMeta());
|
|
65
|
+
|
|
66
|
+
const scriptMap = loadScriptMap(outputPath);
|
|
67
|
+
console.log(buildTree(data, scriptMap, 1).trim());
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// 从 JSON 文件创建
|
|
72
|
+
if (!fs.existsSync(jsonPath)) {
|
|
73
|
+
outputError(`JSON 文件不存在: ${jsonPath}`);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const input = fs.readFileSync(jsonPath, 'utf8');
|
|
78
|
+
const rootNode = fromJSON(input);
|
|
79
|
+
|
|
80
|
+
// 确保根节点名称
|
|
81
|
+
if (!rootNode._name || rootNode._name === 'Node') {
|
|
82
|
+
rootNode._name = prefabName;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// 为所有节点添加 PrefabInfo
|
|
86
|
+
addPrefabInfo(rootNode, true);
|
|
87
|
+
|
|
88
|
+
// 创建预制体
|
|
89
|
+
const prefab = new CCPrefab();
|
|
90
|
+
prefab._root = rootNode;
|
|
91
|
+
|
|
92
|
+
const outputDir = path.dirname(outputPath);
|
|
93
|
+
if (!fs.existsSync(outputDir)) {
|
|
94
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const data = prefab.toJSON();
|
|
98
|
+
fs.writeFileSync(outputPath, JSON.stringify(data, null, 2), 'utf8');
|
|
99
|
+
|
|
100
|
+
// 生成并保存 meta 文件
|
|
101
|
+
saveMetaFile(outputPath, createPrefabMeta());
|
|
102
|
+
|
|
103
|
+
const scriptMap = loadScriptMap(outputPath);
|
|
104
|
+
console.log(buildTree(data, scriptMap, 1).trim());
|
|
105
|
+
|
|
106
|
+
} catch (err) {
|
|
107
|
+
outputError(err.message);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
111
|
module.exports = { run };
|
|
@@ -1,111 +1,111 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* remove-component 命令 - 移除组件
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 查找节点
|
|
13
|
-
*/
|
|
14
|
-
function findNode(root, path) {
|
|
15
|
-
if (!path) return root;
|
|
16
|
-
|
|
17
|
-
const parts = path.split('/').filter(p => p);
|
|
18
|
-
if (parts.length === 0) return root;
|
|
19
|
-
|
|
20
|
-
let current = root;
|
|
21
|
-
|
|
22
|
-
if (parts[0] === root._name) {
|
|
23
|
-
parts.shift();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
for (const part of parts) {
|
|
27
|
-
if (!current._children || current._children.length === 0) return null;
|
|
28
|
-
const found = current._children.find(c => c._name === part);
|
|
29
|
-
if (!found) return null;
|
|
30
|
-
current = found;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return current;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* 查找组件
|
|
38
|
-
*/
|
|
39
|
-
function findComponent(node, compType) {
|
|
40
|
-
if (!node._components) return null;
|
|
41
|
-
|
|
42
|
-
const type = compType.toLowerCase();
|
|
43
|
-
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
-
|
|
45
|
-
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function run(args) {
|
|
49
|
-
if (args.length < 3) {
|
|
50
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli remove-component <场景.fire|预制体.prefab> <节点路径> <组件类型>' }));
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const filePath = args[0];
|
|
55
|
-
const nodePath = args[1];
|
|
56
|
-
const compType = args[2];
|
|
57
|
-
|
|
58
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
59
|
-
|
|
60
|
-
try {
|
|
61
|
-
let root;
|
|
62
|
-
let asset;
|
|
63
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
64
|
-
|
|
65
|
-
if (ext === '.fire') {
|
|
66
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
67
|
-
root = asset._scene;
|
|
68
|
-
} else if (ext === '.prefab') {
|
|
69
|
-
asset = CCPrefab.fromJSON(json);
|
|
70
|
-
root = asset._root;
|
|
71
|
-
} else {
|
|
72
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const node = findNode(root, nodePath);
|
|
77
|
-
|
|
78
|
-
if (!node) {
|
|
79
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const comp = findComponent(node, compType);
|
|
84
|
-
|
|
85
|
-
if (!comp) {
|
|
86
|
-
console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// 移除组件
|
|
91
|
-
const idx = node._components.indexOf(comp);
|
|
92
|
-
if (idx > -1) {
|
|
93
|
-
node._components.splice(idx, 1);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 保存
|
|
97
|
-
const data = asset.toJSON();
|
|
98
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
99
|
-
|
|
100
|
-
// 输出节点树
|
|
101
|
-
const scriptMap = loadScriptMap(filePath);
|
|
102
|
-
const prefab = isPrefab(data);
|
|
103
|
-
const startIndex = prefab ? 0 : 1;
|
|
104
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
105
|
-
|
|
106
|
-
} catch (err) {
|
|
107
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
1
|
+
/**
|
|
2
|
+
* remove-component 命令 - 移除组件
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
+
const { buildTree } = require('../lib/node-utils');
|
|
9
|
+
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 查找节点
|
|
13
|
+
*/
|
|
14
|
+
function findNode(root, path) {
|
|
15
|
+
if (!path) return root;
|
|
16
|
+
|
|
17
|
+
const parts = path.split('/').filter(p => p);
|
|
18
|
+
if (parts.length === 0) return root;
|
|
19
|
+
|
|
20
|
+
let current = root;
|
|
21
|
+
|
|
22
|
+
if (parts[0] === root._name) {
|
|
23
|
+
parts.shift();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
if (!current._children || current._children.length === 0) return null;
|
|
28
|
+
const found = current._children.find(c => c._name === part);
|
|
29
|
+
if (!found) return null;
|
|
30
|
+
current = found;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 查找组件
|
|
38
|
+
*/
|
|
39
|
+
function findComponent(node, compType) {
|
|
40
|
+
if (!node._components) return null;
|
|
41
|
+
|
|
42
|
+
const type = compType.toLowerCase();
|
|
43
|
+
const typeName = 'cc.' + type.charAt(0).toUpperCase() + type.slice(1);
|
|
44
|
+
|
|
45
|
+
return node._components.find(c => c.__type__ === typeName);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function run(args) {
|
|
49
|
+
if (args.length < 3) {
|
|
50
|
+
console.log(JSON.stringify({ error: '用法: cocos2d-cli remove-component <场景.fire|预制体.prefab> <节点路径> <组件类型>' }));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const filePath = args[0];
|
|
55
|
+
const nodePath = args[1];
|
|
56
|
+
const compType = args[2];
|
|
57
|
+
|
|
58
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
let root;
|
|
62
|
+
let asset;
|
|
63
|
+
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
64
|
+
|
|
65
|
+
if (ext === '.fire') {
|
|
66
|
+
asset = CCSceneAsset.fromJSON(json);
|
|
67
|
+
root = asset._scene;
|
|
68
|
+
} else if (ext === '.prefab') {
|
|
69
|
+
asset = CCPrefab.fromJSON(json);
|
|
70
|
+
root = asset._root;
|
|
71
|
+
} else {
|
|
72
|
+
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const node = findNode(root, nodePath);
|
|
77
|
+
|
|
78
|
+
if (!node) {
|
|
79
|
+
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const comp = findComponent(node, compType);
|
|
84
|
+
|
|
85
|
+
if (!comp) {
|
|
86
|
+
console.log(JSON.stringify({ error: `组件不存在: ${compType}` }));
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// 移除组件
|
|
91
|
+
const idx = node._components.indexOf(comp);
|
|
92
|
+
if (idx > -1) {
|
|
93
|
+
node._components.splice(idx, 1);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// 保存
|
|
97
|
+
const data = asset.toJSON();
|
|
98
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
99
|
+
|
|
100
|
+
// 输出节点树
|
|
101
|
+
const scriptMap = loadScriptMap(filePath);
|
|
102
|
+
const prefab = isPrefab(data);
|
|
103
|
+
const startIndex = prefab ? 0 : 1;
|
|
104
|
+
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
105
|
+
|
|
106
|
+
} catch (err) {
|
|
107
|
+
console.log(JSON.stringify({ error: err.message }));
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
111
|
module.exports = { run };
|
package/src/commands/remove.js
CHANGED
|
@@ -1,99 +1,99 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* remove 命令 - 删除节点
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
const path = require('path');
|
|
6
|
-
const fs = require('fs');
|
|
7
|
-
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
-
const { buildTree } = require('../lib/node-utils');
|
|
9
|
-
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 查找节点
|
|
13
|
-
*/
|
|
14
|
-
function findNode(root, path) {
|
|
15
|
-
if (!path) return root;
|
|
16
|
-
|
|
17
|
-
const parts = path.split('/').filter(p => p);
|
|
18
|
-
if (parts.length === 0) return root;
|
|
19
|
-
|
|
20
|
-
let current = root;
|
|
21
|
-
|
|
22
|
-
if (parts[0] === root._name) {
|
|
23
|
-
parts.shift();
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
for (const part of parts) {
|
|
27
|
-
if (!current._children || current._children.length === 0) return null;
|
|
28
|
-
const found = current._children.find(c => c._name === part);
|
|
29
|
-
if (!found) return null;
|
|
30
|
-
current = found;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return current;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function run(args) {
|
|
37
|
-
if (args.length < 2) {
|
|
38
|
-
console.log(JSON.stringify({ error: '用法: cocos2d-cli remove <场景.fire|预制体.prefab> <节点路径>' }));
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const filePath = args[0];
|
|
43
|
-
const nodePath = args[1];
|
|
44
|
-
|
|
45
|
-
const ext = path.extname(filePath).toLowerCase();
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
let root;
|
|
49
|
-
let asset;
|
|
50
|
-
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
51
|
-
|
|
52
|
-
if (ext === '.fire') {
|
|
53
|
-
asset = CCSceneAsset.fromJSON(json);
|
|
54
|
-
root = asset._scene;
|
|
55
|
-
} else if (ext === '.prefab') {
|
|
56
|
-
asset = CCPrefab.fromJSON(json);
|
|
57
|
-
root = asset._root;
|
|
58
|
-
} else {
|
|
59
|
-
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const node = findNode(root, nodePath);
|
|
64
|
-
|
|
65
|
-
if (!node) {
|
|
66
|
-
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
// 不能删除根节点
|
|
71
|
-
if (node === root) {
|
|
72
|
-
console.log(JSON.stringify({ error: '不能删除根节点' }));
|
|
73
|
-
return;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// 从父节点移除
|
|
77
|
-
if (node._parent) {
|
|
78
|
-
const idx = node._parent._children.indexOf(node);
|
|
79
|
-
if (idx > -1) {
|
|
80
|
-
node._parent._children.splice(idx, 1);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 保存
|
|
85
|
-
const data = asset.toJSON();
|
|
86
|
-
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
87
|
-
|
|
88
|
-
// 输出节点树
|
|
89
|
-
const scriptMap = loadScriptMap(filePath);
|
|
90
|
-
const prefab = isPrefab(data);
|
|
91
|
-
const startIndex = prefab ? 0 : 1;
|
|
92
|
-
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
93
|
-
|
|
94
|
-
} catch (err) {
|
|
95
|
-
console.log(JSON.stringify({ error: err.message }));
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = { run };
|
|
1
|
+
/**
|
|
2
|
+
* remove 命令 - 删除节点
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const fs = require('fs');
|
|
7
|
+
const { CCSceneAsset, CCPrefab } = require('../lib/cc');
|
|
8
|
+
const { buildTree } = require('../lib/node-utils');
|
|
9
|
+
const { loadScriptMap, isPrefab } = require('../lib/fire-utils');
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* 查找节点
|
|
13
|
+
*/
|
|
14
|
+
function findNode(root, path) {
|
|
15
|
+
if (!path) return root;
|
|
16
|
+
|
|
17
|
+
const parts = path.split('/').filter(p => p);
|
|
18
|
+
if (parts.length === 0) return root;
|
|
19
|
+
|
|
20
|
+
let current = root;
|
|
21
|
+
|
|
22
|
+
if (parts[0] === root._name) {
|
|
23
|
+
parts.shift();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const part of parts) {
|
|
27
|
+
if (!current._children || current._children.length === 0) return null;
|
|
28
|
+
const found = current._children.find(c => c._name === part);
|
|
29
|
+
if (!found) return null;
|
|
30
|
+
current = found;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return current;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function run(args) {
|
|
37
|
+
if (args.length < 2) {
|
|
38
|
+
console.log(JSON.stringify({ error: '用法: cocos2d-cli remove <场景.fire|预制体.prefab> <节点路径>' }));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const filePath = args[0];
|
|
43
|
+
const nodePath = args[1];
|
|
44
|
+
|
|
45
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
46
|
+
|
|
47
|
+
try {
|
|
48
|
+
let root;
|
|
49
|
+
let asset;
|
|
50
|
+
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
51
|
+
|
|
52
|
+
if (ext === '.fire') {
|
|
53
|
+
asset = CCSceneAsset.fromJSON(json);
|
|
54
|
+
root = asset._scene;
|
|
55
|
+
} else if (ext === '.prefab') {
|
|
56
|
+
asset = CCPrefab.fromJSON(json);
|
|
57
|
+
root = asset._root;
|
|
58
|
+
} else {
|
|
59
|
+
console.log(JSON.stringify({ error: '不支持的文件类型,仅支持 .fire 和 .prefab' }));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const node = findNode(root, nodePath);
|
|
64
|
+
|
|
65
|
+
if (!node) {
|
|
66
|
+
console.log(JSON.stringify({ error: `节点不存在: ${nodePath}` }));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 不能删除根节点
|
|
71
|
+
if (node === root) {
|
|
72
|
+
console.log(JSON.stringify({ error: '不能删除根节点' }));
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 从父节点移除
|
|
77
|
+
if (node._parent) {
|
|
78
|
+
const idx = node._parent._children.indexOf(node);
|
|
79
|
+
if (idx > -1) {
|
|
80
|
+
node._parent._children.splice(idx, 1);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 保存
|
|
85
|
+
const data = asset.toJSON();
|
|
86
|
+
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8');
|
|
87
|
+
|
|
88
|
+
// 输出节点树
|
|
89
|
+
const scriptMap = loadScriptMap(filePath);
|
|
90
|
+
const prefab = isPrefab(data);
|
|
91
|
+
const startIndex = prefab ? 0 : 1;
|
|
92
|
+
console.log(buildTree(data, scriptMap, startIndex).trim());
|
|
93
|
+
|
|
94
|
+
} catch (err) {
|
|
95
|
+
console.log(JSON.stringify({ error: err.message }));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
module.exports = { run };
|