UNPKG

@nestbox-ai/cli

Version:

The cli tools that helps developers to build agents

124 lines 5.45 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.generateWithPlop = generateWithPlop; exports.listAvailableTemplates = listAvailableTemplates; const path_1 = __importDefault(require("path")); const fs_1 = __importDefault(require("fs")); const node_plop_1 = __importDefault(require("node-plop")); const kebabize = (str) => str // Convert underscores to hyphens .replace(/_/g, "-") // Insert hyphen before capitals (handles acronyms) .replace(/[A-Z]+(?![a-z])|[A-Z]/g, (match, offset) => (offset ? "-" : "") + match.toLowerCase()) // Normalize multiple hyphens .replace(/-+/g, "-") // Trim hyphens .replace(/^-|-$/g, ""); /** * Generate project using plop.js templates */ function generateWithPlop(templateType, language, targetFolder, projectName, agentName) { return __awaiter(this, void 0, void 0, function* () { // Create the target directory if (!fs_1.default.existsSync(targetFolder)) { fs_1.default.mkdirSync(targetFolder, { recursive: true }); } // Set up plop programmatically const plop = yield (0, node_plop_1.default)("", { destBasePath: targetFolder, force: false, }); // Template mapping const templateMapping = { agent: "base", chatbot: "chatbot", }; const mappedTemplateType = templateMapping[templateType] || templateType; const templatePath = path_1.default.resolve(__dirname, `../../templates/${mappedTemplateType}-${language}`); if (!fs_1.default.existsSync(templatePath)) { throw new Error(`Template not found: ${templatePath}`); } // Configure the generator const generatorName = `${mappedTemplateType}-${language}`; // Get the main template file path and target name const mainTemplateFile = language === "ts" ? "src/index.ts.hbs" : "index.js.hbs"; const targetFileName = language === "ts" ? "src/index.ts" : "index.js"; plop.setGenerator(generatorName, { description: `Generate a new ${mappedTemplateType} project in ${language}`, prompts: [], actions: [ // Copy all non-template files { type: "addMany", destination: ".", base: templatePath, templateFiles: `${templatePath}/**/*`, globOptions: { dot: true, ignore: ["**/node_modules/**", "**/*.hbs"], }, }, { type: "add", path: targetFileName, templateFile: path_1.default.join(templatePath, mainTemplateFile), }, { type: "add", path: "nestbox-agents.yaml", templateFile: path_1.default.join(templatePath, "nestbox-agents.yaml.hbs"), }, ], }); // Run the generator const generator = plop.getGenerator(generatorName); const agentNameFinal = agentName || (templateType === "agent" ? "myAgent" : "myChatbot"); const agentNameInYaml = kebabize(agentNameFinal); yield generator.runActions({ name: projectName || path_1.default.basename(targetFolder), agentName: agentNameFinal, agentNameInYaml, }); // Update package.json with project name if provided if (projectName) { const packageJsonPath = path_1.default.join(targetFolder, "package.json"); if (fs_1.default.existsSync(packageJsonPath)) { const packageJson = JSON.parse(fs_1.default.readFileSync(packageJsonPath, "utf8")); packageJson.name = projectName; fs_1.default.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2)); } } return { agentNameInYaml, }; }); } /** * List available templates */ function listAvailableTemplates() { const templatesDir = path_1.default.resolve(__dirname, "../../templates"); if (!fs_1.default.existsSync(templatesDir)) { return []; } return fs_1.default .readdirSync(templatesDir) .filter((item) => { const itemPath = path_1.default.join(templatesDir, item); return fs_1.default.statSync(itemPath).isDirectory(); }) .map((item) => item.replace(/\.(ts|js)$/, "")); } //# sourceMappingURL=plopGenerator.js.map