SPB Git

Repository Public

global-setup.ts 1.6 KB 32 lines typescript

1import { execFileSync } from 'node:child_process';2import fs from 'node:fs';3import { DATA_DIR, DB_NAME, KEEP, REPO_ROOT, SCRATCH_DIR, SPEND_LEDGER, WORKSPACE_ROOT } from './env';45/** Fresh instance: stop the stack, recreate the dev database, wipe the dev data / workspace dirs, start again. */6export default function globalSetup(): void {7  const run = (cmd: string, args: string[]): void => {8    execFileSync(cmd, args, { cwd: REPO_ROOT, stdio: ['ignore', 'inherit', 'inherit'] });9  };10  run('bash', ['scripts/dev-down.sh']);11  if (KEEP) {12    // partial re-run on the existing instance (administrator, projects and missions are kept)13    run('bash', ['scripts/dev-up.sh', '--no-build']);14    return;15  }16  // Ledger of real-model spend across resets (the database that holds it is about to be dropped).17  try {18    const usd = execFileSync('psql', ['-h', '/tmp', '-d', DB_NAME, '-Atc', 'SELECT coalesce(sum(usd), 0) FROM spend_days'], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim();19    fs.appendFileSync(SPEND_LEDGER, `${new Date().toISOString()} ${usd}\n`);20  } catch {21    /* no database yet */22  }23  if (!/^agent_os_dev[a-z0-9_]*$/.test(DB_NAME)) throw new Error(`refusing to drop database "${DB_NAME}"`);24  run('dropdb', ['-h', '/tmp', '--if-exists', '--force', DB_NAME]);25  for (const dir of [DATA_DIR, WORKSPACE_ROOT, SCRATCH_DIR]) {26    if (!dir.startsWith('/tmp/aos-dev-') || dir.includes('..')) throw new Error(`refusing to remove "${dir}"`);27    fs.rmSync(dir, { recursive: true, force: true });28  }29  fs.mkdirSync(SCRATCH_DIR, { recursive: true });30  run('bash', ['scripts/dev-up.sh', '--no-build']);31}32
Files main
Menu

Theme