sin-github-issues / scripts /render-github-app-env-template.mjs
OpenCode
Deploy latest SIN-GitHub-Issues runtime
cd455f6
Raw
History Blame Contribute Delete
3.18 kB
#!/usr/bin/env node
import { readFile } from 'node:fs/promises';
import path from 'node:path';
const cwd = process.cwd();
function parseArgs(argv) {
const out = {};
for (let i = 0; i < argv.length; i += 1) {
const token = argv[i];
if (!token.startsWith('--')) continue;
const key = token.slice(2);
const next = argv[i + 1];
if (!next || next.startsWith('--')) {
out[key] = 'true';
continue;
}
out[key] = next;
i += 1;
}
return out;
}
function firstString(...values) {
for (const value of values) {
if (typeof value === 'string' && value.trim()) return value.trim();
}
return null;
}
function parseList(value) {
if (!value || typeof value !== 'string') return [];
return value.split(',').map((item) => item.trim()).filter(Boolean);
}
function asPlaceholder(name) {
return `<SET_${name}>`;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
const format = firstString(args.format, 'dotenv');
const configPath = path.resolve(cwd, args.config || process.env.SIN_GITHUB_APP_ROUTING_PATH || './config/github-app-routing.local.json');
const selectedAgents = new Set(parseList(args.agents || args.agent));
const raw = await readFile(configPath, 'utf8');
const config = JSON.parse(raw);
const apps = Array.isArray(config.apps) ? config.apps : [];
const envEntries = new Map([
['SIN_GITHUB_APP_ROUTING_PATH', './config/github-app-routing.local.json'],
['SIN_GITHUB_API_BASE_URL', 'https://api.github.com'],
]);
const appSummaries = [];
for (const app of apps) {
const agentSlug = firstString(app.agentSlug, app.preferredAgentSlug, app.appSlug);
const appSlug = firstString(app.appSlug, agentSlug);
if (selectedAgents.size > 0 && !selectedAgents.has(agentSlug || '') && !selectedAgents.has(appSlug || '')) continue;
const trackedKeys = [
firstString(app.appIdEnv),
firstString(app.clientIdEnv),
firstString(app.privateKeyEnv),
firstString(app.webhookSecretEnv),
].filter(Boolean);
for (const key of trackedKeys) envEntries.set(key, asPlaceholder(key));
appSummaries.push({
agentSlug,
appSlug,
botName: firstString(app.botName),
routePath: firstString(app.routePath, app.webhookPath, config.defaultWebhookPath, '/github/webhook'),
});
}
if (format === 'json') {
process.stdout.write(JSON.stringify({ configPath, env: Object.fromEntries(envEntries), appSummaries }, null, 2) + '\n');
return;
}
const lines = [];
if (format === 'shell') {
for (const [key, value] of envEntries.entries()) lines.push(`export ${key}=${JSON.stringify(value)}`);
} else {
for (const [key, value] of envEntries.entries()) lines.push(`${key}=${value}`);
}
if (appSummaries.length > 0) {
lines.push('');
lines.push('# app summaries');
for (const summary of appSummaries) {
lines.push(`${summary.agentSlug} -> ${summary.botName || summary.appSlug} -> ${summary.routePath}`);
}
}
process.stdout.write(lines.join('\n') + '\n');
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
});