UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

164 lines (163 loc) 7.16 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); /** * Generate TypeScript types from Directus collections */ const NewCommand = { alias: ['t'], description: 'Generate TypeScript types', hidden: false, name: 'typegen', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l; // Retrieve the tools we need const { config, parameters, print: { error, info, spin, success }, prompt, system, } = toolbox; // Load configuration const ltConfig = config.loadConfig(); // Parse CLI arguments const cliUrl = parameters.options.url || parameters.options.u; const cliToken = parameters.options.token || parameters.options.t; const cliOutput = parameters.options.output || parameters.options.o; // Determine noConfirm with priority: CLI > command > global > default const noConfirm = config.getNoConfirm({ cliValue: parameters.options.noConfirm, commandConfig: (_b = (_a = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _a === void 0 ? void 0 : _a.directus) === null || _b === void 0 ? void 0 : _b.typegen, config: ltConfig, }); // Get configuration values const configUrl = (_e = (_d = (_c = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _c === void 0 ? void 0 : _c.directus) === null || _d === void 0 ? void 0 : _d.typegen) === null || _e === void 0 ? void 0 : _e.url; const configToken = (_h = (_g = (_f = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _f === void 0 ? void 0 : _f.directus) === null || _g === void 0 ? void 0 : _g.typegen) === null || _h === void 0 ? void 0 : _h.token; const configOutput = (_l = (_k = (_j = ltConfig === null || ltConfig === void 0 ? void 0 : ltConfig.commands) === null || _j === void 0 ? void 0 : _j.directus) === null || _k === void 0 ? void 0 : _k.typegen) === null || _l === void 0 ? void 0 : _l.output; // Determine values with priority: CLI > config > default/interactive let url; let token; let output; if (cliUrl) { url = cliUrl; } else if (configUrl) { url = configUrl; info(`Using Directus URL from lt.config: ${url}`); } else if (noConfirm) { url = 'http://localhost:8055'; info(`Using default Directus URL: ${url}`); } else { const urlResponse = yield prompt.ask({ initial: 'http://localhost:8055', message: 'Enter Directus API URL:', name: 'url', type: 'input', }); url = urlResponse.url; } if (!url) { error('Directus URL is required!'); return; } if (cliToken) { token = cliToken; } else if (configToken) { token = configToken; info('Using Directus token from lt.config'); } else if (noConfirm) { error('Directus token is required (use --token or configure in lt.config)!'); return; } else { const tokenResponse = yield prompt.ask({ message: 'Enter Directus API token (needs Administrator permissions):', name: 'token', type: 'password', }); token = tokenResponse.token; } if (!token) { error('Directus token is required!'); return; } if (cliOutput) { output = cliOutput; } else if (configOutput) { output = configOutput; info(`Using output path from lt.config: ${output}`); } else if (noConfirm) { output = './directus-schema.ts'; info(`Using default output path: ${output}`); } else { const outputResponse = yield prompt.ask({ initial: './directus-schema.ts', message: 'Enter output file path:', name: 'output', type: 'input', }); output = outputResponse.output; } if (!output) { error('Output path is required!'); return; } // Check if directus-sdk-typegen is installed globally const hasTypegen = system.which('directus-sdk-typegen'); let useGlobalTypegen = Boolean(hasTypegen); if (!hasTypegen) { info('directus-sdk-typegen is not installed globally.'); if (noConfirm) { info('Installing directus-sdk-typegen globally...'); } else { const shouldInstall = yield prompt.confirm('Would you like to install directus-sdk-typegen globally?'); if (!shouldInstall) { info('Using npx/dlx instead...'); useGlobalTypegen = false; } else { const installSpin = spin('Installing directus-sdk-typegen'); yield system.run(toolbox.pm.globalInstall('directus-sdk-typegen')); installSpin.succeed(); useGlobalTypegen = true; } } } // Generate types const generateSpin = spin('Generating TypeScript types from Directus'); const command = useGlobalTypegen ? `directus-sdk-typegen -u "${url}" -t "${token}" -o "${output}"` : `${toolbox.pm.exec(`directus-sdk-typegen -u "${url}" -t "${token}" -o "${output}"`)}`; try { yield system.run(command); generateSpin.succeed(); success(`TypeScript types generated successfully at ${output}`); } catch (err) { generateSpin.fail(); error('Failed to generate types. Please check your URL and token.'); if (err instanceof Error) { error(err.message); } return; } // Exit if not running from menu if (!toolbox.parameters.options.fromGluegunMenu) { process.exit(); } // For tests return `generated directus types at ${output}`; }), }; exports.default = NewCommand;