@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
104 lines • 4.73 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.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"));
/**
* 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']
}
},
// Add the main template file with proper naming
{
type: 'add',
path: targetFileName,
templateFile: path_1.default.join(templatePath, mainTemplateFile)
}
]
});
// Run the generator
const generator = plop.getGenerator(generatorName);
yield generator.runActions({
name: projectName || path_1.default.basename(targetFolder),
agentName: agentName || (templateType === 'agent' ? 'myAgent' : 'myChatbot')
});
// 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));
}
}
});
}
/**
* 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