find-imagemagick.jsā¢5.01 kB
#!/usr/bin/env node
const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');
async function findAndConfigureImageMagick() {
console.log('š Finding and configuring ImageMagick...');
// Test if already in PATH
console.log('\\n1ļøā£ Testing if ImageMagick is already in PATH...');
const testCommands = ['magick', 'convert', 'identify'];
let workingCommand = null;
for (const cmd of testCommands) {
try {
await new Promise((resolve, reject) => {
const child = spawn(cmd, ['--version'], { stdio: 'pipe', windowsHide: true });
let output = '';
child.stdout.on('data', data => output += data.toString());
child.stderr.on('data', data => output += data.toString());
child.on('close', (code) => {
if (code === 0) {
console.log(`ā
${cmd} command works!`);
console.log(`š Version: ${output.split('\\n')[0]}`);
workingCommand = cmd;
resolve();
} else {
reject(new Error(`Command failed with code ${code}`));
}
});
child.on('error', () => {
reject(new Error(`Command not found`));
});
});
break; // If we get here, command worked
} catch (error) {
console.log(`ā ${cmd} command not found in PATH`);
}
}
if (workingCommand) {
console.log('\\nš ImageMagick is working! You can proceed with OCR testing.');
return workingCommand;
}
// Search for ImageMagick installations
console.log('\\n2ļøā£ Searching for ImageMagick installations...');
const searchPaths = [
'C:\\\\Program Files\\\\ImageMagick*',
'C:\\\\Program Files (x86)\\\\ImageMagick*',
process.env.LOCALAPPDATA + '\\\\Microsoft\\\\WinGet\\\\Packages\\\\ImageMagick*'
];
const foundPaths = [];
for (const searchPath of searchPaths) {
try {
const basePath = searchPath.replace(/\\\\\*$/, '');
if (fs.existsSync(basePath)) {
const items = fs.readdirSync(basePath);
for (const item of items) {
if (item.includes('ImageMagick')) {
const fullPath = path.join(basePath, item);
if (fs.statSync(fullPath).isDirectory()) {
foundPaths.push(fullPath);
}
}
}
}
} catch (error) {
// Continue searching
}
}
// Also try direct search
const commonLocations = [
'C:\\\\Program Files\\\\ImageMagick-7.1.1-Q16-HDRI',
'C:\\\\Program Files\\\\ImageMagick-7.1.1-Q16',
'C:\\\\Program Files\\\\ImageMagick',
'C:\\\\Program Files (x86)\\\\ImageMagick'
];
for (const location of commonLocations) {
if (fs.existsSync(location) && !foundPaths.includes(location)) {
foundPaths.push(location);
}
}
if (foundPaths.length === 0) {
console.log('ā No ImageMagick installations found.');
console.log('\\nš„ Try installing with:');
console.log(' winget install ImageMagick.ImageMagick');
console.log(' OR');
console.log(' choco install imagemagick');
return null;
}
console.log(`\\nā
Found ${foundPaths.length} ImageMagick installation(s):`);
foundPaths.forEach((p, i) => {
console.log(` ${i + 1}. ${p}`);
// Check for magick.exe
const magickExe = path.join(p, 'magick.exe');
const convertExe = path.join(p, 'convert.exe');
if (fs.existsSync(magickExe)) {
console.log(` ā
Has magick.exe`);
}
if (fs.existsSync(convertExe)) {
console.log(` ā
Has convert.exe`);
}
});
// Show how to add to PATH
console.log('\\n3ļøā£ To add ImageMagick to your PATH:');
console.log('\\nš **Temporary (current session only):**');
foundPaths.forEach((p, i) => {
console.log(` $env:PATH += ";${p}"`);
});
console.log('\\nš **Permanent (run as Administrator):**');
foundPaths.forEach((p, i) => {
console.log(` [Environment]::SetEnvironmentVariable("Path", $env:Path + ";${p}", "Machine")`);
});
console.log('\\nš **GUI Method:**');
console.log(' 1. Win+R ā sysdm.cpl ā Environment Variables');
console.log(' 2. Edit System PATH variable');
console.log(' 3. Add New entry with path above');
console.log(' 4. Restart PowerShell');
return foundPaths[0];
}
findAndConfigureImageMagick();