fix(tooling): resolve Bun executable on Windows
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { spawnSync } from 'node:child_process';
|
||||
import { existsSync } from 'node:fs';
|
||||
import path from 'node:path';
|
||||
|
||||
const findWindowsExecutable = (name, env) => {
|
||||
const result = spawnSync('where.exe', [name], {
|
||||
encoding: 'utf8',
|
||||
env,
|
||||
windowsHide: true,
|
||||
});
|
||||
if (result.error || result.status !== 0) {
|
||||
return null;
|
||||
}
|
||||
return String(result.stdout || '')
|
||||
.split(/\r?\n/)
|
||||
.map((entry) => entry.trim())
|
||||
.find(Boolean) ?? null;
|
||||
};
|
||||
|
||||
export function resolveBunExecutable(options = {}) {
|
||||
const platform = options.platform ?? process.platform;
|
||||
const env = options.env ?? process.env;
|
||||
const fileExists = options.fileExists ?? existsSync;
|
||||
const findOnPath = options.findOnPath ?? findWindowsExecutable;
|
||||
const pathApi = platform === 'win32' ? path.win32 : path;
|
||||
|
||||
const npmExecutable = env.npm_execpath?.trim();
|
||||
const npmExecutableName = platform === 'win32' ? 'bun.exe' : 'bun';
|
||||
if (
|
||||
npmExecutable &&
|
||||
pathApi.basename(npmExecutable).toLowerCase() === npmExecutableName &&
|
||||
fileExists(npmExecutable)
|
||||
) {
|
||||
return npmExecutable;
|
||||
}
|
||||
|
||||
if (platform === 'win32') {
|
||||
const directExecutable = findOnPath('bun.exe', env);
|
||||
if (directExecutable) {
|
||||
return directExecutable;
|
||||
}
|
||||
|
||||
const shim = findOnPath('bun.cmd', env);
|
||||
if (shim) {
|
||||
const executable = pathApi.resolve(
|
||||
pathApi.dirname(shim),
|
||||
'node_modules',
|
||||
'bun',
|
||||
'bin',
|
||||
'bun.exe',
|
||||
);
|
||||
if (fileExists(executable)) {
|
||||
return executable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 'bun';
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import path from 'node:path';
|
||||
import test from 'node:test';
|
||||
|
||||
import { resolveBunExecutable } from './bun-executable.mjs';
|
||||
|
||||
test('uses the Bun executable that launched the package script', () => {
|
||||
const executable = '/opt/bun/bin/bun';
|
||||
assert.equal(resolveBunExecutable({
|
||||
platform: 'linux',
|
||||
env: { npm_execpath: executable },
|
||||
fileExists: (candidate) => candidate === executable,
|
||||
}), executable);
|
||||
});
|
||||
|
||||
test('prefers bun.exe from PATH on Windows', () => {
|
||||
const executable = 'C:\\Tools\\Bun\\bun.exe';
|
||||
assert.equal(resolveBunExecutable({
|
||||
platform: 'win32',
|
||||
env: {},
|
||||
fileExists: () => false,
|
||||
findOnPath: (name) => name === 'bun.exe' ? executable : null,
|
||||
}), executable);
|
||||
});
|
||||
|
||||
test('does not return an extensionless Windows npm shim', () => {
|
||||
const shim = 'C:\\Users\\dev\\AppData\\Roaming\\npm\\bun';
|
||||
const executable = 'C:\\Tools\\Bun\\bun.exe';
|
||||
|
||||
assert.equal(resolveBunExecutable({
|
||||
platform: 'win32',
|
||||
env: { npm_execpath: shim },
|
||||
fileExists: (candidate) => candidate === shim,
|
||||
findOnPath: (name) => name === 'bun.exe' ? executable : null,
|
||||
}), executable);
|
||||
});
|
||||
|
||||
test('derives bun.exe when Windows PATH exposes only the npm bun.cmd shim', () => {
|
||||
const shim = 'C:\\Users\\dev\\AppData\\Roaming\\npm\\bun.cmd';
|
||||
const executable = path.win32.resolve(
|
||||
path.win32.dirname(shim),
|
||||
'node_modules',
|
||||
'bun',
|
||||
'bin',
|
||||
'bun.exe',
|
||||
);
|
||||
const env = { Path: 'C:\\Users\\dev\\AppData\\Roaming\\npm' };
|
||||
const lookups = [];
|
||||
|
||||
assert.equal(resolveBunExecutable({
|
||||
platform: 'win32',
|
||||
env,
|
||||
fileExists: (candidate) => candidate === executable,
|
||||
findOnPath: (name, lookupEnv) => {
|
||||
assert.equal(lookupEnv, env);
|
||||
lookups.push(name);
|
||||
return name === 'bun.cmd' ? shim : null;
|
||||
},
|
||||
}), executable);
|
||||
assert.deepEqual(lookups, ['bun.exe', 'bun.cmd']);
|
||||
});
|
||||
|
||||
test('falls back to bun when no directly spawnable executable is available', () => {
|
||||
assert.equal(resolveBunExecutable({
|
||||
platform: 'win32',
|
||||
env: {},
|
||||
fileExists: () => false,
|
||||
findOnPath: () => null,
|
||||
}), 'bun');
|
||||
});
|
||||
Reference in New Issue
Block a user