project-indexer
Version:
Local project indexer with browser trigger support
93 lines (80 loc) • 3.59 kB
JavaScript
/**
* Service installation script for project-indexer
* This script installs project-indexer as a background service using PM2
*/
import {exec} from 'child_process';
import fs from 'fs';
import os from 'os';
import path from 'path';
import {fileURLToPath} from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.resolve(__dirname, '..');
const serverScript = path.join(projectRoot, 'bin', 'start-server.js');
// Ensure the script exists
if (!fs.existsSync(serverScript)) {
console.error(`Error: Server script not found at ${serverScript}`);
process.exit(1);
}
console.log('Installing project-indexer as a background service...');
// Install PM2 globally if not already installed
exec('npm list -g pm2 || npm install -g pm2', (error, stdout, stderr) => {
if (error) {
console.error(`Error installing PM2: ${error.message}`);
return;
}
// Start the service with PM2
exec(`pm2 start ${serverScript} --name project-indexer`, (error, stdout, stderr) => {
if (error) {
console.error(`Error starting service: ${error.message}`);
return;
}
console.log('Service started successfully!');
// Save the PM2 process list so it restarts on system reboot
exec('pm2 save', (error, stdout, stderr) => {
if (error) {
console.error(`Error saving PM2 process list: ${error.message}`);
return;
}
// Generate and configure startup script based on OS
const platform = os.platform();
if (platform === 'win32') {
exec('pm2 startup windows', (error, stdout, stderr) => {
if (error) {
console.error(`Error setting up startup on Windows: ${error.message}`);
return;
}
// console.log('Service configured to start on system boot (Windows)');
// console.log('Installation complete! The service is now running in the background.');
// console.log('To check status: npm run service:status');
// console.log('To stop service: npm run service:stop');
// console.log('To uninstall: npm run service:uninstall');
});
} else if (platform === 'darwin') {
// macOS
console.log('For macOS, run the following command to enable startup on boot:');
console.log('pm2 startup');
// console.log('Then follow the instructions displayed.');
// console.log('Installation complete! The service is now running in the background.');
// console.log('To check status: npm run service:status');
// console.log('To stop service: npm run service:stop');
// console.log('To uninstall: npm run service:uninstall');
} else {
// Linux and other Unix-like systems
exec('pm2 startup', (error, stdout, stderr) => {
if (error) {
console.error(`Error setting up startup: ${error.message}`);
// console.log('You may need to run the pm2 startup command manually with sudo.');
// console.log('Please check the PM2 documentation for your specific Linux distribution.');
return;
}
console.log('Please run the command above with sudo to enable startup on boot.');
console.log('Installation complete! The service is now running in the background.');
// console.log('To check status: indexer status');
// console.log('To start: indexer install');
// console.log('To uninstall: indexer uninstall');
});
}
});
});
});