Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sharp-bins-resolve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ankhorage/devtools': patch
---

Fix bundled tool binary wrappers for packages that do not export their package metadata.
22 changes: 19 additions & 3 deletions src/internal/runPackageBin.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
import { spawn } from 'node:child_process';
import { readFileSync } from 'node:fs';
import { existsSync, readFileSync } from 'node:fs';
import { createRequire } from 'node:module';
import { dirname, resolve } from 'node:path';
import { dirname, join, resolve } from 'node:path';

const require = createRequire(import.meta.url);

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null;
}

function findPackageJsonPath(packageName: string): string {
let currentDirectory = dirname(require.resolve(packageName));

while (currentDirectory !== dirname(currentDirectory)) {
const packageJsonPath = join(currentDirectory, 'package.json');

if (existsSync(packageJsonPath)) {
return packageJsonPath;
}

currentDirectory = dirname(currentDirectory);
}

throw new Error(`Could not find package metadata for ${packageName}.`);
}

function readPackageBinPath(packageName: string, binName: string): string {
const packageJsonPath = require.resolve(`${packageName}/package.json`);
const packageJsonPath = findPackageJsonPath(packageName);
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) as unknown;

if (!isRecord(packageJson)) {
Expand Down
Loading