@meshcore/cli
Version:
Official CLI for managing AI agents in MeshCore.ai with LLM-powered metadata extraction
140 lines • 5.49 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;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReadmeRewriterUtils = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const config_1 = require("../services/config");
class ReadmeRewriterUtils {
static async findReadmeFile(dirPath) {
for (const pattern of this.README_PATTERNS) {
const readmePath = path.join(dirPath, pattern);
if (fs.existsSync(readmePath)) {
return this.readReadmeFile(readmePath);
}
}
return null;
}
static async readReadmeFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`README file not found: ${filePath}`);
}
const stats = fs.statSync(filePath);
if (stats.size > this.MAX_README_SIZE) {
throw new Error(`README file too large: ${stats.size} bytes (max: ${this.MAX_README_SIZE})`);
}
const content = fs.readFileSync(filePath, 'utf-8');
return {
path: filePath,
content,
size: stats.size
};
}
static async backupReadmeFile(filePath) {
if (!fs.existsSync(filePath)) {
throw new Error(`File not found: ${filePath}`);
}
const backupPath = `${filePath}.bak`;
fs.copyFileSync(filePath, backupPath);
return backupPath;
}
static async restoreReadmeFile(filePath) {
const backupPath = `${filePath}.bak`;
if (!fs.existsSync(backupPath)) {
throw new Error(`Backup file not found: ${backupPath}`);
}
fs.copyFileSync(backupPath, filePath);
fs.unlinkSync(backupPath);
}
static async saveRewrittenReadme(originalPath, rewrittenContent, outputPath) {
const config = config_1.ConfigService.getInstance();
const readmeConfig = config.getReadmeConfig();
if (!outputPath) {
const parsedPath = path.parse(originalPath);
outputPath = path.join(parsedPath.dir, `${parsedPath.name}${readmeConfig.outputSuffix}${parsedPath.ext}`);
}
// Backup original if configured
if (readmeConfig.backupOriginal && outputPath === originalPath) {
await this.backupReadmeFile(originalPath);
}
fs.writeFileSync(outputPath, rewrittenContent, 'utf-8');
return outputPath;
}
static extractOriginalUrlFromReadme(content) {
// Try to find API endpoint URLs in the README
const urlPatterns = [
// Look for base URL or endpoint definitions
/(?:base|endpoint|api)\s*[:\-=]\s*(https?:\/\/[^\s\)]+)/gi,
// Look for curl examples
/curl[^"']*["'](https?:\/\/[^"']+)["']/gi,
// Look for HTTP requests
/(?:GET|POST|PUT|DELETE|PATCH)\s+(https?:\/\/[^\s]+)/gi,
// Look for general URLs that might be endpoints
/https?:\/\/[^\s]+\/(?:api|v\d+)[^\s]*/gi
];
for (const pattern of urlPatterns) {
const matches = content.match(pattern);
if (matches && matches.length > 0) {
// Extract just the URL part from the first match
const urlMatch = matches[0].match(/(https?:\/\/[^\s\)"']+)/);
if (urlMatch) {
return urlMatch[1];
}
}
}
return undefined;
}
static formatFileSize(bytes) {
if (bytes < 1024)
return `${bytes} bytes`;
if (bytes < 1024 * 1024)
return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
}
static generateGatewayUrl(agentId, baseUrl) {
const gatewayBase = baseUrl || 'https://api.meshcore.ai';
return `${gatewayBase}/gateway/call/${agentId}`;
}
}
exports.ReadmeRewriterUtils = ReadmeRewriterUtils;
ReadmeRewriterUtils.MAX_README_SIZE = 50000; // 50KB max
ReadmeRewriterUtils.README_PATTERNS = [
'README.md',
'README.txt',
'readme.md',
'README.MD',
'README'
];
//# sourceMappingURL=readme-rewriter.js.map
;