UNPKG

undetected-chromedriver-js

Version:

Node.js wrapper for undetected-chromedriver with automatic setup and cross-platform support

158 lines (126 loc) • 4.42 kB
const { spawn } = require('child_process'); const path = require('path'); const os = require('os'); class DebugSetup { constructor() { this.platform = os.platform(); this.venvPath = path.join(process.cwd(), '.venv'); this.pythonCmd = this.platform === 'win32' ? 'python' : 'python3'; this.venvPythonCmd = this.platform === 'win32' ? path.join(this.venvPath, 'Scripts', 'python') : path.join(this.venvPath, 'bin', 'python'); } async checkPython() { console.log('šŸ” Checking Python version...'); return new Promise((resolve) => { const child = spawn(this.pythonCmd, ['--version']); child.stdout.on('data', (data) => { console.log(`System Python: ${data.toString()}`); }); child.on('close', (code) => { resolve(code === 0); }); }); } async checkVenvPython() { console.log('šŸ” Checking virtual environment Python...'); return new Promise((resolve) => { const child = spawn(this.venvPythonCmd, ['--version']); child.stdout.on('data', (data) => { console.log(`Venv Python: ${data.toString()}`); }); child.on('close', (code) => { resolve(code === 0); }); }); } async checkDistutils() { console.log('šŸ” Checking distutils availability...'); return new Promise((resolve) => { const child = spawn(this.venvPythonCmd, ['-c', 'import distutils; print("distutils available")']); child.stdout.on('data', (data) => { console.log(`āœ“ ${data.toString()}`); }); child.stderr.on('data', (data) => { console.log(`āœ— ${data.toString()}`); }); child.on('close', (code) => { resolve(code === 0); }); }); } async checkSetuptools() { console.log('šŸ” Checking setuptools availability...'); return new Promise((resolve) => { const child = spawn(this.venvPythonCmd, ['-c', 'import setuptools; print("setuptools available")']); child.stdout.on('data', (data) => { console.log(`āœ“ ${data.toString()}`); }); child.stderr.on('data', (data) => { console.log(`āœ— ${data.toString()}`); }); child.on('close', (code) => { resolve(code === 0); }); }); } async checkUndetectedChrome() { console.log('šŸ” Checking undetected-chromedriver import...'); return new Promise((resolve) => { const child = spawn(this.venvPythonCmd, ['-c', 'import undetected_chromedriver; print("undetected-chromedriver available")']); child.stdout.on('data', (data) => { console.log(`āœ“ ${data.toString()}`); }); child.stderr.on('data', (data) => { console.log(`āœ— ${data.toString()}`); }); child.on('close', (code) => { resolve(code === 0); }); }); } async installSetuptools() { console.log('šŸ”§ Installing setuptools...'); return new Promise((resolve) => { const pipCmd = this.platform === 'win32' ? path.join(this.venvPath, 'Scripts', 'pip') : path.join(this.venvPath, 'bin', 'pip'); const child = spawn(pipCmd, ['install', 'setuptools']); child.stdout.on('data', (data) => { process.stdout.write(data); }); child.stderr.on('data', (data) => { process.stderr.write(data); }); child.on('close', (code) => { if (code === 0) { console.log('āœ“ setuptools installed'); resolve(true); } else { console.log('āœ— Failed to install setuptools'); resolve(false); } }); }); } async run() { console.log('šŸ› Debug mode - Checking environment...\n'); await this.checkPython(); await this.checkVenvPython(); console.log('\n--- Checking Python modules ---'); const hasDistutils = await this.checkDistutils(); const hasSetuptools = await this.checkSetuptools(); if (!hasDistutils && !hasSetuptools) { console.log('\nšŸ”§ Neither distutils nor setuptools found, installing setuptools...'); await this.installSetuptools(); await this.checkSetuptools(); } await this.checkUndetectedChrome(); console.log('\nāœ… Debug completed'); } } if (require.main === module) { const debug = new DebugSetup(); debug.run().catch(console.error); } module.exports = DebugSetup;