clawdie-ai/scripts/setup-token.ts
Operator & Codex f04f35eb4e Clear setup token file after completion
---
Build: pass | Tests: pass — 2449 passed (182 files)
2026-05-12 16:18:08 +02:00

50 lines
1.5 KiB
TypeScript

#!/usr/bin/env tsx
import fs from 'fs';
import path from 'path';
import pg from 'pg';
import { OPS_DB_URL } from '../src/config.js';
import {
rotateBootstrapToken,
setupTokenFilePath,
writeSetupTokenFile,
} from '../src/postinstall-setup.js';
import { runSchemaMigration } from '../src/controlplane-db.js';
function usage(): never {
console.error('Usage: npm run setup-token -- rotate');
process.exit(2);
}
async function main(): Promise<void> {
const command = process.argv[2];
if (command !== 'rotate') usage();
const pool = new pg.Pool({ connectionString: OPS_DB_URL, max: 2 });
try {
await runSchemaMigration(pool);
const result = await rotateBootstrapToken(pool);
const tokenFile = setupTokenFilePath();
const shouldRefreshTokenFile =
Boolean(process.env.CLAWDIE_SETUP_TOKEN_FILE) ||
fs.existsSync(tokenFile) ||
fs.existsSync(path.dirname(tokenFile));
const tokenFileResult = shouldRefreshTokenFile
? writeSetupTokenFile(result.token, tokenFile)
: { ok: false, path: tokenFile };
console.log('Clawdie setup bootstrap token:');
console.log(result.token);
console.log(`Expires: ${result.expiresAt.toISOString()}`);
if (tokenFileResult.ok) console.log(`Token file updated: ${tokenFileResult.path}`);
console.log('Use this at /setup. It will be invalidated when setup completes.');
} finally {
await pool.end();
}
}
main().catch((err) => {
console.error(err instanceof Error ? err.message : String(err));
process.exit(1);
});