@nestbox-ai/cli
Version:
The cli tools that helps developers to build agents
186 lines • 8.35 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 __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.findProjectRoot = findProjectRoot;
exports.loadNestboxConfig = loadNestboxConfig;
exports.isTypeScriptProject = isTypeScriptProject;
exports.runPredeployScripts = runPredeployScripts;
exports.createZipFromDirectory = createZipFromDirectory;
exports.getAgentExcludePatterns = getAgentExcludePatterns;
exports.createNestboxConfig = createNestboxConfig;
const path_1 = __importDefault(require("path"));
const fs_1 = __importDefault(require("fs"));
const chalk_1 = __importDefault(require("chalk"));
const ora_1 = __importDefault(require("ora"));
const util_1 = require("util");
const child_process_1 = require("child_process");
const adm_zip_1 = __importDefault(require("adm-zip"));
const os = __importStar(require("os"));
const execAsync = (0, util_1.promisify)(child_process_1.exec);
function findProjectRoot() {
return __awaiter(this, arguments, void 0, function* (startDir = process.cwd()) {
let currentDir = startDir;
while (currentDir !== path_1.default.parse(currentDir).root) {
const nestboxConfigPath = path_1.default.join(currentDir, "nestbox.config.json");
const packageJsonPath = path_1.default.join(currentDir, "package.json");
if (fs_1.default.existsSync(nestboxConfigPath) ||
fs_1.default.existsSync(packageJsonPath)) {
return currentDir;
}
currentDir = path_1.default.dirname(currentDir);
}
return startDir; // Fallback to current directory if no root markers found
});
}
// Function to load and parse nestbox.config.json if it exists
function loadNestboxConfig(projectRoot) {
const configPath = path_1.default.join(projectRoot, "nestbox.config.json");
if (fs_1.default.existsSync(configPath)) {
try {
const configContent = fs_1.default.readFileSync(configPath, "utf8");
return JSON.parse(configContent);
}
catch (error) {
console.warn(chalk_1.default.yellow(`Warning: Error parsing nestbox.config.json: ${error.message}`));
}
}
return null;
}
// Function to detect if a directory contains TypeScript files
function isTypeScriptProject(directoryPath) {
// Check for tsconfig.json
if (fs_1.default.existsSync(path_1.default.join(directoryPath, "tsconfig.json"))) {
return true;
}
// Check for .ts files
try {
const files = fs_1.default.readdirSync(directoryPath);
return files.some(file => file.endsWith(".ts") || file.endsWith(".tsx"));
}
catch (error) {
return false;
}
}
function runPredeployScripts(scripts, projectRoot) {
return __awaiter(this, void 0, void 0, function* () {
if (!scripts || !Array.isArray(scripts) || scripts.length === 0) {
return;
}
const spinner = (0, ora_1.default)("Running predeploy scripts...").start();
try {
for (const script of scripts) {
spinner.text = `Running: ${script}`;
// Make sure we're running in the correct directory
yield execAsync(script, {
cwd: projectRoot,
});
}
spinner.succeed("Predeploy scripts completed successfully");
}
catch (error) {
spinner.fail(`Predeploy script failed: ${error.message}`);
throw new Error(`Predeploy failed: ${error.message}`);
}
});
}
function createZipFromDirectory(dirPath, excludePatterns = ["node_modules"]) {
const dirName = path_1.default.basename(dirPath);
const timestamp = Date.now();
// Create zip in temp directory
const tempZipFilePath = path_1.default.join(os.tmpdir(), `${dirName}_${timestamp}.zip`);
const zip = new adm_zip_1.default();
// Function to recursively add files to zip
function addFilesToZip(currentPath, relativePath = "") {
const items = fs_1.default.readdirSync(currentPath);
for (const item of items) {
const itemPath = path_1.default.join(currentPath, item);
const itemRelativePath = path_1.default.join(relativePath, item);
// Check if item should be excluded
if (excludePatterns.some((pattern) => typeof pattern === "string"
? itemRelativePath === pattern || item === pattern
: pattern.test(itemRelativePath))) {
continue;
}
const stats = fs_1.default.statSync(itemPath);
if (stats.isDirectory()) {
addFilesToZip(itemPath, itemRelativePath);
}
else {
zip.addLocalFile(itemPath, path_1.default.dirname(itemRelativePath));
}
}
}
addFilesToZip(dirPath);
// Write zip to temp directory (for upload)
zip.writeZip(tempZipFilePath);
// Return the temp path for upload
return tempZipFilePath;
}
function getAgentExcludePatterns(config) {
var _a, _b;
const configPatterns = ((_a = config === null || config === void 0 ? void 0 : config.agent) === null || _a === void 0 ? void 0 : _a.exclude) || ((_b = config === null || config === void 0 ? void 0 : config.agents) === null || _b === void 0 ? void 0 : _b.exclude);
const defaultPatterns = ["node_modules"];
if (!Array.isArray(configPatterns)) {
return defaultPatterns;
}
const sanitizedPatterns = configPatterns.filter((pattern) => typeof pattern === "string" && pattern.trim().length > 0);
return [...new Set([...defaultPatterns, ...sanitizedPatterns])];
}
function createNestboxConfig(projectPath, isTypeScript) {
if (!isTypeScript)
return;
const configPath = path_1.default.join(projectPath, "nestbox.config.json");
const config = {
agents: {
predeploy: ["rm -rf dist", "npm run lint", "npm run build"],
exclude: [".git", "node_modules"],
},
};
fs_1.default.writeFileSync(configPath, JSON.stringify(config, null, 2));
console.log(chalk_1.default.green(`Created nestbox.config.json at ${configPath}`));
}
//# sourceMappingURL=agent.js.map