feat: 将发布预检移入内置浏览器
This commit is contained in:
@@ -125,6 +125,30 @@ function sendPublishSourceFailure(
|
||||
sendJson(res, 200, { success: false, status, code, error });
|
||||
}
|
||||
|
||||
const SAFE_PUBLISH_PREFLIGHT_CODES = new Set([
|
||||
'PREVIEW_REQUIRED',
|
||||
'PUBLISH_PREFLIGHT_LOAD_FAILED',
|
||||
'PUBLISH_PREFLIGHT_RUNTIME_ERROR',
|
||||
'PUBLISH_PREFLIGHT_BLANK',
|
||||
'PUBLISH_PREFLIGHT_TIMEOUT',
|
||||
]);
|
||||
const SAFE_PUBLISH_PREFLIGHT_MESSAGES: Record<string, string> = {
|
||||
PREVIEW_REQUIRED: '请先在 Makelore 内置浏览器中打开当前项目预览。',
|
||||
PUBLISH_PREFLIGHT_LOAD_FAILED: '作品主页无法打开。',
|
||||
PUBLISH_PREFLIGHT_RUNTIME_ERROR: '作品打开时发生了运行错误。',
|
||||
PUBLISH_PREFLIGHT_BLANK: '作品打开后没有可见内容。',
|
||||
PUBLISH_PREFLIGHT_TIMEOUT: '作品打开检查超时。',
|
||||
};
|
||||
|
||||
function isSafePublishPreflightError(
|
||||
error: unknown,
|
||||
): error is Error & { code: string } {
|
||||
return error instanceof Error
|
||||
&& 'code' in error
|
||||
&& typeof (error as { code?: unknown }).code === 'string'
|
||||
&& SAFE_PUBLISH_PREFLIGHT_CODES.has((error as { code: string }).code);
|
||||
}
|
||||
|
||||
async function sendPublishSourceUpstreamError(
|
||||
res: ServerResponse,
|
||||
response: Response,
|
||||
@@ -826,6 +850,16 @@ async function handlePublishProjectSource(
|
||||
const temporaryDirectory = await mkdtemp(join(tmpdir(), 'makelore-publish-'));
|
||||
const archivePath = join(temporaryDirectory, 'project.zip');
|
||||
try {
|
||||
if (!ctx.agentBrowser) {
|
||||
sendPublishSourceFailure(
|
||||
res,
|
||||
400,
|
||||
'PREVIEW_REQUIRED',
|
||||
'请先在 Makelore 内置浏览器中打开当前项目预览。',
|
||||
);
|
||||
return;
|
||||
}
|
||||
await ctx.agentBrowser.preflightCurrentProject(localProject.path);
|
||||
const packageSummary = await createStaticProjectPackage({
|
||||
projectPath: localProject.path,
|
||||
archivePath,
|
||||
@@ -1115,9 +1149,13 @@ export async function handleWorksRoutes(
|
||||
const isPackageError = error instanceof ProjectPackageError;
|
||||
sendPublishSourceFailure(
|
||||
res,
|
||||
isPackageError ? 400 : 503,
|
||||
isPackageError ? error.code : 'WORKS_SQUARE_UNAVAILABLE',
|
||||
isPackageError ? error.message : '发布服务暂时不可用,请稍后重试。',
|
||||
isPackageError || isSafePublishPreflightError(error) ? 400 : 503,
|
||||
isPackageError || isSafePublishPreflightError(error) ? error.code : 'WORKS_SQUARE_UNAVAILABLE',
|
||||
isPackageError
|
||||
? error.message
|
||||
: isSafePublishPreflightError(error)
|
||||
? SAFE_PUBLISH_PREFLIGHT_MESSAGES[error.code]
|
||||
: '发布服务暂时不可用,请稍后重试。',
|
||||
);
|
||||
} else if (isProjectStatus) {
|
||||
sendPublishSourceFailure(
|
||||
|
||||
Reference in New Issue
Block a user