UNPKG

@albinberisha/codex-cli

Version:

Codex cli to scaffold plugins with Node

205 lines (162 loc) 5.65 kB
import arg from 'arg' import inquirer from 'inquirer'; import showHelp from './showHelp.js'; import { getApiKey } from './getApiKey.js'; import { getTemplates } from './getTemplates.js'; import { initializeWatcher } from './initializeWatcher.js'; import { returnEnvironment } from './getEnvironment.js'; import { createJsonFile, writeJsonFile } from './jsonFileCommands.js'; import { getEnvParams, setEnvParams } from './envParams.js'; import command from './utils/queries.js'; import open from 'open'; import { returnSuccessMessage, returnErrorMessage } from './utils/returnMessage.js'; import process from 'node:process'; const commands = ['create-plugin', 'run-session'] async function cli(args) { parseArgumentsIntoOptions(args); } async function parseArgumentsIntoOptions(rawArgs) { const args = arg( { '--name': String, '--sessionId': String, '--apiKey': String, '--environment': String, '--git': Boolean, '--help': Boolean, '-n': '--name', '-s': '--sessionId', '-k': '--apiKey', '-e': '--environment', '-g': '--git', '-h': '--help', }, { argv: rawArgs.slice(2), } ); const argumentsFormatted = { name: args['--name'] || '', sessionId: args['--sessionId'] || null, apiKey: args['--apiKey'] || null, git: args['--git'] || false, help: args['--help'] || false, environment: args['--environment'] || 'Production', template: '', } argumentsFormatted.help ?? await showHelpAndClose(); if (args._.length <= 0) { console.log(`codex-cli: See 'codex-cli --help'.`); await showHelpAndClose(1); } if (!args._.some(c => commands.includes(c))) { console.log(`codex-cli: ${args._[0]} is not a valid command. See 'codex-cli --help'.`); await showHelpAndClose(1); } if (argumentsFormatted.environment) { const env = await returnEnvironment(argumentsFormatted.environment); if (!env) { returnErrorMessage(`There is no environment with name: ${argumentsFormatted.environment}`); process.exit(1); } } if (args._.includes('create-plugin')) { await invokeCreatePlugin(argumentsFormatted); process.exit(0); } if (args._.includes('run-session')) { await invokeRunSession(argumentsFormatted); } } async function showHelpAndClose(numOfErr) { await showHelp(); process.exit(numOfErr); } async function invokeCreatePlugin(options) { await createJsonFile(); process.env.ENVIRONMENT = options.environment; await setApiKey(options); await writeJsonFile(); await setEnvParams(); //In case we need to add other parameters // options = await promptForMissingOptions(options); await getTemplates(); return options; } async function invokeRunSession(options) { process.on('SIGINT', async () => { await command.terminateSession(); process.exit(0); }); await setEnvParams(); const envParams = await getEnvParams(); if (!envParams.apiKey) { await setApiKey(options); } await setSession(); await initializeWatcher(); } async function setSession() { const res = await command.createSession(); process.env.SESSION_ID = res.sessionId; returnSuccessMessage(`Running on url: ${res.codexPreviewUrl}`); open(res.codexPreviewUrl); await writeJsonFile(); await setEnvParams(); } async function setApiKey(options) { if (options.apiKey == null || options.apiKey == undefined) { const apiKey = await getApiKey(); if (apiKey === '') { returnErrorMessage('Api Key was not validated, can\'t fetch templates! \n'); process.exit(1); } options.apiKey = apiKey; } process.env.API_KEY = options.apiKey; } async function promptForMissingOptions(options) { const questions = []; if (!options.template) { questions.push({ type: 'list', name: 'template', message: 'Please choose which plugin template to use: ', choices: ['Blank Template', 'Default Template', 'Standard Template'] }); } if (options.name === '') { questions.push({ type: 'input', name: 'name', message: 'Type a name for the plugin: ', validate: (input) => { if (input.length < 3) { return "Name must have at least 3 chars. (kebab-case)"; } else { const kebabCase = string => string .replace(/([a-z])([A-Z])/g, "$1-$2") .replace(/[\s_]+/g, '-'); options.name = kebabCase(input); } return true; }, }); } if (!options.git) { questions.push({ type: 'confirm', name: 'git', message: 'Initialize a git repository? ', default: false, }); } const answers = await inquirer.prompt(questions); return { ...options, template: options.template || answers.template, git: options.git || answers.git, name: options.name || answers.name } } export default cli