This commit is contained in:
yhh
2025-12-26 23:21:17 +08:00
3 changed files with 84 additions and 47 deletions

View File

@@ -1,5 +1,14 @@
# @esengine/cli # @esengine/cli
## 1.2.1
### Patch Changes
- [#352](https://github.com/esengine/esengine/pull/352) [`33e98b9`](https://github.com/esengine/esengine/commit/33e98b9a750f9fe684c36f1937c1afa38da36315) Thanks [@esengine](https://github.com/esengine)! - fix(cli): 修复 Cocos Creator 3.x 项目检测逻辑
- 优先检查 package.json 中的 creator.version 字段
- 添加 .creator 和 settings 目录检测
- 重构检测代码,提取通用辅助函数
## 1.2.0 ## 1.2.0
### Minor Changes ### Minor Changes

View File

@@ -1,6 +1,6 @@
{ {
"name": "@esengine/cli", "name": "@esengine/cli",
"version": "1.2.0", "version": "1.2.1",
"description": "CLI tool for adding ESEngine ECS framework to existing projects", "description": "CLI tool for adding ESEngine ECS framework to existing projects",
"type": "module", "type": "module",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -26,52 +26,68 @@ function printLogo(): void {
console.log(); console.log();
} }
// =============================================================================
// 项目检测 | Project Detection
// =============================================================================
/** /**
* @zh 检测是否存在 *.laya 文件 * @zh 检查文件或目录是否存在
* @en Check if *.laya file exists * @en Check if file or directory exists
*/ */
function hasLayaProjectFile(cwd: string): boolean { const exists = (cwd: string, ...paths: string[]): boolean =>
paths.some(p => fs.existsSync(path.join(cwd, p)));
/**
* @zh 安全读取 JSON 文件
* @en Safely read JSON file
*/
function readJson<T = Record<string, unknown>>(filePath: string): T | null {
try { try {
const files = fs.readdirSync(cwd); return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
return files.some(f => f.endsWith('.laya')); } catch {
return null;
}
}
/**
* @zh 检查目录中是否有匹配后缀的文件
* @en Check if directory contains files with matching extension
*/
function hasFileWithExt(cwd: string, ext: string): boolean {
try {
return fs.readdirSync(cwd).some(f => f.endsWith(ext));
} catch { } catch {
return false; return false;
} }
} }
/** /**
* @zh 检测 Cocos Creator 版本 * @zh 从 package.json 获取 Cocos Creator 版本
* @en Detect Cocos Creator version * @en Get Cocos Creator version from package.json
*/ */
function detectCocosVersion(cwd: string): 'cocos' | 'cocos2' | null { function getCocosVersionFromPackage(cwd: string): string | null {
// Cocos 3.x: 检查 cc.config.json 或 extensions 目录 const pkg = readJson<{ creator?: { version?: string } }>(path.join(cwd, 'package.json'));
if (fs.existsSync(path.join(cwd, 'cc.config.json')) || return pkg?.creator?.version ?? null;
fs.existsSync(path.join(cwd, 'extensions'))) { }
return 'cocos';
}
// 检查 project.json 中的版本号 /**
const projectJsonPath = path.join(cwd, 'project.json'); * @zh 从 project.json 获取 Cocos 2.x 版本
if (fs.existsSync(projectJsonPath)) { * @en Get Cocos 2.x version from project.json
try { */
const project = JSON.parse(fs.readFileSync(projectJsonPath, 'utf-8')); function getCocos2VersionFromProject(cwd: string): string | null {
// Cocos 2.x project.json 有 engine-version 字段 const project = readJson<{ 'engine-version'?: string; engine?: string }>(
if (project['engine-version'] || project.engine) { path.join(cwd, 'project.json')
const version = project['engine-version'] || project.engine || ''; );
// 2.x 版本格式: "cocos-creator-js-2.4.x" 或 "2.4.x" return project?.['engine-version'] ?? project?.engine ?? null;
if (version.includes('2.') || version.startsWith('2')) { }
return 'cocos2';
}
}
// 有 project.json 但没有版本信息,假设是 3.x
return 'cocos';
} catch {
// 解析失败,假设是 3.x
return 'cocos';
}
}
return null; /**
* @zh 判断版本号属于哪个大版本
* @en Determine major version from version string
*/
function getMajorVersion(version: string): number | null {
const match = version.match(/^(\d+)\./);
return match ? parseInt(match[1], 10) : null;
} }
/** /**
@@ -79,23 +95,35 @@ function detectCocosVersion(cwd: string): 'cocos' | 'cocos2' | null {
* @en Detect project type * @en Detect project type
*/ */
function detectProjectType(cwd: string): PlatformType | null { function detectProjectType(cwd: string): PlatformType | null {
// Laya: 检查 *.laya 文件.laya 目录laya.json // Laya: *.laya 文件.laya 目录laya.json
if (hasLayaProjectFile(cwd) || if (hasFileWithExt(cwd, '.laya') || exists(cwd, '.laya', 'laya.json')) {
fs.existsSync(path.join(cwd, '.laya')) ||
fs.existsSync(path.join(cwd, 'laya.json'))) {
return 'laya'; return 'laya';
} }
// Cocos Creator: 检查 assets 目录 // Cocos Creator: 检查 package.json 中的 creator.version
if (fs.existsSync(path.join(cwd, 'assets'))) { const cocosVersion = getCocosVersionFromPackage(cwd);
const cocosVersion = detectCocosVersion(cwd); if (cocosVersion) {
if (cocosVersion) { const major = getMajorVersion(cocosVersion);
return cocosVersion; if (major === 2) return 'cocos2';
} if (major && major >= 3) return 'cocos';
} }
// Node.js: 检查 package.json // Cocos 3.x: .creator 目录、settings 目录、cc.config.json、extensions 目录
if (fs.existsSync(path.join(cwd, 'package.json'))) { if (exists(cwd, '.creator', 'settings', 'cc.config.json', 'extensions')) {
return 'cocos';
}
// Cocos 2.x: project.json 中的 engine-version
const cocos2Version = getCocos2VersionFromProject(cwd);
if (cocos2Version) {
if (cocos2Version.includes('2.') || cocos2Version.startsWith('2')) {
return 'cocos2';
}
return 'cocos';
}
// Node.js: 有 package.json 但不是 Cocos
if (exists(cwd, 'package.json')) {
return 'nodejs'; return 'nodejs';
} }