#!/usr/bin/env node import * as p from '@clack/prompts'; import color from 'picocolors'; import { askAccessToken } from './prompts/asks'; import minimist from 'minimist'; import { Modes } from './types/types'; import { handleCreateInteractive, handleCreateNonInteractive, handleDeleteNonInteractive, handleGetActive, handleGetAll, handleGetHelp, handleGetHistory, handleGetStatusAsync, handleStartNewRun, handleStartNewRunNonInteractive, handleStopRun, handleStopRunNonInteractive } from './eventHandlers/handlers'; import dotenv from 'dotenv'; import { initalizeEnvironmentVariables } from './api/auth'; import { init_NODE_ENV_BLINQ } from './utils/general'; import { TempCache } from './cache/TempCache'; dotenv.config({ quiet: true }); export const cache = new TempCache<'userById' | 'userByToken' | 'projectByToken' | 'testData', string, any>(); async function handleNonInteractiveMode( mode: Modes, args: minimist.ParsedArgs ) { switch (mode) { case 'create': await handleCreateNonInteractive(args); break; case 'delete': await handleDeleteNonInteractive(args); break; case 'startRun': await handleStartNewRunNonInteractive(args); break; case 'status': await handleGetStatusAsync(args); break; case 'stopRun': await handleStopRunNonInteractive(args); break; case 'getAll': await handleGetAll(); break; case 'getActive': await handleGetActive(); break; case 'getHistory': await handleGetHistory(args.count ?? 5); break; default: p.note('Unknown mode'); } } async function mainMenu(userName: string): Promise { let isFirstTime = true; while (true) { const choice = await p.select({ message: isFirstTime ? `Welcome ${color.cyan(userName)}, what would you like to do?` : 'What would you like to do next?', options: [ { value: 'getActive', label: 'View active executions' }, { value: 'getAll', label: 'Show all executions' }, { value: 'create', label: 'Create a new execution' }, { value: 'startRun', label: 'Trigger a new execution' }, { value: 'stopRun', label: 'Stop a running execution' }, { value: 'getHistory', label: 'View completed executions' }, { value: 'exit', label: 'Exit' } ], }); if (p.isCancel(choice)) { p.cancel('Op cancelled.'); process.exit(0); } switch (choice) { case 'getActive': await handleGetActive(); break; case 'getAll': await handleGetAll(); break; case 'create': await handleCreateInteractive(); break; case 'startRun': await handleStartNewRun(); break; case 'stopRun': await handleStopRun(); break; case 'getHistory': await handleGetHistory(5); break; case 'exit': p.outro('Goodbye!'); process.exit(0); } isFirstTime = false; } } async function main() { const args = minimist(process.argv.slice(2)); const token = process.env.TOKEN ?? args.token; const mode = args.mode as Modes | undefined; const debug = args.debug || false; process.env.DEBUG = debug ? 'true' : 'false'; process.env.NODE_ENV_BLINQ = args.NODE_ENV_BLINQ ?? 'prod'; let count = 0; for (const arg of (args._ || [])) { count == 0 && console.log('⚙️ Test Data Overwrite:'); count++; const [key, ...rest] = String(arg).split("="); if (rest.length > 0) { const value = rest.join("="); cache.set('testData', key, value); console.log(` ${count}. ${key}=${value}`) } } let userName: string; handleGetHelp(args); init_NODE_ENV_BLINQ(); if (mode) { if (!token) { p.outro(color.red('Error: You must supply a token via the TOKEN env variable or --token CLI flag in non-interactive mode.')); process.exit(1); } const validation = await initalizeEnvironmentVariables(token); if (!validation.valid) { p.outro(color.red('Invalid token provided via env/flag.')); process.exit(1); } userName = validation.userName ?? 'user'; await handleNonInteractiveMode(mode, args); process.exit(0); } else { userName = await askAccessToken(); await mainMenu(userName); } } main().catch(err => { p.outro(color.red(err instanceof Error ? err.message : String(err))); });