Files
openclaw/scripts/generate-bundled-provider-auth-env-vars.mjs
2026-03-24 15:48:35 +00:00

84 lines
2.8 KiB
JavaScript

import path from "node:path";
import { collectBundledPluginSources } from "./lib/bundled-plugin-source-utils.mjs";
import { reportGeneratedOutputCli, writeGeneratedOutput } from "./lib/generated-output-utils.mjs";
const GENERATED_BY = "scripts/generate-bundled-provider-auth-env-vars.mjs";
const DEFAULT_OUTPUT_PATH = "src/plugins/bundled-provider-auth-env-vars.generated.ts";
function normalizeProviderAuthEnvVars(providerAuthEnvVars) {
if (
!providerAuthEnvVars ||
typeof providerAuthEnvVars !== "object" ||
Array.isArray(providerAuthEnvVars)
) {
return [];
}
return Object.entries(providerAuthEnvVars)
.map(([providerId, envVars]) => {
const normalizedProviderId = providerId.trim();
const normalizedEnvVars = Array.isArray(envVars)
? envVars.map((value) => String(value).trim()).filter(Boolean)
: [];
if (!normalizedProviderId || normalizedEnvVars.length === 0) {
return null;
}
return [normalizedProviderId, normalizedEnvVars];
})
.filter(Boolean)
.toSorted(([left], [right]) => left.localeCompare(right));
}
export function collectBundledProviderAuthEnvVars(params = {}) {
const repoRoot = path.resolve(params.repoRoot ?? process.cwd());
const entries = new Map();
for (const source of collectBundledPluginSources({ repoRoot })) {
for (const [providerId, envVars] of normalizeProviderAuthEnvVars(
source.manifest.providerAuthEnvVars,
)) {
entries.set(providerId, envVars);
}
}
return Object.fromEntries(
[...entries.entries()].toSorted(([left], [right]) => left.localeCompare(right)),
);
}
export function renderBundledProviderAuthEnvVarModule(entries) {
const renderedEntries = Object.entries(entries)
.map(([providerId, envVars]) => {
const renderedKey = /^[$A-Z_a-z][\w$]*$/u.test(providerId)
? providerId
: JSON.stringify(providerId);
const renderedEnvVars = envVars.map((value) => JSON.stringify(value)).join(", ");
return ` ${renderedKey}: [${renderedEnvVars}],`;
})
.join("\n");
return `// Auto-generated by ${GENERATED_BY}. Do not edit directly.
export const BUNDLED_PROVIDER_AUTH_ENV_VAR_CANDIDATES = {
${renderedEntries}
} as const satisfies Record<string, readonly string[]>;
`;
}
export function writeBundledProviderAuthEnvVarModule(params = {}) {
const repoRoot = path.resolve(params.repoRoot ?? process.cwd());
const next = renderBundledProviderAuthEnvVarModule(
collectBundledProviderAuthEnvVars({ repoRoot }),
);
return writeGeneratedOutput({
repoRoot,
outputPath: params.outputPath ?? DEFAULT_OUTPUT_PATH,
next,
check: params.check,
});
}
reportGeneratedOutputCli({
importMetaUrl: import.meta.url,
label: "bundled-provider-auth-env-vars",
run: ({ check }) => writeBundledProviderAuthEnvVarModule({ check }),
});