@lenne.tech/cli
Version:
lenne.Tech CLI: lt
177 lines (176 loc) • 7.22 kB
JavaScript
"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 });
/**
* Initialize a new lt.config file
*/
const InitCommand = {
alias: ['i'],
description: 'Initialize lt.config file',
hidden: false,
name: 'init',
run: (toolbox) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const { config, filesystem, parameters, print: { error, info, success }, prompt: { ask, confirm }, } = toolbox;
// Load configuration
const ltConfig = config.loadConfig();
// 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.config) === null || _b === void 0 ? void 0 : _b.init,
config: ltConfig,
});
// Check for existing config files
const cwd = filesystem.cwd();
const configFiles = ['lt.config.json', 'lt.config.yaml', 'lt.config'];
const existingConfig = configFiles.find((f) => filesystem.exists(filesystem.path(cwd, f)));
if (existingConfig) {
if (!noConfirm) {
const overwrite = yield confirm(`${existingConfig} already exists. Overwrite?`, false);
if (!overwrite) {
info('Configuration initialization cancelled.');
return;
}
}
// Remove existing config file before creating new one
filesystem.remove(filesystem.path(cwd, existingConfig));
}
// Get configuration options from CLI or interactive mode
const { controller, format: cliFormat, frontend, git: initGit, interactive = true } = parameters.options;
const newConfig = {
commands: {},
meta: {
version: '1.0.0',
},
};
let format = 'json';
if (interactive) {
// Interactive mode - ask for configuration
info('Creating lt.config configuration...');
info('');
// Ask for file format
const formatChoice = yield ask([
{
choices: ['json', 'yaml'],
initial: 0,
message: 'Configuration file format?',
name: 'format',
type: 'select',
},
]);
format = formatChoice.format;
// Ask for server module configuration
const serverConfig = yield ask([
{
initial: true,
message: 'Configure server module defaults?',
name: 'configureServer',
type: 'confirm',
},
]);
if (serverConfig.configureServer) {
const serverOptions = yield ask([
{
choices: ['Rest', 'GraphQL', 'Both', 'auto'],
initial: 2, // Both
message: 'Default controller type for new modules?',
name: 'controller',
type: 'select',
},
{
initial: false,
message: 'Skip lint by default?',
name: 'skipLint',
type: 'confirm',
},
]);
newConfig.commands.server = {
addProp: {
skipLint: serverOptions.skipLint,
},
module: {
controller: serverOptions.controller,
skipLint: serverOptions.skipLint,
},
object: {
skipLint: serverOptions.skipLint,
},
};
}
// Ask for fullstack configuration
const fullstackConfig = yield ask([
{
initial: false,
message: 'Configure fullstack defaults?',
name: 'configureFullstack',
type: 'confirm',
},
]);
if (fullstackConfig.configureFullstack) {
const fullstackOptions = yield ask([
{
choices: ['angular', 'nuxt'],
initial: 1, // nuxt
message: 'Default frontend framework?',
name: 'frontend',
type: 'select',
},
{
initial: false,
message: 'Initialize git by default?',
name: 'git',
type: 'confirm',
},
]);
newConfig.commands.fullstack = {
frontend: fullstackOptions.frontend,
git: fullstackOptions.git,
};
}
}
else {
// Non-interactive mode - use CLI parameters
format = cliFormat === 'yaml' ? 'yaml' : 'json';
if (controller) {
newConfig.commands.server = {
module: {
controller: controller,
},
};
}
if (frontend) {
newConfig.commands.fullstack = {
frontend: frontend,
git: initGit !== undefined ? initGit === 'true' : undefined,
};
}
}
// Save configuration
try {
config.saveConfig(newConfig, cwd, { format });
const fileName = format === 'yaml' ? 'lt.config.yaml' : 'lt.config.json';
const configPath = filesystem.path(cwd, fileName);
info('');
success(`${fileName} created successfully!`);
info('');
info(`Configuration saved to: ${configPath}`);
}
catch (e) {
error(`Failed to create configuration file: ${e.message}`);
return 'config init: failed';
}
if (!toolbox.parameters.options.fromGluegunMenu) {
process.exit();
}
return 'config init';
}),
};
exports.default = InitCommand;