UNPKG

@kubb/cli

Version:

Command-line interface for Kubb, enabling easy generation of TypeScript, React-Query, Zod, and other code from OpenAPI specifications.

311 lines (300 loc) • 8.89 kB
'use strict'; var chunkMKHKV2HH_cjs = require('./chunk-MKHKV2HH.cjs'); var citty = require('citty'); var utils = require('consola/utils'); var utils$1 = require('@kubb/core/utils'); var cosmiconfig = require('cosmiconfig'); var jiti = require('jiti'); var logger = require('@kubb/core/logger'); var path = require('path'); var process = require('process'); var core = require('@kubb/core'); var open = require('open'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n.default = e; return Object.freeze(n); } var path__default = /*#__PURE__*/_interopDefault(path); var process__namespace = /*#__PURE__*/_interopNamespace(process); var open__default = /*#__PURE__*/_interopDefault(open); // src/commands/generate.ts chunkMKHKV2HH_cjs.init_cjs_shims(); // src/utils/getConfig.ts chunkMKHKV2HH_cjs.init_cjs_shims(); // src/utils/getPlugins.ts chunkMKHKV2HH_cjs.init_cjs_shims(); function isJSONPlugins(plugins) { return !!plugins?.some((plugin) => { return Array.isArray(plugin) && typeof plugin?.at(0) === "string"; }); } function isObjectPlugins(plugins) { return plugins instanceof Object && !Array.isArray(plugins); } function getPlugins(plugins) { if (isObjectPlugins(plugins)) { throw new Error("Object plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json"); } if (isJSONPlugins(plugins)) { throw new Error("JSON plugins are not supported anymore, best to use http://kubb.dev/getting-started/configure#json"); } return Promise.resolve(plugins); } // src/utils/getConfig.ts async function getConfig(result, args2) { const config = result?.config; let kubbUserConfig = Promise.resolve(config); if (typeof config === "function") { const possiblePromise = config(args2); if (utils$1.isPromise(possiblePromise)) { kubbUserConfig = possiblePromise; } kubbUserConfig = Promise.resolve(possiblePromise); } let JSONConfig = await kubbUserConfig; if (Array.isArray(JSONConfig)) { const results = []; for (const item of JSONConfig) { const plugins = item.plugins ? await getPlugins(item.plugins) : void 0; results.push({ ...item, plugins }); } return results; } JSONConfig = { ...JSONConfig, plugins: JSONConfig.plugins ? await getPlugins(JSONConfig.plugins) : void 0 }; return JSONConfig; } // src/utils/getCosmiConfig.ts chunkMKHKV2HH_cjs.init_cjs_shims(); var tsLoader = async (configFile) => { const jiti$1 = jiti.createJiti(chunkMKHKV2HH_cjs.importMetaUrl, { jsx: { runtime: "automatic", importSource: "@kubb/react" }, sourceMaps: true }); const mod = await jiti$1.import(configFile, { default: true }); return mod; }; async function getCosmiConfig(moduleName, config) { const searchPlaces = [ "package.json", `.${moduleName}rc`, `.${moduleName}rc.json`, `.${moduleName}rc.yaml`, `.${moduleName}rc.yml`, `.${moduleName}rc.ts`, `.${moduleName}rc.js`, `.${moduleName}rc.mjs`, `.${moduleName}rc.cjs`, `${moduleName}.config.ts`, `${moduleName}.config.js`, `${moduleName}.config.mjs`, `${moduleName}.config.cjs` ]; const explorer = cosmiconfig.cosmiconfig(moduleName, { cache: false, searchPlaces: [ ...searchPlaces.map((searchPlace) => { return `.config/${searchPlace}`; }), ...searchPlaces.map((searchPlace) => { return `configs/${searchPlace}`; }), ...searchPlaces ], loaders: { ".ts": tsLoader } }); const result = config ? await explorer.load(config) : await explorer.search(); if (result?.isEmpty || !result || !result.config) { throw new Error("Config not defined, create a kubb.config.js or pass through your config with the option --config"); } return result; } // src/utils/watcher.ts chunkMKHKV2HH_cjs.init_cjs_shims(); async function startWatcher(path2, cb) { const { watch } = await import('chokidar'); const logger$1 = logger.createLogger(); const ignored = "**/{.git,node_modules}/**"; const watcher = watch(path2, { ignorePermissionErrors: true, ignored }); watcher.on("all", (type, file) => { logger$1?.emit("info", utils.colors.yellow(utils.colors.bold(`Change detected: ${type} ${file}`))); try { cb(path2); } catch (_e) { logger$1?.emit("warning", utils.colors.red("Watcher failed")); } }); } var args = { config: { type: "string", description: "Path to the Kubb config", alias: "c" }, logLevel: { type: "string", description: "Info, silent or debug", alias: "l", default: "info", valueHint: "silent|info|debug" }, watch: { type: "boolean", description: "Watch mode based on the input file", alias: "w", default: false }, debug: { type: "boolean", description: "Override logLevel to debug", alias: "d", default: false }, ui: { type: "boolean", description: "Open ui", alias: "u", default: false }, help: { type: "boolean", description: "Show help", alias: "h", default: false } }; var command = citty.defineCommand({ meta: { name: "generate", description: "[input] Generate files based on a 'kubb.config.ts' file" }, args, async run(commandContext) { let name = ""; const progressCache = /* @__PURE__ */ new Map(); const { args: args2 } = commandContext; const input = args2._[0]; if (args2.help) { return citty.showUsage(command); } if (args2.debug) { args2.logLevel = "debug"; } const logLevel = logger.LogMapper[args2.logLevel] || 3; const logger$1 = logger.createLogger({ logLevel }); const { generate } = await import('./generate-J27CF4EQ.cjs'); logger$1.emit("start", "Loading config"); const result = await getCosmiConfig("kubb", args2.config); logger$1.emit("success", `Config loaded(${utils.colors.dim(path__default.default.relative(process__namespace.cwd(), result.filepath))})`); const config = await getConfig(result, args2); const start = async () => { if (Array.isArray(config)) { const promiseManager = new core.PromiseManager(); const promises = config.map((c) => () => { name = c.name || ""; progressCache.clear(); return generate({ input, config: c, args: args2, progressCache }); }); await promiseManager.run("seq", promises); return; } progressCache.clear(); await generate({ input, config, progressCache, args: args2 }); return; }; if (args2.ui) { const { startServer } = await import('@kubb/ui'); await startServer( { stop: () => process__namespace.exit(1), restart: () => start(), getMeta: () => { const entries = [...progressCache.entries()]; const percentages = entries.reduce( (acc, [key, singleBar]) => { acc[key] = singleBar.getProgress(); return acc; }, {} ); return { name, percentages }; } }, (info) => { const url = `${info.address}:${info.port}`.replace("::", "http://localhost"); logger$1.consola?.start(`Starting ui on ${url}`); open__default.default(url); } ); } if (args2.watch) { if (Array.isArray(config)) { throw new Error("Cannot use watcher with multiple Configs(array)"); } if (core.isInputPath(config)) { return startWatcher([input || config.input.path], async (paths) => { await start(); logger$1.emit("start", utils.colors.yellow(utils.colors.bold(`Watching for changes in ${paths.join(" and ")}`))); }); } } await start(); if (globalThis.isDevtoolsEnabled) { const canRestart = await logger$1.consola?.prompt("Restart(could be used to validate the profiler)?", { type: "confirm", initial: false }); if (canRestart) { await start(); } else { process__namespace.exit(1); } } } }); var generate_default = command; module.exports = generate_default; //# sourceMappingURL=generate-YT6MCGVO.cjs.map //# sourceMappingURL=generate-YT6MCGVO.cjs.map