ChessBuilder/Assets/ChessEngines/fairy-chess-server/engine-path.js

46 lines
No EOL
1.6 KiB
JavaScript

// Assets/ChessEngines/fairy-chess-server/engine-path.js
import path from 'path';
import os from 'os';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
// Get current file's directory in ES modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
function getEnginePath() {
console.log("getEnginePath")
// First check if path is provided through environment variable
if (process.env.FAIRY_STOCKFISH_PATH) {
return process.env.FAIRY_STOCKFISH_PATH;
}
// Get the current directory (fairy-chess-server)
const currentDir = __dirname;
// Navigate up to Assets/ChessEngines
const engineBaseDir = path.join(currentDir, '..', '..');
// Determine OS and set appropriate path
if (os.platform() === 'win32') {
// Windows path
console.log(engineBaseDir + '/ChessEngines' + '/stockfish' + '/stockfish.exe')
return engineBaseDir + '/ChessEngines' + '/stockfish' + '/stockfish.exe'
} else {
//res://Assets/ChessEngines/Fairy-Stockfish/src/stockfish
// Unix-like systems (Linux, macOS)
console.log(engineBaseDir + '/ChessEngines'+ '/Fairy-Stockfish' + '/src' + '/stockfish')
return engineBaseDir + '/ChessEngines'+ '/Fairy-Stockfish' + '/src' + '/stockfish';
}
}
// Verify engine path exists
async function verifyEnginePath(enginePath) {
const fs = await import('fs');
if (!fs.existsSync(enginePath)) {
throw new Error(`Chess engine not found at path: ${enginePath}`);
}
return enginePath;
}
export { getEnginePath, verifyEnginePath };