chromancer
Version:
A powerful command-line interface for automating Chrome browser using Playwright. Perfect for web scraping, automation, testing, and browser workflows.
60 lines (59 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@oclif/core");
const session_js_1 = require("../session.js");
class Stop extends core_1.Command {
static description = 'Stop the active Chrome browser instance';
static examples = [
'<%= config.bin %> <%= command.id %>',
];
async run() {
const session = await session_js_1.SessionManager.getValidSession();
if (!session) {
this.log('No active Chrome session found');
return;
}
this.log(`Stopping Chrome on port ${session.port}...`);
try {
if (process.platform === 'win32') {
// Windows-specific process termination
try {
// Use taskkill command on Windows
const { execSync } = require('child_process');
execSync(`taskkill /F /PID ${session.pid}`, { stdio: 'ignore' });
}
catch (error) {
// Process might already be terminated
}
}
else {
// Unix-like systems
try {
// Try to kill the process gracefully
process.kill(session.pid, 'SIGTERM');
// Give it a moment to shut down gracefully
await new Promise(resolve => setTimeout(resolve, 1000));
// Check if it's still running
try {
process.kill(session.pid, 0);
// If we get here, process is still running, force kill
process.kill(session.pid, 'SIGKILL');
}
catch {
// Process already terminated
}
}
catch {
// Process might already be terminated
}
}
// Clear the session
session_js_1.SessionManager.clearSession();
this.log('✅ Chrome stopped successfully');
}
catch (error) {
this.error(`Failed to stop Chrome: ${error}`);
}
}
}
exports.default = Stop;