UNPKG

@lenne.tech/cli

Version:

lenne.Tech CLI: lt

87 lines (86 loc) 4.76 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 }); const nuxt_base_components_1 = require("../../lib/nuxt-base-components"); const AddBlockCommand = { description: 'Add Nuxt block', name: 'add', run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; const { config, parameters } = toolbox; // Load configuration const ltConfig = config.loadConfig(); // Determine noConfirm with priority: CLI > config > global > default (false) 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.blocks) === null || _b === void 0 ? void 0 : _b.add, config: ltConfig, }); const blockName = parameters.first; yield addBlock(toolbox, blockName, noConfirm); if (!parameters.options.fromGluegunMenu) { process.exit(); } return `added block ${blockName || 'selected'}`; }), }; function addBlock(toolbox_1, blockName_1) { return __awaiter(this, arguments, void 0, function* (toolbox, blockName, noConfirm = false) { const { print, prompt } = toolbox; try { const compSpinner = print.spin('Load block selection from GitHub...'); const possibleBlocks = yield (0, nuxt_base_components_1.getFileInfo)('blocks'); compSpinner.succeed('Blocks selection successfully loaded from GitHub'); if (possibleBlocks.length > 0) { let selectedBlock = ''; if (!blockName) { const response = yield prompt.ask({ choices: possibleBlocks, message: 'Which block would you like to add?', name: 'blockType', type: 'select', }); selectedBlock = response.blockType; } else { const foundBlock = possibleBlocks.find((e) => e.name.toLowerCase() === `${blockName.toLowerCase()}.vue` || e.name.toLowerCase() === blockName.toLowerCase()); selectedBlock = (foundBlock === null || foundBlock === void 0 ? void 0 : foundBlock.name) || blockName; } const selectedFile = possibleBlocks.find((e) => e.name.toLowerCase() === selectedBlock.toLowerCase()); if ((selectedFile === null || selectedFile === void 0 ? void 0 : selectedFile.type) === 'dir') { print.success(`The directory ${selectedFile.name} has been selected.`); const directoryFiles = yield (0, nuxt_base_components_1.getFileInfo)('blocks', selectedFile.name); if (directoryFiles.length > 0) { for (const file of directoryFiles) { yield (0, nuxt_base_components_1.copyFile)({ name: `${selectedFile.name}/${file.name}`, type: 'dir' }, toolbox, 'blocks', noConfirm); } print.success(`All files from the directory ${selectedFile.name} have been successfully copied.`); } else { print.error(`The directory ${selectedFile.name} is empty.`); } } else if ((selectedFile === null || selectedFile === void 0 ? void 0 : selectedFile.type) === 'file') { print.success(`The block ${selectedFile.name} was selected.`); yield (0, nuxt_base_components_1.copyFile)(selectedFile, toolbox, 'blocks', noConfirm); } } else { print.error('No block found on GitHub.'); } } catch (error) { print.error(`Error when adding/selecting the block: ${error.message}`); } }); } exports.default = AddBlockCommand;