Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/builder/@types/protected/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ export const enum BuildExitCode {
BUILD_FAILED = 34,
BUILD_SUCCESS = 0,
BUILD_BUSY = 37,
STATIC_COMPILE_ERROR = 38,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schema definition and its description in the API also need to be updated.

UNKNOWN_ERROR = 50,
}

Expand Down
1 change: 1 addition & 0 deletions src/core/builder/error-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const BuildErrorMap = {
[BuildExitCode.BUILD_FAILED]: '构建失败,请参考错误日志排查错误原因',
[BuildExitCode.PARAM_ERROR]: '构建参数错误,请参考错误日志调整构建参数后重试',
[BuildExitCode.UNKNOWN_ERROR]: '未知错误,请联系 cocos 官方',
[BuildExitCode.STATIC_COMPILE_ERROR]: '静态编译检查失败,发现 assets 相关的 TypeScript 错误',
};

export default BuildErrorMap;
7 changes: 6 additions & 1 deletion src/core/builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ export async function build<P extends Platform>(platformTmp: P, options?: IBuild
const duration = formatMSTime(Date.now() - startTime);
newConsole.error(error);
newConsole.buildComplete(platform, duration, false);
return { code: BuildExitCode.BUILD_FAILED, reason: 'Build failed! ' + String(error) };
// 如果错误对象包含 code 属性,使用该错误码(如 500)
let errorCode = error?.code && typeof error.code === 'number' ? error.code as BuildExitCode : BuildExitCode.BUILD_FAILED;
if (errorCode === BuildExitCode.BUILD_SUCCESS) {
errorCode = BuildExitCode.BUILD_FAILED;
}
return { code: errorCode as Exclude<BuildExitCode, BuildExitCode.BUILD_SUCCESS>, reason: error?.message || String(error) };
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/core/builder/worker/builder/asset-handler/script/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MacroItem } from '../../../../../engine/@types/config';
import { compressUuid } from '../../utils';
import project from '../../../../../project';
import { runStaticCompileCheck } from './static-compile-check';
import { BuildExitCode } from '../../../../@types/protected';
type PlatformType = StatsQuery.ConstantManager.PlatformType;

interface IScriptProjectOption extends SharedSettings {
Expand Down Expand Up @@ -134,9 +135,13 @@ export class ScriptBuilder {
// 执行静态编译检查
// 注意:如果在 BuildCommand 中已经执行过,这里会重复执行。
// 但为了确保脚本编译的安全性,这里强制检查。
const checkPassed = await runStaticCompileCheck(project.path, true);
if (!checkPassed) {
console.warn('⚠ Warning: Found assets-related TypeScript errors. Build will continue...');
const checkResult = await runStaticCompileCheck(project.path, true);
if (!checkResult.passed) {
// 构建失败,抛出错误,错误码为 500
const errorMessage = checkResult.errorMessage || 'Found assets-related TypeScript errors';
const error = new Error(errorMessage);
(error as any).code = BuildExitCode.STATIC_COMPILE_ERROR;
throw error;
}

const cceModuleMap = script.queryCCEModuleMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ function filterAssetsErrors(output: string): string {
* 执行静态编译检查
* @param projectPath 项目路径
* @param showOutput 是否显示输出信息(默认 true)
* @returns 返回 true 表示检查通过(没有 assets 相关错误),false 表示有错误
* @returns 返回对象,包含检查结果和错误信息。passed 为 true 表示检查通过(没有 assets 相关错误),false 表示有错误
*/
export async function runStaticCompileCheck(projectPath: string, showOutput: boolean = true): Promise<boolean> {
export async function runStaticCompileCheck(projectPath: string, showOutput: boolean = true): Promise<{ passed: boolean; errorMessage?: string }> {
if (showOutput) {
console.log(chalk.blue('Running TypeScript static compile check...'));
console.log(chalk.gray(`Project: ${projectPath}`));
Expand Down Expand Up @@ -119,7 +119,7 @@ export async function runStaticCompileCheck(projectPath: string, showOutput: boo
if (showOutput) {
console.log(chalk.green('✓ No assets-related TypeScript errors found!'));
}
return true;
return { passed: true };
}

// 过滤出包含 "assets" 的错误
Expand All @@ -130,13 +130,13 @@ export async function runStaticCompileCheck(projectPath: string, showOutput: boo
if (showOutput) {
console.log(filteredOutput);
}
return false;
return { passed: false, errorMessage: filteredOutput };
} else {
// 没有 assets 相关的错误
if (showOutput) {
console.log(chalk.green('✓ No assets-related TypeScript errors found!'));
}
return true;
return { passed: true };
}
} catch (error: any) {
// execAsync 在命令返回非零退出码时会抛出错误
Expand All @@ -152,7 +152,7 @@ export async function runStaticCompileCheck(projectPath: string, showOutput: boo
if (showOutput) {
console.log(chalk.green('✓ No assets-related TypeScript errors found!'));
}
return true;
return { passed: true };
}

// 过滤出包含 "assets" 的错误
Expand All @@ -163,13 +163,13 @@ export async function runStaticCompileCheck(projectPath: string, showOutput: boo
if (showOutput) {
console.log(filteredOutput);
}
return false;
return { passed: false, errorMessage: filteredOutput };
} else {
// 没有 assets 相关的错误
if (showOutput) {
console.log(chalk.green('✓ No assets-related TypeScript errors found!'));
}
return true;
return { passed: true };
}
}
}
Loading