@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
162 lines • 8.37 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());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.registerProjectCommand = registerProjectCommand;
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const fs_1 = __importDefault(require("fs"));
const agent_1 = require("../../utils/agent");
const plopGenerator_1 = require("../../utils/plopGenerator");
const validation_1 = require("../../utils/validation");
const inquirer_1 = __importDefault(require("inquirer"));
const path_1 = __importDefault(require("path"));
function registerProjectCommand(generateCommand) {
generateCommand
.command("project <folder>")
.description("Generate a new project from templates")
.option("--lang <language>", "Project language (ts|js)")
.option("--template <type>", "Template type (agent|chatbot)")
.option("--name <agentName>", "Agent/Chatbot name (must be a valid function name)")
.option("--project <projectId>", "Project ID")
.action((folder, options) => __awaiter(this, void 0, void 0, function* () {
try {
const spinner = (0, ora_1.default)("Initializing project generation...").start();
// Ensure target folder doesn't exist
if (fs_1.default.existsSync(folder)) {
spinner.fail(`Folder ${folder} already exists`);
return;
}
let selectedLang = options.lang;
let selectedTemplate = options.template;
let agentName = options.name;
// Interactive selection if not provided
if (!selectedLang || !selectedTemplate) {
spinner.stop();
const answers = yield inquirer_1.default.prompt([
{
type: "list",
name: "lang",
message: "Select project language:",
choices: [
{ name: "TypeScript", value: "ts" },
{ name: "JavaScript", value: "js" },
{ name: "Python", value: "py" },
],
when: () => !selectedLang,
},
{
type: "list",
name: "template",
message: "Select template type:",
choices: [
{ name: "Agent", value: "agent" },
{ name: "Chatbot", value: "chatbot" },
],
when: () => !selectedTemplate,
},
{
type: "input",
name: "agentName",
message: "Enter agent/chatbot name (must be a valid function name):",
when: () => !agentName,
default: (answers) => {
const type = selectedTemplate || answers.template;
return type === "agent"
? "myAgent"
: "myChatbot";
},
validate: (input) => {
if (!input.trim()) {
return "Agent name cannot be empty";
}
if (!(0, validation_1.isValidFunctionName)(input.trim())) {
return "Must be a valid function name (e.g., myAgent, chatBot123, my_agent)";
}
return true;
},
},
]);
selectedLang = selectedLang || answers.lang;
selectedTemplate = selectedTemplate || answers.template;
agentName = agentName || answers.agentName;
spinner.start("Generating project...");
}
// Validate agent name if provided via CLI option
if (agentName && !(0, validation_1.isValidFunctionName)(agentName)) {
spinner.fail(`Invalid agent name: "${agentName}". Must be a valid function name (e.g., myAgent, chatBot123, my_agent)`);
return;
}
// Set default agent name if not provided
if (!agentName) {
agentName =
selectedTemplate === "agent" ? "myAgent" : "myChatbot";
}
// Find matching template in local templates folder
const templateMapping = {
agent: "base",
chatbot: "chatbot",
};
const mappedTemplateType = templateMapping[selectedTemplate] || selectedTemplate;
// Check if template directory exists
console.log(__dirname);
const templatePath = path_1.default.resolve(__dirname, `../../../templates/${mappedTemplateType}-${selectedLang}`);
if (!fs_1.default.existsSync(templatePath)) {
spinner.fail(`Template not found: ${templatePath}`);
// Show available templates
const availableTemplates = (0, plopGenerator_1.listAvailableTemplates)();
if (availableTemplates.length > 0) {
console.log(chalk_1.default.yellow("Available templates:"));
availableTemplates.forEach(template => {
console.log(chalk_1.default.yellow(` - ${template}`));
});
}
else {
console.log(chalk_1.default.red("No templates found. Please add your templates to the templates directory."));
}
return;
}
spinner.text = `Generating ${mappedTemplateType} project in ${folder}...`;
try {
// Generate project using plop
const { agentNameInYaml } = yield (0, plopGenerator_1.generateWithPlop)(selectedTemplate, selectedLang, folder, path_1.default.basename(folder), agentName);
// Create nestbox.config.json for TypeScript projects
(0, agent_1.createNestboxConfig)(folder, selectedLang === "ts");
spinner.succeed(`Successfully generated ${mappedTemplateType} project in ${folder}`);
console.log(chalk_1.default.green("\nNext steps:"));
console.log(chalk_1.default.yellow(` cd ${folder}`));
if (selectedLang === "py") {
console.log(chalk_1.default.yellow(" pip install -r requirements.txt"));
}
else {
console.log(chalk_1.default.yellow(" npm install"));
if (selectedLang === "ts") {
console.log(chalk_1.default.yellow(" npm run build"));
}
}
console.log(chalk_1.default.yellow(` nestbox agent deploy --agent ${agentNameInYaml} --instance <instance-name>`));
}
catch (error) {
// Clean up on error
if (fs_1.default.existsSync(folder)) {
fs_1.default.rmSync(folder, { recursive: true, force: true });
}
throw error;
}
}
catch (error) {
console.error(chalk_1.default.red("Error:"), error.message || "Failed to generate project");
}
}));
}
//# sourceMappingURL=project.js.map