delqhi/sin-github-issues
0
1#!/usr/bin/env node2 3import { readFile } from 'node:fs/promises';4import path from 'node:path';5 6const cwd = process.cwd();7 8function parseArgs(argv) {9 const out = {};10 for (let i = 0; i < argv.length; i += 1) {11 const token = argv[i];12 if (!token.startsWith('--')) continue;13 const key = token.slice(2);14 const next = argv[i + 1];15 if (!next || next.startsWith('--')) {16 out[key] = 'true';17 continue;18 }19 out[key] = next;20 i += 1;21 }22 return out;23}24 25function firstString(...values) {26 for (const value of values) {27 if (typeof value === 'string' && value.trim()) return value.trim();28 }29 return null;30}31 32function parseList(value) {33 if (!value || typeof value !== 'string') return [];34 return value.split(',').map((item) => item.trim()).filter(Boolean);35}36 37function asPlaceholder(name) {38 return `<SET_${name}>`;39}40 41async function main() {42 const args = parseArgs(process.argv.slice(2));43 const format = firstString(args.format, 'dotenv');44 const configPath = path.resolve(cwd, args.config || process.env.SIN_GITHUB_APP_ROUTING_PATH || './config/github-app-routing.local.json');45 const selectedAgents = new Set(parseList(args.agents || args.agent));46 const raw = await readFile(configPath, 'utf8');47 const config = JSON.parse(raw);48 const apps = Array.isArray(config.apps) ? config.apps : [];49 50 const envEntries = new Map([51 ['SIN_GITHUB_APP_ROUTING_PATH', './config/github-app-routing.local.json'],52 ['SIN_GITHUB_API_BASE_URL', 'https://api.github.com'],53 ]);54 const appSummaries = [];55 56 for (const app of apps) {57 const agentSlug = firstString(app.agentSlug, app.preferredAgentSlug, app.appSlug);58 const appSlug = firstString(app.appSlug, agentSlug);59 if (selectedAgents.size > 0 && !selectedAgents.has(agentSlug || '') && !selectedAgents.has(appSlug || '')) continue;60 61 const trackedKeys = [62 firstString(app.appIdEnv),63 firstString(app.clientIdEnv),64 firstString(app.privateKeyEnv),65 firstString(app.webhookSecretEnv),66 ].filter(Boolean);67 68 for (const key of trackedKeys) envEntries.set(key, asPlaceholder(key));69 70 appSummaries.push({71 agentSlug,72 appSlug,73 botName: firstString(app.botName),74 routePath: firstString(app.routePath, app.webhookPath, config.defaultWebhookPath, '/github/webhook'),75 });76 }77 78 if (format === 'json') {79 process.stdout.write(JSON.stringify({ configPath, env: Object.fromEntries(envEntries), appSummaries }, null, 2) + '\n');80 return;81 }82 83 const lines = [];84 if (format === 'shell') {85 for (const [key, value] of envEntries.entries()) lines.push(`export ${key}=${JSON.stringify(value)}`);86 } else {87 for (const [key, value] of envEntries.entries()) lines.push(`${key}=${value}`);88 }89 if (appSummaries.length > 0) {90 lines.push('');91 lines.push('# app summaries');92 for (const summary of appSummaries) {93 lines.push(`${summary.agentSlug} -> ${summary.botName || summary.appSlug} -> ${summary.routePath}`);94 }95 }96 process.stdout.write(lines.join('\n') + '\n');97}98 99main().catch((error) => {100 console.error(error instanceof Error ? error.message : String(error));101 process.exit(1);102});103 