project-indexer
Version:
Local project indexer with browser trigger support
217 lines (187 loc) • 6.72 kB
JavaScript
/**
* Service manager for project-indexer
* This script provides a command-line interface for managing the project-indexer service
*
* Usage:
* indexer install - Install as a background service
* indexer uninstall - Uninstall the service
* indexer start - Start the service
* indexer stop - Stop the service
* indexer status - Check service status
*/
import {execSync} from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {fileURLToPath} from 'url';
// Get the directory where this script is located
const __dirname = path.dirname(fileURLToPath(import.meta.url));
// Find the path to the start-server.js script
// This works whether installed globally or locally
const serverScript = path.join(__dirname, 'start-server.js');
// Ensure the script exists
if (!fs.existsSync(serverScript)) {
console.error(`Error: Server script not found at ${serverScript}`);
process.exit(1);
}
// Get the command from command line arguments
const command = process.argv[2];
if (!command) {
showUsage();
process.exit(1);
}
// Process the command
switch (command.toLowerCase()) {
case 'install':
installService();
break;
case 'uninstall':
uninstallService();
break;
case 'start':
startService();
break;
case 'stop':
stopService();
break;
case 'status':
checkStatus();
break;
default:
console.error(`Unknown command: ${command}`);
showUsage();
process.exit(1);
}
// Function to show usage information
function showUsage() {
console.log('Usage:');
console.log(' indexer install - Install as a background service');
console.log(' indexer uninstall - Uninstall the service');
console.log(' indexer start - Start the service');
console.log(' indexer stop - Stop the service');
console.log(' indexer status - Check service status');
}
// Function to install the service
function installService() {
console.log('Installing project-indexer as a background service...');
try {
// Check if PM2 is installed
try {
execSync('pm2 -v', { stdio: 'ignore' });
} catch (error) {
// console.log('PM2 not found. Installing PM2...');
execSync('npm install -g pm2', { stdio: 'inherit' });
}
// Start the service with PM2
console.log('Starting service...');
execSync(`pm2 start ${serverScript} --name project-indexer`, { stdio: 'inherit' });
// Save the PM2 process list
console.log('Saving process list...');
execSync('pm2 save', { stdio: 'inherit' });
// Configure startup based on OS
const platform = os.platform();
console.log(`Configuring startup for ${platform}...`);
if (platform === 'win32') {
try {
execSync('pm2 startup windows', { stdio: 'inherit' });
console.log('Service configured to start on system boot (Windows)');
} catch (error) {
console.error('Error setting up Windows startup. You may need to run as Administrator.');
// console.log('To enable startup on boot, run the following command as Administrator:');
// console.log('pm2 startup windows');
}
} else if (platform === 'darwin') {
// macOS
try {
execSync('pm2 startup', { stdio: 'inherit' });
console.log('To complete macOS startup setup, run the command displayed above.');
} catch (error) {
console.error('Error setting up macOS startup.');
// console.log('To enable startup on boot, run:');
// console.log('pm2 startup');
// console.log('Then follow the instructions displayed.');
}
} else {
// Linux and other Unix-like systems
try {
execSync('pm2 startup', { stdio: 'inherit' });
console.log('To complete Linux startup setup, run the command displayed above with sudo.');
} catch (error) {
console.error('Error setting up Linux startup.');
// console.log('To enable startup on boot, run:');
// console.log('pm2 startup');
// console.log('Then follow the instructions displayed.');
}
}
// console.log('\nInstallation complete! The service is now running in the background.');
console.log('To check status: indexer status');
console.log('To stop service: indexer stop');
console.log('To uninstall: indexer uninstall');
} catch (error) {
console.error(`Error installing service: ${error.message}`);
process.exit(1);
}
}
// Function to uninstall the service
function uninstallService() {
console.log('Uninstalling project-indexer background service...');
try {
// Stop and remove from PM2
execSync('pm2 delete project-indexer', { stdio: 'inherit' });
console.log('Service stopped and removed from PM2.');
// Remove from startup based on OS
const platform = os.platform();
if (platform === 'win32') {
try {
execSync('pm2 unstartup windows', { stdio: 'inherit' });
console.log('Service removed from Windows startup.');
} catch (error) {
console.error('Error removing from Windows startup. You may need to run as Administrator.');
console.log('To remove from startup, run the following command as Administrator:');
console.log('pm2 unstartup windows');
}
} else {
// For macOS and Linux
console.log('To completely remove from system startup, run:');
console.log('pm2 unstartup');
console.log('And follow any instructions displayed.');
}
console.log('Uninstallation complete!');
} catch (error) {
console.error(`Error uninstalling service: ${error.message}`);
console.log('Service might not be running or already uninstalled.');
}
}
// Function to start the service
function startService() {
console.log('Starting project-indexer service...');
try {
execSync('pm2 start project-indexer', { stdio: 'inherit' });
console.log('Service started successfully!');
} catch (error) {
console.error(`Error starting service: ${error.message}`);
process.exit(1);
}
}
// Function to stop the service
function stopService() {
console.log('Stopping project-indexer service...');
try {
execSync('pm2 stop project-indexer', { stdio: 'inherit' });
console.log('Service stopped successfully!');
} catch (error) {
console.error(`Error stopping service: ${error.message}`);
process.exit(1);
}
}
// Function to check service status
function checkStatus() {
console.log('Checking project-indexer service status...');
try {
execSync('pm2 status project-indexer', { stdio: 'inherit' });
} catch (error) {
console.error(`Error checking service status: ${error.message}`);
process.exit(1);
}
}