@lenne.tech/cli
Version:
lenne.Tech CLI: lt
87 lines (86 loc) • 4.93 kB
JavaScript
;
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 AddComponentCommand = {
description: 'Add Nuxt component',
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.components) === null || _b === void 0 ? void 0 : _b.add,
config: ltConfig,
});
const componentName = parameters.first;
yield addComponent(toolbox, componentName, noConfirm);
if (!parameters.options.fromGluegunMenu) {
process.exit();
}
return `added component ${componentName || 'selected'}`;
}),
};
function addComponent(toolbox_1, componentName_1) {
return __awaiter(this, arguments, void 0, function* (toolbox, componentName, noConfirm = false) {
const { print, prompt } = toolbox;
try {
const compSpinner = print.spin('Load component selection from GitHub...');
const possibleComponents = yield (0, nuxt_base_components_1.getFileInfo)('components');
compSpinner.succeed('Components selection successfully loaded from GitHub');
if (possibleComponents.length > 0) {
let selectedComponent = '';
if (!componentName) {
const response = yield prompt.ask({
choices: possibleComponents,
message: 'Which component would you like to add?',
name: 'componentType',
type: 'select',
});
selectedComponent = response.componentType;
}
else {
const foundComponent = possibleComponents.find((e) => e.name.toLowerCase() === `${componentName.toLowerCase()}.vue` ||
e.name.toLowerCase() === componentName.toLowerCase());
selectedComponent = (foundComponent === null || foundComponent === void 0 ? void 0 : foundComponent.name) || componentName;
}
const selectedFile = possibleComponents.find((e) => e.name.toLowerCase() === selectedComponent.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)('components', 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, 'components', 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 component ${selectedFile.name} was selected.`);
yield (0, nuxt_base_components_1.copyFile)(selectedFile, toolbox, 'components', noConfirm);
}
}
else {
print.error('No components found on GitHub.');
}
}
catch (error) {
print.error(`Error when adding/selecting the component: ${error.message}`);
}
});
}
exports.default = AddComponentCommand;