UNPKG

@monitoro/herd

Version:

Automate your browser, build AI web tools and MCP servers with Monitoro Herd

82 lines (81 loc) 2.82 kB
import { fileURLToPath } from 'url'; import { dirname, join, relative } from 'path'; import * as fs from 'fs'; import * as esbuild from 'esbuild'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); export async function buildTrail(trailPath, options = {}) { const { outDir = join(trailPath, '.build'), format = 'esm', platform = 'node', target = 'node18', silent = false } = options; // Ensure trailPath is absolute trailPath = fs.realpathSync(trailPath); // Create build directory if it doesn't exist if (!fs.existsSync(outDir)) { fs.mkdirSync(outDir, { recursive: true }); } // Common build options const commonOptions = { format, platform, target, bundle: true, sourcemap: true, outdir: outDir, external: ['@monitoro/herd'], define: { 'process.env.NODE_ENV': '"production"' }, logLevel: silent ? 'silent' : 'info', color: true }; // Build each file const files = ['actions.ts', 'urls.ts', 'selectors.ts']; const buildPromises = files.map(async (file) => { const entryPoint = join(trailPath, file); if (!fs.existsSync(entryPoint)) { if (!silent) { console.warn(`⚠️ Warning: ${file} not found in trail directory`); } return; } try { await esbuild.build({ ...commonOptions, entryPoints: [entryPoint], outdir: outDir }); if (!silent) { console.log(`✨ Built ${relative(trailPath, entryPoint)}`); } } catch (error) { if (!silent) { console.error(`❌ Failed to build ${relative(trailPath, entryPoint)}:`, error); } throw error; } }); await Promise.all(buildPromises); // Copy package.json if it exists const packageJsonPath = join(trailPath, 'package.json'); if (fs.existsSync(packageJsonPath)) { fs.copyFileSync(packageJsonPath, join(outDir, 'package.json')); if (!silent) { console.log(`✨ Copied package.json`); } } return outDir; } export function getBuiltTrailPath(trailPath) { // Ensure trailPath is absolute trailPath = fs.realpathSync(trailPath); return join(trailPath, '.build'); } export function isTrailBuilt(trailPath) { // Ensure trailPath is absolute trailPath = fs.realpathSync(trailPath); const builtPath = getBuiltTrailPath(trailPath); return fs.existsSync(builtPath) && fs.existsSync(join(builtPath, 'actions.js')) && fs.existsSync(join(builtPath, 'urls.js')) && fs.existsSync(join(builtPath, 'selectors.js')); }