UNPKG

@withkeystone/cli

Version:

Keystone CLI - Test automation for modern web apps

96 lines 4.88 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.initCommand = void 0; const commander_1 = require("commander"); const chalk_1 = __importDefault(require("chalk")); const inquirer_1 = __importDefault(require("inquirer")); const PKCEAuthenticator_js_1 = require("../auth/PKCEAuthenticator.js"); const DeviceFlowAuthenticator_js_1 = require("../auth/DeviceFlowAuthenticator.js"); const TokenStorage_js_1 = require("../auth/TokenStorage.js"); const AuthenticatedClient_js_1 = require("../auth/AuthenticatedClient.js"); exports.initCommand = new commander_1.Command('init') .description('Initialize Keystone CLI and authenticate') .option('--no-browser', 'Use device flow instead of browser authentication') .option('--api-url <url>', 'API URL', process.env.KEYSTONE_API_URL || 'https://api.withkeystone.com') .option('--frontend-url <url>', 'Frontend URL', process.env.KEYSTONE_FRONTEND_URL || 'https://app.withkeystone.com') .action(async (options) => { console.log(chalk_1.default.blue.bold('\nWelcome to Keystone CLI!\n')); const storage = new TokenStorage_js_1.TokenStorage(options.apiUrl); const client = new AuthenticatedClient_js_1.AuthenticatedClient(options.apiUrl); // Check if already authenticated if (await client.isAuthenticated()) { try { const session = await client.getSession(); if (session) { console.log(chalk_1.default.green(`✓ Already authenticated as ${session.email}`)); console.log(chalk_1.default.green(`✓ Organization: ${session.organization_name || 'No organization'}\n`)); const { reconfigure } = await inquirer_1.default.prompt([{ type: 'confirm', name: 'reconfigure', message: 'Would you like to reconfigure?', default: false }]); if (!reconfigure) return; } } catch { // Token is invalid, continue with auth console.log(chalk_1.default.yellow('Existing authentication is invalid. Please authenticate again.\n')); } } try { let tokens; const config = { apiUrl: options.apiUrl, appUrl: options.frontendUrl }; // Check if we're in an SSH session or if browser is disabled const isSSH = !!process.env.SSH_CONNECTION; const useBrowser = !options.noBrowser && !isSSH; if (useBrowser) { console.log(chalk_1.default.gray('Using browser authentication...\n')); const auth = new PKCEAuthenticator_js_1.PKCEAuthenticator(config); tokens = await auth.authenticate(); console.log("TOKENS", tokens); } else { if (isSSH) { console.log("IS SSH"); console.log(chalk_1.default.gray('Detected SSH session. Using device flow authentication...\n')); } else { console.log(chalk_1.default.gray('Using device flow authentication...\n')); } console.log("USING DEVICE FLOW"); const auth = new DeviceFlowAuthenticator_js_1.DeviceFlowAuthenticator(config); tokens = await auth.authenticate(); } // Save tokens await storage.saveTokens(tokens); console.log(chalk_1.default.gray('Tokens saved successfully')); // Validate tokens by making API call const newClient = new AuthenticatedClient_js_1.AuthenticatedClient(options.apiUrl); console.log(chalk_1.default.gray('Validating authentication...')); let session; try { session = await newClient.getSession(); if (!session) { throw new Error('Failed to validate authentication - no session returned'); } console.log(chalk_1.default.gray('Authentication validated successfully')); } catch (error) { console.error(chalk_1.default.gray('Session validation error:'), error); throw new Error('Failed to validate authentication'); } console.log(chalk_1.default.green(`\n✓ Successfully authenticated as ${session.email}`)); console.log(chalk_1.default.green(`✓ Organization: ${session.organization_name || 'No organization'}`)); console.log(chalk_1.default.gray('\nYou can now use the Keystone CLI to run tests and manage your test suites.\n')); } catch (error) { console.error(chalk_1.default.red('\n✗ Authentication failed:'), error instanceof Error ? error.message : error); process.exit(1); } }); //# sourceMappingURL=init.js.map