cocos2d-cli 1.6.5 → 2.1.0

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 (62) hide show
  1. package/data/script_map.json +25 -25
  2. package/dist/bin/cocos2d-cli.js +64 -0
  3. package/dist/src/commands/add-component.js +3 -0
  4. package/dist/src/commands/add.js +3 -0
  5. package/dist/src/commands/build.js +6 -0
  6. package/dist/src/commands/create-scene.js +3 -0
  7. package/dist/src/commands/get.js +3 -0
  8. package/dist/src/commands/prefab-create.js +109 -0
  9. package/dist/src/commands/remove-component.js +3 -0
  10. package/dist/src/commands/remove.js +3 -0
  11. package/dist/src/commands/screenshot.js +41 -0
  12. package/dist/src/commands/set-component.js +3 -0
  13. package/dist/src/commands/set.js +3 -0
  14. package/dist/src/commands/tree.js +24 -0
  15. package/{src → dist/src}/lib/cc/CCButton.js +115 -122
  16. package/{src → dist/src}/lib/cc/CCCamera.js +83 -93
  17. package/{src → dist/src}/lib/cc/CCCanvas.js +49 -54
  18. package/{src → dist/src}/lib/cc/CCColor.js +30 -32
  19. package/{src → dist/src}/lib/cc/CCComponent.js +39 -60
  20. package/{src → dist/src}/lib/cc/CCLabel.js +139 -146
  21. package/{src → dist/src}/lib/cc/CCNode.js +190 -256
  22. package/{src → dist/src}/lib/cc/CCObject.js +19 -23
  23. package/{src → dist/src}/lib/cc/CCPrefab.js +219 -242
  24. package/{src → dist/src}/lib/cc/CCRect.js +30 -32
  25. package/{src → dist/src}/lib/cc/CCRichText.js +38 -44
  26. package/{src → dist/src}/lib/cc/CCScene.js +32 -42
  27. package/dist/src/lib/cc/CCSceneAsset.js +242 -0
  28. package/{src → dist/src}/lib/cc/CCSize.js +22 -26
  29. package/{src → dist/src}/lib/cc/CCSprite.js +82 -94
  30. package/{src → dist/src}/lib/cc/CCTrs.js +49 -74
  31. package/{src → dist/src}/lib/cc/CCVec2.js +22 -26
  32. package/{src → dist/src}/lib/cc/CCVec3.js +26 -29
  33. package/{src → dist/src}/lib/cc/CCWidget.js +94 -98
  34. package/dist/src/lib/fire-utils.js +86 -0
  35. package/dist/src/lib/json-parser.js +114 -0
  36. package/dist/src/lib/node-utils.js +131 -0
  37. package/{src → dist/src}/lib/screenshot-core.js +242 -285
  38. package/dist/src/lib/templates.js +17 -0
  39. package/dist/src/lib/utils.js +81 -0
  40. package/package.json +40 -33
  41. package/bin/cocos2d-cli.js +0 -152
  42. package/src/commands/add-component.js +0 -112
  43. package/src/commands/add.js +0 -177
  44. package/src/commands/build.js +0 -78
  45. package/src/commands/create-scene.js +0 -181
  46. package/src/commands/get.js +0 -108
  47. package/src/commands/prefab-create.js +0 -111
  48. package/src/commands/remove-component.js +0 -111
  49. package/src/commands/remove.js +0 -99
  50. package/src/commands/screenshot.js +0 -108
  51. package/src/commands/set-component.js +0 -119
  52. package/src/commands/set.js +0 -107
  53. package/src/commands/tree.js +0 -29
  54. package/src/lib/cc/CCSceneAsset.js +0 -303
  55. package/src/lib/cc/index.js +0 -42
  56. package/src/lib/fire-utils.js +0 -374
  57. package/src/lib/json-parser.js +0 -185
  58. package/src/lib/node-utils.js +0 -395
  59. package/src/lib/screenshot/favicon.ico +0 -0
  60. package/src/lib/screenshot/index.html +0 -30
  61. package/src/lib/templates.js +0 -49
  62. package/src/lib/utils.js +0 -202
