Skip to content
Source
Sign inSign up
samwillis/neon-glow-demo
Open History to see the last commit

capture-token.ts

17 lines934 Bytes

1import { config, parse } from 'dotenv';
2import { readFile, writeFile } from 'node:fs/promises';
3import { hostname } from 'node:os';
4import { signGrant } from '../services/api/src/tokens.ts';
5​
6// Mints a capture-worker grant. It authorizes only claiming and completing capture jobs.
7config({ path: '.env.local', quiet: true });
8const workerId = process.argv[2] ?? `dev-${hostname().split('.')[0]}`.slice(0, 80);
9const days = Number(process.argv[3] ?? 30);
10const token = await signGrant('capture-worker', { workerId }, days * 86_400);
11if (process.argv.includes('--print')) console.log(token);
12else {
13 const local = parse(await readFile('.env.local'));
14 local.GLOW_CAPTURE_WORKER_TOKEN = token;
15 await writeFile('.env.local', Object.entries(local).map(([key, value]) => `${key}=${JSON.stringify(value)}`).join('\n') + '\n', { mode: 0o600 });
16 console.log(`Saved a ${days}-day capture worker token for ${workerId} to .env.local.`);