Skip to content

Commit def0084

Browse files
authored
feat(frontend): Preserve openapi.json spec file on generation failure (#10791)
`openapi.json` file is cleared when script fails to retrieve api spec from the server. This shouldn't happen and it breaks building docker containers. ### Changes 🏗️ Use temp file during generation to prevent actual file clearing on failure. ### Checklist 📋 #### For code changes: - [x] I have clearly listed my changes in the PR description - [x] I have made a test plan - [x] I have tested my changes according to the test plan: - [x] Spec file doesn't get cleared on failure - [x] Spec file is correctly generated - [x] Works when frontend is run in docker container
1 parent 916d0ad commit def0084

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

autogpt_platform/frontend/scripts/generate-api-queries.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { getAgptServerBaseUrl } from "@/lib/env-config";
44
import { execSync } from "child_process";
55
import * as path from "path";
66
import * as fs from "fs";
7+
import * as os from "os";
78

89
function fetchOpenApiSpec(): void {
910
const args = process.argv.slice(2);
@@ -42,15 +43,30 @@ function fetchOpenApiSpec(): void {
4243

4344
console.log(`Fetching OpenAPI spec from: ${openApiUrl}`);
4445

46+
// Write to a temporary file first to avoid clearing the real file on failure
47+
const tmpOutputPath = path.join(
48+
os.tmpdir(),
49+
`openapi-fetch-${Date.now()}.json`,
50+
);
51+
4552
try {
46-
// Fetch the OpenAPI spec
47-
execSync(`curl "${openApiUrl}" > "${outputPath}"`, { stdio: "inherit" });
53+
// Fetch the OpenAPI spec to a temp file
54+
execSync(`curl "${openApiUrl}" -o "${tmpOutputPath}"`, {
55+
stdio: "inherit",
56+
});
4857

4958
// Format with prettier
50-
execSync(`prettier --write "${outputPath}"`, { stdio: "inherit" });
59+
execSync(`prettier --write "${tmpOutputPath}"`, { stdio: "inherit" });
60+
61+
// Move temp file to final output path
62+
fs.copyFileSync(tmpOutputPath, outputPath);
63+
fs.unlinkSync(tmpOutputPath);
5164

5265
console.log("✅ OpenAPI spec fetched and formatted successfully");
5366
} catch (error) {
67+
if (fs.existsSync(tmpOutputPath)) {
68+
fs.unlinkSync(tmpOutputPath);
69+
}
5470
console.error("❌ Failed to fetch OpenAPI spec:", error);
5571
process.exit(1);
5672
}

0 commit comments

Comments
 (0)