@@ -1,285 +1,242 @@
1
- /**
2
- * Screenshot Core Module
3
- * 渲染 JSON 数据并使用 Playwright 截图
4
- */
5
-
6
- const { chromium } = require('playwright');
7
- const fs = require('fs').promises;
8
- const path = require('path');
9
- const http = require('http');
10
- const url = require('url');
11
- const os = require('os');
12
-
13
- // 默认配置
14
- const DEFAULT_CONFIG = {
15
- jsonPath: null,
16
- outputDir: process.cwd(),
17
- viewport: { width: 750, height: 1334 },
18
- fullPage: true,
19
- debugBounds: false,
20
- timeout: 30000,
21
- waitTime: 1000
22
- };
23
-
24
- // 获取内置资源目录
25
- function getAssetsDir() {
26
- return path.join(__dirname, 'screenshot');
27
- }
28
-
29
- // 创建临时工作目录
30
- async function createTempWorkDir() {
31
- const tempBase = os.tmpdir();
32
- const timestamp = Date.now();
33
- const tempDir = path.join(tempBase, `cocos2d-screenshot-${timestamp}`);
34
- await fs.mkdir(tempDir, { recursive: true });
35
- return tempDir;
36
- }
37
-
38
- // 复制内置资源到临时目录
39
- async function copyBuiltInAssets(tempDir, logs) {
40
- const assetsDir = getAssetsDir();
41
- const assets = ['index.html', 'favicon.ico'];
42
-
43
- for (const asset of assets) {
44
- const src = path.join(assetsDir, asset);
45
- const dest = path.join(tempDir, asset);
46
- try {
47
- await fs.copyFile(src, dest);
48
- } catch (err) {
49
- logs.push({
50
- timestamp: new Date().toISOString(),
51
- type: 'warning',
52
- text: `Could not copy ${asset}: ${err.message}`
53
- });
54
- }
55
- }
56
- }
57
-
58
- // 静态文件服务器
59
- async function startServer(staticDir, logs) {
60
- return new Promise((resolve, reject) => {
61
- const server = http.createServer(async (req, res) => {
62
- try {
63
- const parsedUrl = url.parse(req.url);
64
- let filePath = path.join(staticDir, parsedUrl.pathname);
65
-
66
- if (parsedUrl.pathname === '/') {
67
- filePath = path.join(staticDir, 'index.html');
68
- }
69
-
70
- const data = await fs.readFile(filePath);
71
-
72
- const ext = path.extname(filePath).toLowerCase();
73
- const contentTypes = {
74
- '.html': 'text/html',
75
- '.htm': 'text/html',
76
- '.json': 'application/json',
77
- '.js': 'application/javascript',
78
- '.mjs': 'application/javascript',
79
- '.css': 'text/css',
80
- '.png': 'image/png',
81
- '.jpg': 'image/jpeg',
82
- '.jpeg': 'image/jpeg',
83
- '.gif': 'image/gif',
84
- '.svg': 'image/svg+xml',
85
- '.ico': 'image/x-icon',
86
- '.txt': 'text/plain',
87
- '.xml': 'application/xml'
88
- };
89
-
90
- res.writeHead(200, {
91
- 'Content-Type': contentTypes[ext] || 'application/octet-stream',
92
- 'Access-Control-Allow-Origin': '*'
93
- });
94
- res.end(data);
95
-
96
- } catch (err) {
97
- if (err.code === 'ENOENT') {
98
- res.writeHead(404);
99
- res.end();
100
- } else {
101
- logs.push({
102
- timestamp: new Date().toISOString(),
103
- type: 'server-error',
104
- text: `${req.url}: ${err.message}`
105
- });
106
- res.writeHead(500);
107
- res.end();
108
- }
109
- }
110
- });
111
-
112
- server.listen(0, '127.0.0.1', () => resolve(server));
113
- });
114
- }
115
-
116
- // 递归删除目录
117
- async function removeDir(dirPath, logs) {
118
- try {
119
- await fs.rm(dirPath, { recursive: true, force: true });
120
- } catch (err) {
121
- if (logs) {
122
- logs.push({
123
- timestamp: new Date().toISOString(),
124
- type: 'warning',
125
- text: `Could not remove temp dir: ${err.message}`
126
- });
127
- }
128
- }
129
- }
130
-
131
- /**
132
- * 截图核心函数
133
- * @param {Object} userConfig - 配置选项
134
- * @param {string} userConfig.jsonPath - JSON 文件路径
135
- * @param {string} userConfig.outputDir - 输出目录
136
- * @param {Object} userConfig.viewport - 视口大小 {width, height}
137
- * @param {boolean} userConfig.fullPage - 是否全页截图
138
- * @param {number} userConfig.timeout - 超时时间(毫秒)
139
- * @param {number} userConfig.waitTime - 等待时间(毫秒)
140
- * @returns {Promise<{screenshotPath: string, logs: Array}>}
141
- */
142
- async function takeScreenshot(userConfig = {}) {
143
- const config = { ...DEFAULT_CONFIG, ...userConfig };
144
-
145
- if (!config.jsonPath) {
146
- throw new Error('JSON file path is required');
147
- }
148
-
149
- let server = null;
150
- let browser = null;
151
- let tempDir = null;
152
- const logs = [];
153
- let screenshotPath = null;
154
- let logDir = null;
155
-
156
- const addLog = (type, text, extra = {}) => {
157
- logs.push({
158
- timestamp: new Date().toISOString(),
159
- type,
160
- text,
161
- ...extra
162
- });
163
- };
164
-
165
- try {
166
- const timestamp = Date.now();
167
-
168
- // 检查 JSON 文件是否存在
169
- try {
170
- await fs.access(config.jsonPath);
171
- } catch (error) {
172
- throw new Error(`JSON file not found: ${config.jsonPath}`);
173
- }
174
-
175
- // 确保输出目录存在
176
- await fs.mkdir(config.outputDir, { recursive: true });
177
-
178
- // 创建临时工作目录
179
- tempDir = await createTempWorkDir();
180
- addLog('info', `Temp dir: ${tempDir}`);
181
-
182
- // 复制内置资源到临时目录
183
- await copyBuiltInAssets(tempDir, logs);
184
-
185
- // 复制用户的 JSON 文件到临时目录
186
- const destJsonPath = path.join(tempDir, 'data.json');
187
- await fs.copyFile(config.jsonPath, destJsonPath);
188
- addLog('info', `JSON: ${config.jsonPath}`);
189
-
190
- // 截图输出路径
191
- screenshotPath = path.join(config.outputDir, `screenshot-${timestamp}.png`);
192
-
193
- // 启动HTTP服务器
194
- addLog('info', 'Starting Server');
195
- server = await startServer(tempDir, logs);
196
- const serverUrl = `http://127.0.0.1:${server.address().port}`;
197
- addLog('info', `Server: ${serverUrl}`);
198
-
199
- // 启动浏览器
200
- addLog('info', 'Launching Browser');
201
- browser = await chromium.launch({
202
- headless: true
203
- });
204
- addLog('info', 'Browser launched');
205
-
206
- const page = await browser.newPage({
207
- viewport: config.viewport
208
- });
209
-
210
- // 监听浏览器控制台日志
211
- page.on('console', msg => {
212
- const text = msg.text();
213
- addLog(msg.type(), text);
214
- });
215
-
216
- page.on('pageerror', error => {
217
- addLog('pageerror', error.message);
218
- });
219
-
220
- page.on('requestfailed', request => {
221
- const failure = request.failure();
222
- const errorText = failure ? failure.errorText : 'Unknown error';
223
- addLog('requestfailed', request.url(), { error: errorText });
224
- });
225
-
226
- // 加载页面
227
- addLog('info', 'Loading Page');
228
- const pageUrl = config.debugBounds
229
- ? `${serverUrl}/index.html?debugBounds=true`
230
- : `${serverUrl}/index.html`;
231
- await page.goto(pageUrl, {
232
- waitUntil: 'networkidle',
233
- timeout: config.timeout
234
- });
235
- addLog('info', 'Page loaded');
236
-
237
- // 等待渲染
238
- addLog('info', 'Waiting for Render');
239
- await page.waitForTimeout(config.waitTime);
240
- addLog('info', 'Wait complete');
241
-
242
- // 截图
243
- addLog('info', 'Taking Screenshot');
244
- await page.screenshot({
245
- path: screenshotPath,
246
- fullPage: config.fullPage
247
- });
248
- addLog('info', `Screenshot saved: ${screenshotPath}`);
249
- addLog('info', 'Done');
250
-
251
- return { screenshotPath, logs };
252
-
253
- } catch (error) {
254
- addLog('error', error.message);
255
- try {
256
- logDir = path.join(config.outputDir, `screenshot-logs-${Date.now()}`);
257
- await fs.mkdir(logDir, { recursive: true });
258
- await fs.writeFile(
259
- path.join(logDir, 'logs.json'),
260
- JSON.stringify({
261
- error: error.message,
262
- jsonPath: config.jsonPath,
263
- outputDir: config.outputDir,
264
- viewport: config.viewport,
265
- fullPage: config.fullPage,
266
- debugBounds: config.debugBounds,
267
- timeout: config.timeout,
268
- waitTime: config.waitTime,
269
- screenshotPath,
270
- logs
271
- }, null, 2),
272
- 'utf8'
273
- );
274
- } catch (_) {}
275
- error.logDir = logDir;
276
- throw error;
277
- } finally {
278
- // 清理资源
279
- if (browser) await browser.close().catch(() => {});
280
- if (server) server.close();
281
- if (tempDir) await removeDir(tempDir, logs);
282
- }
283
- }
284
-
285
- module.exports = { takeScreenshot };
1
+ import { chromium } from 'playwright';
2
+ import * as fs from 'fs/promises';
3
+ import * as path from 'path';
4
+ import * as http from 'http';
5
+ import { fileURLToPath } from 'url';
6
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
7
+ const DEFAULT_CONFIG = {
8
+ outputDir: process.cwd(),
9
+ viewport: { width: 750, height: 1334 },
10
+ fullPage: true,
11
+ debugBounds: false,
12
+ timeout: 30000,
13
+ waitTime: 1000
14
+ };
15
+ function getAssetsDir() {
16
+ const projectRoot = process.cwd();
17
+ return path.join(projectRoot, 'src', 'lib', 'screenshot');
18
+ }
19
+ async function createTempWorkDir() {
20
+ const tempBase = process.env.TEMP || process.env.TMP || '/tmp';
21
+ const timestamp = Date.now();
22
+ const tempDir = path.join(tempBase, `cocos2d-screenshot-${timestamp}`);
23
+ await fs.mkdir(tempDir, { recursive: true });
24
+ return tempDir;
25
+ }
26
+ async function copyBuiltInAssets(tempDir, logs) {
27
+ const assetsDir = getAssetsDir();
28
+ const assets = ['index.html', 'favicon.ico'];
29
+ for (const asset of assets) {
30
+ const src = path.join(assetsDir, asset);
31
+ const dest = path.join(tempDir, asset);
32
+ try {
33
+ await fs.copyFile(src, dest);
34
+ }
35
+ catch (err) {
36
+ logs.push({
37
+ timestamp: new Date().toISOString(),
38
+ type: 'warning',
39
+ text: `Could not copy ${asset}: ${err.message}`
40
+ });
41
+ }
42
+ }
43
+ }
44
+ async function startServer(staticDir, logs) {
45
+ return new Promise((resolve, reject) => {
46
+ const server = http.createServer(async (req, res) => {
47
+ try {
48
+ const reqUrl = new URL(req.url || '/', `http://${req.headers.host}`);
49
+ let filePath = path.join(staticDir, reqUrl.pathname);
50
+ if (reqUrl.pathname === '/') {
51
+ filePath = path.join(staticDir, 'index.html');
52
+ }
53
+ const data = await fs.readFile(filePath);
54
+ const ext = path.extname(filePath).toLowerCase();
55
+ const contentTypes = {
56
+ '.html': 'text/html',
57
+ '.htm': 'text/html',
58
+ '.json': 'application/json',
59
+ '.js': 'application/javascript',
60
+ '.mjs': 'application/javascript',
61
+ '.css': 'text/css',
62
+ '.png': 'image/png',
63
+ '.jpg': 'image/jpeg',
64
+ '.jpeg': 'image/jpeg',
65
+ '.gif': 'image/gif',
66
+ '.svg': 'image/svg+xml',
67
+ '.ico': 'image/x-icon',
68
+ '.txt': 'text/plain',
69
+ '.xml': 'application/xml'
70
+ };
71
+ res.writeHead(200, {
72
+ 'Content-Type': contentTypes[ext] || 'application/octet-stream',
73
+ 'Access-Control-Allow-Origin': '*'
74
+ });
75
+ res.end(data);
76
+ }
77
+ catch (err) {
78
+ if (err.code === 'ENOENT') {
79
+ res.writeHead(404);
80
+ res.end();
81
+ }
82
+ else {
83
+ logs.push({
84
+ timestamp: new Date().toISOString(),
85
+ type: 'server-error',
86
+ text: `${req.url}: ${err.message}`
87
+ });
88
+ res.writeHead(500);
89
+ res.end();
90
+ }
91
+ }
92
+ });
93
+ server.listen(0, '127.0.0.1', () => resolve(server));
94
+ });
95
+ }
96
+ async function removeDir(dirPath, logs) {
97
+ try {
98
+ await fs.rm(dirPath, { recursive: true, force: true });
99
+ }
100
+ catch (err) {
101
+ if (logs) {
102
+ logs.push({
103
+ timestamp: new Date().toISOString(),
104
+ type: 'warning',
105
+ text: `Could not remove temp dir: ${err.message}`
106
+ });
107
+ }
108
+ }
109
+ }
110
+ async function launchBrowser(addLog) {
111
+ try {
112
+ addLog('info', 'Launching Playwright Chromium');
113
+ const browser = await chromium.launch({
114
+ headless: true,
115
+ });
116
+ addLog('info', 'Browser launched with Playwright Chromium');
117
+ return browser;
118
+ }
119
+ catch (error) {
120
+ const message = String(error?.message || error);
121
+ if (!message.includes("Executable doesn't exist")) {
122
+ throw error;
123
+ }
124
+ addLog('warn', 'Playwright Chromium not found, falling back to system Edge');
125
+ const browser = await chromium.launch({
126
+ channel: 'msedge',
127
+ headless: true,
128
+ });
129
+ addLog('info', 'Browser launched with system Edge');
130
+ return browser;
131
+ }
132
+ }
133
+ export async function takeScreenshot(userConfig = {}) {
134
+ const config = { ...DEFAULT_CONFIG, ...userConfig };
135
+ if (!config.jsonPath) {
136
+ throw new Error('JSON file path is required');
137
+ }
138
+ let server = null;
139
+ let browser = null;
140
+ let tempDir = null;
141
+ const logs = [];
142
+ let screenshotPath = null;
143
+ let logDir = null;
144
+ const addLog = (type, text, extra = {}) => {
145
+ logs.push({
146
+ timestamp: new Date().toISOString(),
147
+ type,
148
+ text,
149
+ ...extra
150
+ });
151
+ };
152
+ try {
153
+ const timestamp = Date.now();
154
+ try {
155
+ await fs.access(config.jsonPath);
156
+ }
157
+ catch (error) {
158
+ throw new Error(`JSON file not found: ${config.jsonPath}`);
159
+ }
160
+ await fs.mkdir(config.outputDir, { recursive: true });
161
+ tempDir = await createTempWorkDir();
162
+ addLog('info', `Temp dir: ${tempDir}`);
163
+ await copyBuiltInAssets(tempDir, logs);
164
+ const destJsonPath = path.join(tempDir, 'data.json');
165
+ await fs.copyFile(config.jsonPath, destJsonPath);
166
+ addLog('info', `JSON: ${config.jsonPath}`);
167
+ screenshotPath = path.join(config.outputDir, `screenshot-${timestamp}.png`);
168
+ addLog('info', 'Starting Server');
169
+ server = await startServer(tempDir, logs);
170
+ const address = server.address();
171
+ const serverUrl = `http://127.0.0.1:${address.port}`;
172
+ addLog('info', `Server: ${serverUrl}`);
173
+ addLog('info', 'Launching Browser');
174
+ browser = await launchBrowser(addLog);
175
+ addLog('info', 'Browser launched');
176
+ const page = await browser.newPage({
177
+ viewport: config.viewport
178
+ });
179
+ page.on('console', (msg) => {
180
+ const text = msg.text();
181
+ addLog(msg.type(), text);
182
+ });
183
+ page.on('pageerror', (error) => {
184
+ addLog('pageerror', error.message);
185
+ });
186
+ page.on('requestfailed', (request) => {
187
+ const failure = request.failure();
188
+ const errorText = failure ? failure.errorText : 'Unknown error';
189
+ addLog('requestfailed', request.url(), { error: errorText });
190
+ });
191
+ addLog('info', 'Loading Page');
192
+ const pageUrl = config.debugBounds
193
+ ? `${serverUrl}/index.html?debugBounds=true`
194
+ : `${serverUrl}/index.html`;
195
+ await page.goto(pageUrl, {
196
+ waitUntil: 'networkidle',
197
+ timeout: config.timeout
198
+ });
199
+ addLog('info', 'Page loaded');
200
+ addLog('info', 'Waiting for Render');
201
+ await page.waitForTimeout(config.waitTime);
202
+ addLog('info', 'Wait complete');
203
+ addLog('info', 'Taking Screenshot');
204
+ await page.screenshot({
205
+ path: screenshotPath,
206
+ fullPage: config.fullPage
207
+ });
208
+ addLog('info', `Screenshot saved: ${screenshotPath}`);
209
+ addLog('info', 'Done');
210
+ return { screenshotPath, logs };
211
+ }
212
+ catch (error) {
213
+ addLog('error', error.message);
214
+ try {
215
+ logDir = path.join(config.outputDir, `screenshot-logs-${Date.now()}`);
216
+ await fs.mkdir(logDir, { recursive: true });
217
+ await fs.writeFile(path.join(logDir, 'logs.json'), JSON.stringify({
218
+ error: error.message,
219
+ jsonPath: config.jsonPath,
220
+ outputDir: config.outputDir,
221
+ viewport: config.viewport,
222
+ fullPage: config.fullPage,
223
+ debugBounds: config.debugBounds,
224
+ timeout: config.timeout,
225
+ waitTime: config.waitTime,
226
+ screenshotPath,
227
+ logs
228
+ }, null, 2), 'utf8');
229
+ }
230
+ catch (_) { }
231
+ error.logDir = logDir;
232
+ throw error;
233
+ }
234
+ finally {
235
+ if (browser)
236
+ await browser.close().catch(() => { });
237
+ if (server)
238
+ server.close();
239
+ if (tempDir)
240
+ await removeDir(tempDir, logs);
241
+ }
242
+ }
@@ -0,0 +1,17 @@
1
+ import CCNode from './cc/CCNode.js';
2
+ import CCScene from './cc/CCScene.js';
3
+ import CCSceneAsset from './cc/CCSceneAsset.js';
4
+ import { CCPrefab, CCPrefabInfo } from './cc/CCPrefab.js';
5
+ export function createPrefab(name = 'Prefab') {
6
+ const prefab = new CCPrefab();
7
+ const root = new CCNode(name);
8
+ prefab.setRoot(root);
9
+ return prefab;
10
+ }
11
+ export function createScene(name = 'NewScene') {
12
+ const asset = new CCSceneAsset();
13
+ const scene = new CCScene(name);
14
+ asset._scene = scene;
15
+ return asset;
16
+ }
17
+ export { CCNode, CCScene, CCSceneAsset, CCPrefab, CCPrefabInfo };
@@ -0,0 +1,81 @@
1
+ import * as crypto from 'crypto';
2
+ export function parseColor(color) {
3
+ if (!color)
4
+ return null;
5
+ if (color.startsWith('#')) {
6
+ const hex = color.slice(1);
7
+ if (hex.length === 6 || hex.length === 8) {
8
+ return {
9
+ r: parseInt(hex.slice(0, 2), 16),
10
+ g: parseInt(hex.slice(2, 4), 16),
11
+ b: parseInt(hex.slice(4, 6), 16),
12
+ a: hex.length === 8 ? parseInt(hex.slice(6, 8), 16) : 255
13
+ };
14
+ }
15
+ }
16
+ const rgbMatch = color.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
17
+ if (rgbMatch) {
18
+ return {
19
+ r: parseInt(rgbMatch[1]),
20
+ g: parseInt(rgbMatch[2]),
21
+ b: parseInt(rgbMatch[3]),
22
+ a: rgbMatch[4] ? Math.round(parseFloat(rgbMatch[4]) * 255) : 255
23
+ };
24
+ }
25
+ return null;
26
+ }
27
+ export function parseColorToCcColor(color) {
28
+ const parsed = parseColor(color);
29
+ if (!parsed)
30
+ return null;
31
+ return { __type__: 'cc.Color', ...parsed };
32
+ }
33
+ export function colorToHex(r, g, b, a = 255) {
34
+ return `#${r.toString(16).padStart(2, '0')}${g.toString(16).padStart(2, '0')}${b.toString(16).padStart(2, '0')}${a !== 255 ? a.toString(16).padStart(2, '0') : ''}`;
35
+ }
36
+ export function generateUUID() {
37
+ return crypto.randomUUID().replace(/-/g, '');
38
+ }
39
+ export function compressUUID(uuid) {
40
+ const hex = uuid.replace(/-/g, '');
41
+ const bytes = Buffer.from(hex, 'hex');
42
+ return bytes.toString('base64url').slice(0, 22);
43
+ }
44
+ export function generateCompressedUUID() {
45
+ return compressUUID(generateUUID());
46
+ }
47
+ export function generateId() {
48
+ const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
49
+ let result = '';
50
+ for (let i = 0; i < 22; i++) {
51
+ result += chars.charAt(Math.floor(Math.random() * chars.length));
52
+ }
53
+ return result;
54
+ }
55
+ export function parseOptions(args) {
56
+ const options = {};
57
+ for (let i = 0; i < args.length; i++) {
58
+ const arg = args[i];
59
+ if (arg.startsWith('--')) {
60
+ const key = arg.slice(2);
61
+ const nextArg = args[i + 1];
62
+ if (nextArg && !nextArg.startsWith('--')) {
63
+ options[key] = nextArg;
64
+ i++;
65
+ }
66
+ else {
67
+ options[key] = true;
68
+ }
69
+ }
70
+ }
71
+ return options;
72
+ }
73
+ export function outputJson(data) {
74
+ console.log(JSON.stringify(data, null, 2));
75
+ }
76
+ export function outputError(message) {
77
+ console.log(JSON.stringify({ error: message }));
78
+ }
79
+ export function outputSuccess(message) {
80
+ console.log(JSON.stringify({ success: message }));
81
+ }