/** * Context management commands * @generated by @constructive-io/graphql-codegen * DO NOT EDIT - changes will be overwritten */ import { CLIOptions, Inquirerer, extractFirst } from 'inquirerer'; import { getStore } from '../executor'; const usage = '\ncsdk context \n\nCommands:\n create Create a new context\n list List all contexts\n use Set the active context\n current Show current context\n delete Delete a context\n\nCreate Options:\n --endpoint GraphQL endpoint URL\n\n --help, -h Show this help message\n'; export default async ( argv: Partial>, prompter: Inquirerer, _options: CLIOptions ) => { if (argv.help || argv.h) { console.log(usage); process.exit(0); } const store = getStore(); const { first: subcommand, newArgv } = extractFirst(argv); if (!subcommand) { const answer = await prompter.prompt(argv, [ { type: 'autocomplete', name: 'subcommand', message: 'What do you want to do?', options: ['create', 'list', 'use', 'current', 'delete'], }, ]); return handleSubcommand(answer.subcommand as string, newArgv, prompter, store); } return handleSubcommand(subcommand, newArgv, prompter, store); }; async function handleSubcommand( subcommand: string, argv: Partial>, prompter: Inquirerer, store: ReturnType ) { switch (subcommand) { case 'create': return handleCreate(argv, prompter, store); case 'list': return handleList(store); case 'use': return handleUse(argv, prompter, store); case 'current': return handleCurrent(store); case 'delete': return handleDelete(argv, prompter, store); default: console.log(usage); process.exit(1); } } async function handleCreate( argv: Partial>, prompter: Inquirerer, store: ReturnType ) { const { first: name, newArgv: restArgv } = extractFirst(argv); const answers = (await prompter.prompt( { name, ...restArgv, }, [ { type: 'text', name: 'name', message: 'Context name', required: true, }, { type: 'text', name: 'endpoint', message: 'GraphQL endpoint URL', required: true, }, ] )) as unknown as Record; const contextName = answers.name; const endpoint = answers.endpoint; store.createContext(contextName, { endpoint: endpoint, }); const settings = store.loadSettings(); if (!settings.currentContext) { store.setCurrentContext(contextName); } console.log(`Created context: ${contextName}`); console.log(` Endpoint: ${endpoint}`); } function handleList(store: ReturnType) { const contexts = store.listContexts(); const settings = store.loadSettings(); if (contexts.length === 0) { console.log('No contexts configured.'); return; } console.log('Contexts:'); for (const ctx of contexts) { const marker = ctx.name === settings.currentContext ? '* ' : ' '; const authStatus = store.hasValidCredentials(ctx.name) ? '[authenticated]' : '[no token]'; console.log(`${marker}${ctx.name} ${authStatus}`); console.log(` Endpoint: ${ctx.endpoint}`); } } async function handleUse( argv: Partial>, prompter: Inquirerer, store: ReturnType ) { const { first: name } = extractFirst(argv); const contexts = store.listContexts(); if (contexts.length === 0) { console.log('No contexts configured.'); return; } let contextName = name; if (!contextName) { const answer = await prompter.prompt(argv, [ { type: 'autocomplete', name: 'name', message: 'Select context', options: contexts.map((c) => c.name), }, ]); contextName = answer.name as string; } if (store.setCurrentContext(contextName)) { console.log(`Switched to context: ${contextName}`); } else { console.error(`Context "${contextName}" not found.`); process.exit(1); } } function handleCurrent(store: ReturnType) { const current = store.getCurrentContext(); if (!current) { console.log('No current context set.'); return; } console.log(`Current context: ${current.name}`); console.log(` Endpoint: ${current.endpoint}`); const hasAuth = store.hasValidCredentials(current.name); console.log(` Auth: ${hasAuth ? 'authenticated' : 'not authenticated'}`); } async function handleDelete( argv: Partial>, prompter: Inquirerer, store: ReturnType ) { const { first: name } = extractFirst(argv); const contexts = store.listContexts(); if (contexts.length === 0) { console.log('No contexts configured.'); return; } let contextName = name; if (!contextName) { const answer = await prompter.prompt(argv, [ { type: 'autocomplete', name: 'name', message: 'Select context to delete', options: contexts.map((c) => c.name), }, ]); contextName = answer.name as string; } if (store.deleteContext(contextName)) { console.log(`Deleted context: ${contextName}`); } else { console.error(`Context "${contextName}" not found.`); process.exit(1); } }