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 86e59c5421 - Show all commits
+21 -9
View File
@@ -5,6 +5,7 @@
import * as path from 'path';
import type { obsidianTypstPDFExport } from '../../../main';
import { PathUtils } from '../../core/PathUtils';
export interface BinaryLocation {
/** Full path to the binary */
@@ -32,19 +33,26 @@ export class BinaryLocator {
const possiblePluginDirs = this.getPossiblePluginDirs(plugin, pluginDirName, configDir);
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
// Find the first directory that exists and has node_modules
const pluginDir = possiblePluginDirs.find(dir => {
let pluginDir = possiblePluginDirs[0]; // Default fallback
for (const dir of possiblePluginDirs) {
try {
const nodeModulesPath = path.join(dir, 'node_modules');
return require('fs').existsSync(nodeModulesPath);
if (plugin?.app) {
const pathUtils = new PathUtils(plugin.app);
if (await pathUtils.fileExists(nodeModulesPath)) {
pluginDir = dir;
break;
}
}
} catch {
return false;
continue;
}
}) || possiblePluginDirs[0]; // Fallback to first option
}
const pdf2imgPath = path.join(pluginDir, 'node_modules', '.bin', 'pdf2img');
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 -->
// Check if the binary exists
const exists = await this.validateBinaryExists(pdf2imgPath);
const exists = await this.validateBinaryExists(pdf2imgPath, plugin);
if (!exists) {
return {
@@ -83,19 +91,23 @@ export class BinaryLocator {
// Strategy 2: Assuming we're running from vault root
path.join(configDir, 'plugins', pluginDirName),
// Strategy 3: From vault base path if plugin is available
...(plugin ? [path.join((plugin.app.vault.adapter as unknown as { basePath: string }).basePath, plugin.manifest.dir!)] : [])
...(plugin ? [path.join(new PathUtils(plugin.app).getVaultPath(), plugin.manifest.dir!)] : [])
];
}
/**
* Validate that a binary exists and is accessible.
* @param binaryPath Full path to the binary
* @param plugin Plugin instance for PathUtils
* @returns True if binary exists, false otherwise
*/
private static async validateBinaryExists(binaryPath: string): Promise<boolean> {
private static async validateBinaryExists(binaryPath: string, plugin?: obsidianTypstPDFExport): Promise<boolean> {
try {
const fs2 = require('fs');
return fs2.existsSync(binaryPath);
if (!plugin?.app) {
return false;
}
const pathUtils = new PathUtils(plugin.app);
return await pathUtils.fileExists(binaryPath);
} catch {
return false;
}