Files
AutoGPT/autogpt_platform/frontend/scripts/generate-api-queries.ts
Ubbe 063dc5cf65 refactor(frontend): standardise with environment service (#11209)
## Changes 🏗️

Standardize all the runtime environment checks on the Front-end and
associated conditions to run against a single environment service where
all the environment config is centralized and hence easier to manage.

This helps prevent typos and bug when manually asserting against
environment variables ( which are typed as `string` ), the helper
functions are easier to read and re-use across the codebase.

## 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] Run the app and click around
  - [x] Everything is smooth
  - [x] Test on the CI and types are green  

### For configuration changes:

None 🙏🏽
2025-10-21 08:44:34 +00:00

78 lines
2.0 KiB
JavaScript

#!/usr/bin/env node
import { execSync } from "child_process";
import * as path from "path";
import * as fs from "fs";
import * as os from "os";
import { environment } from "@/services/environment";
function fetchOpenApiSpec(): void {
const args = process.argv.slice(2);
const forceFlag = args.includes("--force");
const baseUrl = environment.getAGPTServerBaseUrl();
const openApiUrl = `${baseUrl}/openapi.json`;
const outputPath = path.join(
__dirname,
"..",
"src",
"app",
"api",
"openapi.json",
);
console.log(`Output path: ${outputPath}`);
console.log(`Force flag: ${forceFlag}`);
// Check if local file exists
const localFileExists = fs.existsSync(outputPath);
if (!forceFlag && localFileExists) {
console.log("✅ Using existing local OpenAPI spec file");
console.log("💡 Use --force flag to fetch from server");
return;
}
if (!localFileExists) {
console.log("📄 No local OpenAPI spec found, fetching from server...");
} else {
console.log(
"🔄 Force flag detected, fetching fresh OpenAPI spec from server...",
);
}
console.log(`Fetching OpenAPI spec from: ${openApiUrl}`);
// Write to a temporary file first to avoid clearing the real file on failure
const tmpOutputPath = path.join(
os.tmpdir(),
`openapi-fetch-${Date.now()}.json`,
);
try {
// Fetch the OpenAPI spec to a temp file
execSync(`curl "${openApiUrl}" -o "${tmpOutputPath}"`, {
stdio: "inherit",
});
// Format with prettier
execSync(`prettier --write "${tmpOutputPath}"`, { stdio: "inherit" });
// Move temp file to final output path
fs.copyFileSync(tmpOutputPath, outputPath);
fs.unlinkSync(tmpOutputPath);
console.log("✅ OpenAPI spec fetched and formatted successfully");
} catch (error) {
if (fs.existsSync(tmpOutputPath)) {
fs.unlinkSync(tmpOutputPath);
}
console.error("❌ Failed to fetch OpenAPI spec:", error);
process.exit(1);
}
}
if (require.main === module) {
fetchOpenApiSpec();
}