UNPKG

@launchql/cli

Version:
95 lines (94 loc) 3.46 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.commands = void 0; const find_and_require_package_json_1 = require("find-and-require-package-json"); const pgpm_1 = require("pgpm"); const codegen_1 = __importDefault(require("./commands/codegen")); const explorer_1 = __importDefault(require("./commands/explorer")); const get_graphql_schema_1 = __importDefault(require("./commands/get-graphql-schema")); const server_1 = __importDefault(require("./commands/server")); const utils_1 = require("./utils"); const createCommandMap = (skipPgTeardown = false) => { const pgpmCommands = (0, pgpm_1.createPgpmCommandMap)(skipPgTeardown); return { ...pgpmCommands, server: server_1.default, explorer: explorer_1.default, 'get-graphql-schema': get_graphql_schema_1.default, codegen: codegen_1.default }; }; const commands = async (argv, prompter, options) => { let { first: command, newArgv } = (0, utils_1.extractFirst)(argv); // Run update check early so it shows on help/version paths too try { const pkg = (0, find_and_require_package_json_1.findAndRequirePackageJson)(__dirname); await (0, pgpm_1.checkForUpdates)({ command: command || 'help', pkgName: pkg.name, pkgVersion: pkg.version, toolName: 'lql', key: pkg.name, updateCommand: `Run npm i -g ${pkg.name}@latest to upgrade.` }); } catch { // ignore update check failures } if (argv.version || argv.v) { const pkg = (0, find_and_require_package_json_1.findAndRequirePackageJson)(__dirname); console.log(pkg.version); process.exit(0); } // Show usage if explicitly requested but no command specified if ((argv.help || argv.h || command === 'help') && !command) { console.log(utils_1.usageText); process.exit(0); } // Show usage for help command specifically if (command === 'help') { console.log(utils_1.usageText); process.exit(0); } // Command-specific help for init if (command === 'init' && (argv.help || argv.h)) { console.log((0, pgpm_1.createInitUsageText)('lql', 'LaunchQL')); process.exit(0); } const commandMap = createCommandMap(options?.skipPgTeardown); // Prompt if no command provided if (!command) { const answer = await prompter.prompt(argv, [ { type: 'autocomplete', name: 'command', message: 'What do you want to do?', options: Object.keys(commandMap) } ]); command = answer.command; } // Prompt for working directory newArgv = await prompter.prompt(newArgv, [ { type: 'text', name: 'cwd', message: 'Working directory', required: false, default: process.cwd(), useDefault: true } ]); const commandFn = commandMap[command]; if (!commandFn) { console.log(utils_1.usageText); await (0, utils_1.cliExitWithError)(`Unknown command: ${command}`); } await commandFn(newArgv, prompter, options); prompter.close(); return argv; }; exports.commands = commands;