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
24 changes: 24 additions & 0 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const {
_readLockFile,
_readLockFileWithWriter,
_resolveEsbuildBin,
_resolveHostOpencliRoot,
uninstallPlugin,
updatePlugin,
_parseSource,
Expand Down Expand Up @@ -403,6 +404,29 @@ describe('resolveEsbuildBin', () => {
});
});

describe('resolveHostOpencliRoot', () => {
let tmpDir: string;

beforeEach(() => {
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'opencli-host-root-test-'));
});

afterEach(() => {
fs.rmSync(tmpDir, { recursive: true, force: true });
});

it('walks up from compiled dist/src files to the package root', () => {
fs.writeFileSync(
path.join(tmpDir, 'package.json'),
JSON.stringify({ name: '@jackwener/opencli' }),
);
const distSrcDir = path.join(tmpDir, 'dist', 'src');
fs.mkdirSync(distSrcDir, { recursive: true });

expect(_resolveHostOpencliRoot(path.join(distSrcDir, 'plugin.js'))).toBe(tmpDir);
});
});

describe('listPlugins', () => {
const testDir = path.join(PLUGINS_DIR, '__test-list-plugin__');

Expand Down
34 changes: 27 additions & 7 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1381,11 +1381,7 @@ function parseSource(
*/
function linkHostOpencli(pluginDir: string): void {
try {
// Determine the host opencli package root from this module's location.
// Both dev (tsx src/plugin.ts) and prod (node dist/plugin.js) are one level
// deep, so path.dirname + '..' always gives us the package root.
const thisFile = fileURLToPath(import.meta.url);
const hostRoot = path.resolve(path.dirname(thisFile), '..');
const hostRoot = resolveHostOpencliRoot();

const targetLink = path.join(pluginDir, 'node_modules', '@jackwener', 'opencli');

Expand All @@ -1411,8 +1407,7 @@ function linkHostOpencli(pluginDir: string): void {
* Resolve the path to the esbuild CLI executable with fallback strategies.
*/
export function resolveEsbuildBin(): string | null {
const thisFile = fileURLToPath(import.meta.url);
const hostRoot = path.resolve(path.dirname(thisFile), '..');
const hostRoot = resolveHostOpencliRoot();

// Strategy 1 (Windows): prefer the .cmd wrapper which is executable via shell
if (isWindows) {
Expand Down Expand Up @@ -1466,6 +1461,30 @@ export function resolveEsbuildBin(): string | null {
return null;
}

function resolveHostOpencliRoot(startFile = fileURLToPath(import.meta.url)): string {
let dir = path.dirname(startFile);

while (true) {
const pkgPath = path.join(dir, 'package.json');
if (fs.existsSync(pkgPath)) {
try {
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'));
if (pkg?.name === '@jackwener/opencli') {
return dir;
}
} catch {
// Keep walking; a malformed package.json should not hide an ancestor package root.
}
}

const parent = path.dirname(dir);
if (parent === dir) break;
dir = parent;
}

return path.resolve(path.dirname(startFile), '..');
}

/**
* Transpile TS plugin files to JS so they work in production mode.
* Uses esbuild from the host opencli's node_modules for fast single-file transpilation.
Expand Down Expand Up @@ -1512,6 +1531,7 @@ function transpilePluginTs(pluginDir: string): void {
}

export {
resolveHostOpencliRoot as _resolveHostOpencliRoot,
resolveEsbuildBin as _resolveEsbuildBin,
getCommitHash as _getCommitHash,
installDependencies as _installDependencies,
Expand Down