@monitoro/herd
Version:
Automate your browser, build AI web tools and MCP servers with Monitoro Herd
113 lines (98 loc) • 4.21 kB
JavaScript
/**
* This wrapper script provides TypeScript support in the Herd CLI
* It always attempts to use tsx for TypeScript files since it's now a direct dependency
*/
// Suppress warnings from the start
process.env.NODE_NO_WARNINGS = '1';
process.env.NODE_OPTIONS = (process.env.NODE_OPTIONS || '') + ' --no-deprecation --no-warnings';
const { spawn } = require('child_process');
const path = require('path');
const fs = require('fs');
// This is important to allow Node.js to find our modules
// We add the parent directory of this file to the module search path
// This ensures Node can find our bundled tsx copy
module.paths.unshift(path.join(__dirname, 'node_modules'));
module.paths.unshift(path.join(__dirname, '../node_modules'));
// Main execution path
async function main() {
// Check if we should invoke via tsx
const cliTsPath = path.join(__dirname, 'cli.ts');
const cliJsPath = path.join(__dirname, 'cli.js');
try {
// Get the path to the tsx executable from our package - check multiple locations
let tsxPath;
try {
// First try our bundled copy
const bundledTsxPath = path.join(__dirname, 'tsx/cli.js');
if (fs.existsSync(bundledTsxPath)) {
tsxPath = bundledTsxPath;
console.log('✅ Using bundled tsx');
} else {
// Then try to find tsx within our own package
tsxPath = require.resolve('tsx/dist/cli.js');
console.log('✅ Found tsx in node_modules');
}
} catch (err) {
console.warn('⚠️ Could not find tsx within package, trying global install...');
try {
// Fallback to global tsx
tsxPath = require.resolve('tsx');
console.log('✅ Using globally installed tsx');
} catch (err) {
console.error('❌ Error: Could not find tsx module. Please report this issue.');
// We'll fall back to JS if we can't find tsx
}
}
// If we found tsx and the TS file exists, run with tsx
if (tsxPath && fs.existsSync(cliTsPath)) {
console.log('🔧 Running TypeScript directly with tsx...');
// Run with Node.js, passing in the path to tsx and our TS file
const args = [
tsxPath, // Path to tsx cli
cliTsPath, // Path to our TypeScript file
...process.argv.slice(2) // Pass all CLI arguments
];
const child = spawn(process.execPath, args, {
stdio: 'inherit',
env: {
...process.env,
NODE_NO_WARNINGS: '1',
NODE_OPTIONS: '--no-deprecation --no-warnings',
HERD_USING_TSX: 'true',
// Add our module paths to NODE_PATH
NODE_PATH: [
path.join(__dirname, 'node_modules'),
path.join(__dirname, '../node_modules'),
process.env.NODE_PATH || ''
].filter(Boolean).join(path.delimiter)
}
});
child.on('exit', (code) => {
process.exit(code || 0);
});
return; // Exit this function since we've started the process
}
} catch (err) {
console.warn('⚠️ Error trying to run with tsx:', err.message);
// Fall through to JS version below
}
// Fallback to the compiled JS version if we can't use tsx or TS file doesn't exist
if (fs.existsSync(cliJsPath)) {
console.log('ℹ️ Running compiled JavaScript version...');
require(cliJsPath);
} else {
console.error(`
❌ Error: Could not find the Herd CLI entry point.
Neither the TypeScript nor JavaScript version could be located.
Please try reinstalling the package:
npm install -g @monitoro/herd
For more information, visit: https://herd.garden/docs
`);
process.exit(1);
}
}
main().catch(err => {
console.error('❌ Failed to execute the Herd CLI:', err);
process.exit(1);
});