UNPKG

@launchql/cli

Version:
132 lines (129 loc) 6.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = require("fs"); const path_1 = require("path"); const js_yaml_1 = __importDefault(require("js-yaml")); const codegen_1 = require("@launchql/codegen"); const server_1 = require("@launchql/server"); const usage = ` LaunchQL Codegen: lql codegen [OPTIONS] Options: --help, -h Show this help message --config <path> Config file (json|yaml) --schema <path> Schema SDL file path --endpoint <url> GraphQL endpoint to fetch schema via introspection --headerHost <host> Optional Host header to send with endpoint requests --auth <token> Optional Authorization header value (e.g., "Bearer 123") --header "Name: Value" Optional HTTP header; repeat to add multiple headers --out <dir> Output root directory (default: packages/launchql-gen/dist) --format <gql|ts> Document format (default: gql) --convention <style> Filename convention (dashed|underscore|camelcase|camelUpper) --emitTypes <bool> Emit types (default: true) --emitOperations <bool> Emit operations (default: true) --emitSdk <bool> Emit sdk (default: true) --allowQuery <name> Only generate for this root field (repeatable) --excludeQuery <name> Exclude this root field (repeatable) --excludePattern <regex> Exclude fields matching regex (repeatable) `; function parseBool(v, d) { if (v === undefined) return d; if (typeof v === 'boolean') return v; const s = String(v).toLowerCase(); if (s === 'true') return true; if (s === 'false') return false; return d; } async function loadConfig(path) { const content = await fs_1.promises.readFile(path, 'utf8'); if (/\.ya?ml$/i.test(path)) return js_yaml_1.default.load(content); if (/\.json$/i.test(path)) return JSON.parse(content); return {}; } exports.default = async (argv, _prompter, _options) => { if (argv.help || argv.h) { console.log(usage); process.exit(0); } const cwd = argv.cwd || process.cwd(); const configPath = argv.config || ''; let fileOpts = {}; if (configPath) fileOpts = await loadConfig(configPath); const overrides = {}; if (argv.schema) overrides.input = { ...(overrides.input || {}), schema: String(argv.schema) }; if (argv.endpoint) overrides.input = { ...(overrides.input || {}), endpoint: String(argv.endpoint) }; const headerHost = argv.headerHost ?? ''; const auth = argv.auth ?? ''; const headerArg = argv.header; const headerList = Array.isArray(headerArg) ? headerArg : headerArg ? [headerArg] : []; const headers = {}; if (auth) headers['Authorization'] = auth; for (const h of headerList) { const idx = typeof h === 'string' ? h.indexOf(':') : -1; if (idx <= 0) continue; const name = h.slice(0, idx).trim(); const value = h.slice(idx + 1).trim(); if (!name) continue; headers[name] = value; } if (Object.keys(headers).length) overrides.input = { ...(overrides.input || {}), headers }; if (argv.out) overrides.output = { ...(overrides.output || {}), root: String(argv.out) }; if (argv.format) overrides.documents = { ...(overrides.documents || {}), format: String(argv.format) }; if (argv.convention) overrides.documents = { ...(overrides.documents || {}), convention: String(argv.convention) }; const allowQueryArg = argv.allowQuery; const excludeQueryArg = argv.excludeQuery; const excludePatternArg = argv.excludePattern; const allowQueries = Array.isArray(allowQueryArg) ? allowQueryArg : allowQueryArg ? [String(allowQueryArg)] : []; const excludeQueries = Array.isArray(excludeQueryArg) ? excludeQueryArg : excludeQueryArg ? [String(excludeQueryArg)] : []; const excludePatterns = Array.isArray(excludePatternArg) ? excludePatternArg : excludePatternArg ? [String(excludePatternArg)] : []; if (allowQueries.length || excludeQueries.length || excludePatterns.length) { overrides.documents = { ...(overrides.documents || {}), allowQueries, excludeQueries, excludePatterns }; } const emitTypes = parseBool(argv.emitTypes, true); const emitOperations = parseBool(argv.emitOperations, true); const emitSdk = parseBool(argv.emitSdk, true); overrides.features = { emitTypes, emitOperations, emitSdk }; const merged = (0, codegen_1.mergeLaunchQLGenOptions)(codegen_1.defaultLaunchQLGenOptions, fileOpts); const finalOptions = (0, codegen_1.mergeLaunchQLGenOptions)(merged, overrides); if (finalOptions.input.endpoint && headerHost) { const opts = {}; if (headerHost) opts.headerHost = headerHost; if (auth) opts.auth = auth; if (Object.keys(headers).length) opts.headers = headers; const sdl = await server_1.fetchEndpointSchemaSDL(String(finalOptions.input.endpoint), opts); const tmpSchemaPath = (0, path_1.join)(cwd, '.lql-codegen-schema.graphql'); await fs_1.promises.writeFile(tmpSchemaPath, sdl, 'utf8'); finalOptions.input.schema = tmpSchemaPath; finalOptions.input.endpoint = ''; } const hasSchema = !!finalOptions.input.schema && String(finalOptions.input.schema).trim() !== ''; const hasEndpoint = !!finalOptions.input.endpoint && String(finalOptions.input.endpoint).trim() !== ''; if (!hasSchema && !hasEndpoint) { console.error('Missing --schema or --endpoint or config.input'); process.exit(1); } const result = await (0, codegen_1.runCodegen)(finalOptions, cwd); console.log(`Generated at ${(0, path_1.join)(result.root)}`); };