Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: run prebuild-install only if actually used by the package #83

Merged
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
30 changes: 26 additions & 4 deletions lib/producer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,36 @@ function findPackageJson(nodeFile: string) {
return dir;
}

function getPrebuildEnvPrefix(pkgName: string): string {
return `npm_config_${(pkgName || '')
.replace(/[^a-zA-Z0-9]/g, '_')
.replace(/^_/, '')}`;
}

function nativePrebuildInstall(target: Target, nodeFile: string) {
const prebuildInstall = path.join(
__dirname,
'../node_modules/.bin/prebuild-install',
);
const dir = findPackageJson(nodeFile);
const packageJson = JSON.parse(
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }),
);

// only try prebuild-install for packages that actually use it or if
// explicitly configured via environment variables
const envPrefix = getPrebuildEnvPrefix(packageJson.name);
if (
packageJson.dependencies?.['prebuild-install'] == null &&
![
`${envPrefix}_binary_host`,
`${envPrefix}_binary_host_mirror`,
`${envPrefix}_local_prebuilds`,
].some((i) => i in process.env)
) {
return;
}

// parse the target node version from the binaryPath
const nodeVersion = path.basename(target.binaryPath).split('-')[1];

Expand All @@ -221,9 +245,7 @@ function nativePrebuildInstall(target: Target, nodeFile: string) {
fs.copyFileSync(nodeFile, `${nodeFile}.bak`);
}

const napiVersions = JSON.parse(
fs.readFileSync(path.join(dir, 'package.json'), { encoding: 'utf-8' }),
)?.binary?.napi_versions;
const napiVersions = packageJson?.binary?.napi_versions;

const options = [
'--platform',
Expand Down Expand Up @@ -461,7 +483,7 @@ export default function producer({
try {
const platformFile = nativePrebuildInstall(target, stripe.file);

if (fs.existsSync(platformFile)) {
if (platformFile && fs.existsSync(platformFile)) {
return cb(
null,
pipeMayCompressToNewMeter(
Expand Down
Loading