droid1.28.07.0 kB
/**
* Platform detection utilities for @factory/cli
*
* Exports the platform package mapping and helper functions for
* determining the correct binary to use.
*/
const fs = require('fs');
const child_process = require('child_process');
// The template directory has no package.json before the build.
let PACKAGE = { name: 'droid', version: '0.0.0' };
try {
PACKAGE = require('./package.json');
} catch {
// Keep the template defaults.
}
// Platform-specific package mapping
// x64 platforms have both regular (AVX2) and baseline (no AVX2) variants
const PLATFORM_PACKAGES = {
'darwin-arm64': { regular: '@factory/cli-darwin-arm64' },
'darwin-x64': {
regular: '@factory/cli-darwin-x64',
baseline: '@factory/cli-darwin-x64-baseline',
},
'linux-arm64': { regular: '@factory/cli-linux-arm64' },
'linux-x64': {
regular: '@factory/cli-linux-x64',
baseline: '@factory/cli-linux-x64-baseline',
},
'win32-x64': {
regular: '@factory/cli-win32-x64',
baseline: '@factory/cli-win32-x64-baseline',
},
'win32-arm64': { regular: '@factory/cli-win32-arm64' },
};
// All packages (used for optionalDependencies)
const ALL_PACKAGES = Object.values(PLATFORM_PACKAGES).flatMap((p) =>
[p.regular, p.baseline].filter(Boolean)
);
function getPlatformKey() {
return `${process.platform}-${process.arch}`;
}
function getBinaryName() {
return process.platform === 'win32' ? 'droid.exe' : 'droid';
}
function getPlatformPackages() {
const platformKey = getPlatformKey();
return PLATFORM_PACKAGES[platformKey] || null;
}
/**
* Detect if CPU supports AVX2 instructions.
* Returns true if AVX2 is supported, false if not, null if detection failed.
*/
function detectAVX2Support() {
try {
if (process.platform === 'linux') {
const cpuinfo = fs.readFileSync('/proc/cpuinfo', 'utf8');
return cpuinfo.toLowerCase().includes('avx2');
} else if (process.platform === 'darwin') {
const result = child_process.execSync(
'sysctl -n machdep.cpu.leaf7_features',
{ encoding: 'utf8', timeout: 5000 }
);
return result.toLowerCase().includes('avx2');
} else if (process.platform === 'win32') {
// Windows: use kernel32.dll IsProcessorFeaturePresent(40) via PowerShell
// Feature ID 40 = PF_AVX2_INSTRUCTIONS_AVAILABLE
// This is the same method used by the Factory CLI installer and Bun
const script =
'$ProgressPreference = "SilentlyContinue"; ' +
'try { ' +
'$hasAvx2 = (Add-Type -MemberDefinition \'[DllImport("kernel32.dll")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);\' -Name "Kernel32" -Namespace "Win32" -PassThru -ErrorAction Stop)::IsProcessorFeaturePresent(40); ' +
'if ($hasAvx2) { Write-Output "true" } else { Write-Output "false" } ' +
'} catch { ' +
'try { ' +
'$hasAvx2 = ([Win32.Kernel32]::IsProcessorFeaturePresent(40)); ' +
'if ($hasAvx2) { Write-Output "true" } else { Write-Output "false" } ' +
'} catch { ' +
'Write-Output "unknown" ' +
'} }';
const encodedScript = Buffer.from(script, 'utf16le').toString('base64');
const result = child_process.execFileSync(
'powershell',
['-NoProfile', '-NonInteractive', '-EncodedCommand', encodedScript],
{ encoding: 'utf8', timeout: 10000 }
);
const output = result.trim().toLowerCase();
if (output === 'true') return true;
if (output === 'false') return false;
// 'unknown' or other - detection failed
return null;
}
} catch (e) {
// Detection failed
}
return null;
}
/**
* The platform package this machine should run: the baseline build on an x64
* CPU known to lack AVX2, else the regular one. Null on an unsupported
* platform.
*/
function getPreferredPackageName() {
const packages = getPlatformPackages();
if (!packages) return null;
return packages.baseline && detectAVX2Support() === false
? packages.baseline
: packages.regular;
}
/**
* What is missing when this machine has no droid binary: an unsupported
* platform, or the exact platform package and version, which the user's
* package manager installs. The package ships as both `droid` and
* `@factory/cli`.
*/
function getMissingBinaryMessage() {
const platformKey = getPlatformKey();
if (!getPlatformPackages()) {
return (
`Unsupported platform: ${platformKey}\n` +
`Supported platforms: ${Object.keys(PLATFORM_PACKAGES).join(', ')}`
);
}
const spec = `${getPreferredPackageName()}@${PACKAGE.version}`;
// What to fix, not one command: reinstalling does not help while the
// registry lacks the package or the package manager skips this platform.
return (
`No droid binary for ${platformKey} was found. ${PACKAGE.name} gets it from its optional dependency ${spec}, ` +
`which is not installed or has no bin/${getBinaryName()}.\n` +
`To install it, serve ${spec} from your registry and install optional dependencies for ${platformKey} ` +
`(npm: without --omit=optional; Yarn: this OS and CPU in supportedArchitectures), then install ${PACKAGE.name} again.`
);
}
function resolveBinaryPath(packageName) {
if (!packageName) return null;
try {
return require.resolve(`${packageName}/bin/${getBinaryName()}`, {
paths: [__dirname, process.cwd()],
});
} catch (e) {
return null;
}
}
/**
* Get the binary path with AVX2-aware package selection.
* Returns { path, pkg, hasBaseline } or null if not found.
*/
function getBinaryPathWithInfo() {
const packages = getPlatformPackages();
if (!packages) {
return null;
}
const hasBaseline = !!packages.baseline;
const preferredPkg = getPreferredPackageName();
const fallbackPkg =
preferredPkg === packages.baseline ? packages.regular : packages.baseline;
let binPath = resolveBinaryPath(preferredPkg);
let selectedPkg = preferredPkg;
if (!binPath && fallbackPkg) {
binPath = resolveBinaryPath(fallbackPkg);
selectedPkg = fallbackPkg;
}
if (!binPath) {
return null;
}
return { path: binPath, pkg: selectedPkg, hasBaseline };
}
/**
* Get the binary path (simple version for backward compatibility).
*
* `@factory/droid-sdk` calls this too. Given this package's `bin/droid`, it
* loads `../platform.js` from beside it with `require` and starts the
* returned binary itself, because the launcher cannot pass an IPC channel
* on. It knows the launcher by the two lines `bin/droid` opens with. Keep
* this file CommonJS and at this path, and keep this function synchronous,
* returning the path or null.
*/
function getBinaryPath() {
const result = getBinaryPathWithInfo();
return result ? result.path : null;
}
function isSupportedPlatform() {
return getPlatformPackages() !== null;
}
module.exports = {
PACKAGE,
PLATFORM_PACKAGES,
ALL_PACKAGES,
getPlatformKey,
getBinaryName,
getPlatformPackages,
detectAVX2Support,
getPreferredPackageName,
getMissingBinaryMessage,
resolveBinaryPath,
getBinaryPathWithInfo,
getBinaryPath,
isSupportedPlatform,
};
Shown as plain text from the published tarball, after apm checked it against the version's checksum. Nothing in it runs on this page.