Enhances path handling and file resolution #5

Merged
AlexKucera merged 36 commits from 1.3.0 into master 2025-09-25 16:47:12 +02:00
Showing only changes of commit 24a169d490 - Show all commits
+64 -18
View File
@@ -20,6 +20,7 @@ export class BinaryLocator {
/**
* Find the pdf2img binary in the plugin's node_modules directory.
* Handles the complex Obsidian environment where __dirname points to electron.asar.
* Includes Windows-specific executable detection and fallback paths.
* @param plugin The plugin instance for accessing manifest and settings
* @returns Binary location info
*/
@@ -31,7 +32,7 @@ export class BinaryLocator {
const pluginDirName = plugin?.manifest?.dir || 'typst-pdf-export';
const configDir = plugin?.app.vault.configDir || '.obsidian';
coderabbitai[bot] commented 2025-09-25 13:05:39 +02:00 (Migrated from github.com)
Review

⚠️ Potential issue | 🟠 Major

Handle Windows shims in node_modules/.bin

On Windows, the executable is typically pdf2img.cmd (or .ps1). Probe platform-specific candidates before failing.

Apply this diff:

-			const pdf2imgPath = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
-
-			// Check if the binary exists
-			const exists = await this.validateBinaryExists(pdf2imgPath, plugin);
+			const base = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
+			const candidates = process.platform === 'win32'
+				? [base + '.cmd', base + '.ps1', base]
+				: [base];
+			let pdf2imgPath = candidates[0];
+			let exists = false;
+			for (const c of candidates) {
+				if (await this.validateBinaryExists(c, plugin)) {
+					pdf2imgPath = c;
+					exists = true;
+					break;
+				}
+			}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

			const base = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
			const candidates = process.platform === 'win32'
				? [base + '.cmd', base + '.ps1', base]
				: [base];
			let pdf2imgPath = candidates[0];
			let exists = false;
			for (const c of candidates) {
				if (await this.validateBinaryExists(c, plugin)) {
					pdf2imgPath = c;
					exists = true;
					break;
				}
			}
🤖 Prompt for AI Agents
In src/converters/pdf/BinaryLocator.ts around lines 52 to 56, the code only
checks for the Unix-style 'node_modules/.bin/pdf2img' which fails on Windows
where shims are 'pdf2img.cmd' or 'pdf2img.ps1' (or .exe). Update the logic to
build a candidate list based on process.platform (on win32 include pdf2img.cmd,
pdf2img.ps1, pdf2img.exe and the POSIX name), iterate candidates calling
this.validateBinaryExists(plugin) for each, pick the first existing path and use
it, and only fail if none of the platform-specific candidates exist; keep
behavior unchanged for non-Windows platforms.

✅ Addressed in commits 24a169d to 2cdc9c3

_⚠️ Potential issue_ | _🟠 Major_ **Handle Windows shims in node_modules/.bin** On Windows, the executable is typically pdf2img.cmd (or .ps1). Probe platform-specific candidates before failing. Apply this diff: ```diff - const pdf2imgPath = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img'); - - // Check if the binary exists - const exists = await this.validateBinaryExists(pdf2imgPath, plugin); + const base = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img'); + const candidates = process.platform === 'win32' + ? [base + '.cmd', base + '.ps1', base] + : [base]; + let pdf2imgPath = candidates[0]; + let exists = false; + for (const c of candidates) { + if (await this.validateBinaryExists(c, plugin)) { + pdf2imgPath = c; + exists = true; + break; + } + } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion const base = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img'); const candidates = process.platform === 'win32' ? [base + '.cmd', base + '.ps1', base] : [base]; let pdf2imgPath = candidates[0]; let exists = false; for (const c of candidates) { if (await this.validateBinaryExists(c, plugin)) { pdf2imgPath = c; exists = true; break; } } ``` </details> <!-- suggestion_end --> <details> <summary>🤖 Prompt for AI Agents</summary> ``` In src/converters/pdf/BinaryLocator.ts around lines 52 to 56, the code only checks for the Unix-style 'node_modules/.bin/pdf2img' which fails on Windows where shims are 'pdf2img.cmd' or 'pdf2img.ps1' (or .exe). Update the logic to build a candidate list based on process.platform (on win32 include pdf2img.cmd, pdf2img.ps1, pdf2img.exe and the POSIX name), iterate candidates calling this.validateBinaryExists(plugin) for each, pick the first existing path and use it, and only fail if none of the platform-specific candidates exist; keep behavior unchanged for non-Windows platforms. ``` </details> <!-- fingerprinting:phantom:medusa:chinchilla --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commits 24a169d to 2cdc9c3
const possiblePluginDirs = this.getPossiblePluginDirs(plugin, pluginDirName, configDir);
// Find the first directory that exists and has node_modules
let pluginDir = possiblePluginDirs[0]; // Default fallback
for (const dir of possiblePluginDirs) {
@@ -48,24 +49,11 @@ export class BinaryLocator {
continue;
}
}
const pdf2imgPath = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
// Check if the binary exists
const exists = await this.validateBinaryExists(pdf2imgPath, plugin);
if (!exists) {
return {
binaryPath: pdf2imgPath,
exists: false,
error: `PDF2IMG binary not found at: ${pdf2imgPath}`
};
}
coderabbitai[bot] commented 2025-09-25 16:51:33 +02:00 (Migrated from github.com)
Review

⚠️ Potential issue | 🟠 Major

Use manifest.id for folder name; manifest.dir may be undefined or absolute

Using manifest.dir here is risky (often undefined or absolute), which can break downstream joins. Prefer manifest.id (the plugin folder) for consistent resolution.

Apply this diff:

-			const pluginDirName = plugin?.manifest?.dir || 'typst-pdf-export';
+			const pluginDirName = plugin?.manifest?.id || 'typst-pdf-export';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

			const pluginDirName = plugin?.manifest?.id || 'typst-pdf-export';
			const configDir = plugin?.app.vault.configDir || '.obsidian';
			const possiblePluginDirs = this.getPossiblePluginDirs(plugin, pluginDirName, configDir);
🤖 Prompt for AI Agents
In src/converters/pdf/BinaryLocator.ts around lines 32 to 35, the code uses
plugin?.manifest?.dir for the plugin folder which can be undefined or an
absolute path and break downstream joins; change it to use plugin?.manifest?.id
(the plugin folder name) as the primary value and fall back to a safe literal
like 'typst-pdf-export' or plugin.id if manifest.id is missing; keep configDir
as plugin?.app.vault.configDir || '.obsidian' and pass the new pluginDirName
into getPossiblePluginDirs so subsequent path joins use a consistent,
non-absolute folder name.
_⚠️ Potential issue_ | _🟠 Major_ **Use manifest.id for folder name; manifest.dir may be undefined or absolute** Using `manifest.dir` here is risky (often undefined or absolute), which can break downstream joins. Prefer `manifest.id` (the plugin folder) for consistent resolution. Apply this diff: ```diff - const pluginDirName = plugin?.manifest?.dir || 'typst-pdf-export'; + const pluginDirName = plugin?.manifest?.id || 'typst-pdf-export'; ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. ```suggestion const pluginDirName = plugin?.manifest?.id || 'typst-pdf-export'; const configDir = plugin?.app.vault.configDir || '.obsidian'; const possiblePluginDirs = this.getPossiblePluginDirs(plugin, pluginDirName, configDir); ``` </details> <!-- suggestion_end --> <details> <summary>🤖 Prompt for AI Agents</summary> ``` In src/converters/pdf/BinaryLocator.ts around lines 32 to 35, the code uses plugin?.manifest?.dir for the plugin folder which can be undefined or an absolute path and break downstream joins; change it to use plugin?.manifest?.id (the plugin folder name) as the primary value and fall back to a safe literal like 'typst-pdf-export' or plugin.id if manifest.id is missing; keep configDir as plugin?.app.vault.configDir || '.obsidian' and pass the new pluginDirName into getPossiblePluginDirs so subsequent path joins use a consistent, non-absolute folder name. ``` </details> <!-- fingerprinting:phantom:medusa:chinchilla --> <!-- This is an auto-generated comment by CodeRabbit -->
return {
binaryPath: pdf2imgPath,
exists: true
};
// Try to find the binary with platform-specific handling
const binaryLocation = await this.findBinaryWithFallbacks(pluginDir, plugin);
return binaryLocation;
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : String(error);
@@ -77,6 +65,64 @@ export class BinaryLocator {
}
}
/**
* Find the pdf2img binary with platform-specific extensions and multiple fallback paths.
* @param pluginDir The plugin directory path
* @param plugin The plugin instance for path utilities
* @returns Binary location info
*/
private static async findBinaryWithFallbacks(pluginDir: string, plugin?: obsidianTypstPDFExport): Promise<BinaryLocation> {
// Define Windows-specific executable extensions
const windowsExtensions = process.platform === 'win32' ? ['.cmd', '.bat', '.ps1', '.exe'] : [''];
// Get platform-specific binary name
const baseBinaryName = 'pdf2img';
// Define fallback search paths for different installation methods
const fallbackPaths = [
// Standard npm binary location
path.join(pluginDir, 'node_modules', '.bin'),
// Direct package binary location
path.join(pluginDir, 'node_modules', 'pdf-to-img', 'bin'),
// Windows-specific npm locations
...(process.platform === 'win32' ? [
path.join(process.env.APPDATA || '', 'npm'),
path.join(process.env.LOCALAPPDATA || '', 'npm-cache'),
// Additional Windows global npm locations
path.join(process.env.PROGRAMFILES || 'C:\\Program Files', 'nodejs'),
path.join(process.env['PROGRAMFILES(X86)'] || 'C:\\Program Files (x86)', 'nodejs')
] : [])
];
// Try each fallback path with platform-specific extensions
for (const searchPath of fallbackPaths) {
for (const extension of windowsExtensions) {
const binaryName = baseBinaryName + extension;
const fullBinaryPath = path.join(searchPath, binaryName);
// Check if this binary exists and is accessible
const exists = await this.validateBinaryExists(fullBinaryPath, plugin);
if (exists) {
return {
binaryPath: fullBinaryPath,
exists: true
};
}
}
}
// If no binary found, return the default expected path for error reporting
const defaultPath = path.join(pluginDir, 'node_modules', '.bin', baseBinaryName);
const finalPath = process.platform === 'win32' ? defaultPath + '.cmd' : defaultPath;
return {
binaryPath: finalPath,
exists: false,
error: `PDF2IMG binary not found. Searched in ${fallbackPaths.length} locations with platform-specific extensions. Expected at: ${finalPath}`
};
}
/**
* Get possible plugin directory locations based on different Obsidian environments.
* @param plugin Plugin instance
1