Enhances path handling and file resolution #5
@@ -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);
|
||||
|
|
||||
|
||||
// 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');
|
||||
|
⚠️ Potential issue | 🟠 Major Use manifest.id for folder name; manifest.dir may be undefined or absolute Using Apply this diff: 📝 Committable suggestion
🤖 Prompt for AI Agents_⚠️ 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;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user
⚠️ 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:
📝 Committable suggestion
🤖 Prompt for AI Agents
✅ Addressed in commits
24a169dto2cdc9c3