@ciklumrnd/ciklum-cli
Version:
Ciklum CLI tool for internal engineering teams
161 lines (160 loc) • 6.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const inquirer_1 = __importDefault(require("inquirer"));
const path = __importStar(require("path"));
const fs = __importStar(require("fs-extra"));
const template_engine_1 = require("../lib/template-engine");
class Accelerate extends core_1.Command {
static description = 'Create new projects from templates';
static examples = [
'<%= config.bin %> <%= command.id %> mcp',
'<%= config.bin %> <%= command.id %> mcp --target ./my-project',
];
static flags = {
help: core_1.Flags.help({ char: 'h' }),
target: core_1.Flags.string({
char: 't',
description: 'Target directory for the new project',
}),
};
static args = {
template: core_1.Args.string({
name: 'template',
required: false,
description: 'Template type to use (e.g., mcp)',
}),
};
async run() {
const { args, flags } = await this.parse(Accelerate);
// Initialize template engine
const templatesDir = path.join(__dirname, '..', '..', 'templates');
const engine = new template_engine_1.TemplateEngine(templatesDir);
// If no template specified, show available templates
if (!args.template) {
const templates = await engine.listTemplates();
if (templates.length === 0) {
this.log('\nNo templates available.');
this.log('Templates should be placed in the templates directory.');
return;
}
this.log('\nAvailable templates:');
this.log('==================\n');
for (const template of templates) {
this.log(` ${template.name.padEnd(15)} ${template.description}`);
}
this.log('\nUsage:');
this.log(` ${this.config.bin} accelerate <template>\n`);
this.log('Example:');
this.log(` ${this.config.bin} accelerate mcp\n`);
return;
}
try {
// Load template definition
const template = await engine.loadTemplate(args.template);
this.log(`\nCreating new project from template: ${template.name}`);
this.log(`${template.description}\n`);
// Collect parameters through interactive prompts
const params = {};
for (const param of template.parameters) {
if (param.type === 'string') {
const answer = await inquirer_1.default.prompt({
type: 'input',
name: param.name,
message: param.prompt,
default: param.default,
validate: param.validate ? (input) => {
const regex = new RegExp(param.validate);
return regex.test(input) || `Invalid format. Must match: ${param.validate}`;
} : undefined,
});
params[param.name] = answer[param.name];
}
else if (param.type === 'choice') {
const answer = await inquirer_1.default.prompt({
type: 'list',
name: param.name,
message: param.prompt,
choices: param.choices.map(c => ({
name: c.label,
value: c.value,
})),
default: param.default,
});
params[param.name] = answer[param.name];
}
}
// Determine target directory
const targetDir = flags.target || path.join(process.cwd(), params.projectName);
// Check if target directory exists
if (await fs.pathExists(targetDir)) {
const overwrite = await inquirer_1.default.prompt({
type: 'confirm',
name: 'overwrite',
message: `Directory ${targetDir} already exists. Overwrite?`,
default: false,
});
if (!overwrite.overwrite) {
this.log('Aborted.');
return;
}
await fs.remove(targetDir);
}
// Generate project
this.log('\nGenerating project...');
await engine.generateProject(args.template, targetDir, params);
this.log(`\n✅ Project created successfully at: ${targetDir}`);
this.log('\nNext steps:');
this.log(` cd ${path.relative(process.cwd(), targetDir)}`);
this.log(' npm install');
this.log(' npm run dev');
}
catch (error) {
// Handle Ctrl+C gracefully
if (error.name === 'ExitPromptError') {
process.exit(0);
}
if (error.code === 'ENOENT' && error.path?.includes('template.json')) {
this.error(`Template '${args.template}' not found`);
}
throw error;
}
}
}
exports.default = Accelerate;