Improve resilience of installation check

This commit is contained in:
Lovell Fuller 2023-12-17 09:46:28 +00:00
parent 5cd787bf85
commit f28e79ef4f

View File

@ -3,34 +3,39 @@
'use strict'; 'use strict';
const { useGlobalLibvips, globalLibvipsVersion, log, spawnRebuild } = require('../lib/libvips'); try {
const { useGlobalLibvips, globalLibvipsVersion, log, spawnRebuild } = require('../lib/libvips');
const buildFromSource = (msg) => { const buildFromSource = (msg) => {
log(msg); log(msg);
log('Attempting to build from source via node-gyp'); log('Attempting to build from source via node-gyp');
try { try {
require('node-addon-api'); require('node-addon-api');
log('Found node-addon-api'); log('Found node-addon-api');
} catch (err) { } catch (err) {
log('Please add node-addon-api to your dependencies'); log('Please add node-addon-api to your dependencies');
return; return;
} }
try { try {
const gyp = require('node-gyp'); const gyp = require('node-gyp');
log(`Found node-gyp version ${gyp().version}`); log(`Found node-gyp version ${gyp().version}`);
} catch (err) { } catch (err) {
log('Please add node-gyp to your dependencies'); log('Please add node-gyp to your dependencies');
return; return;
} }
log('See https://sharp.pixelplumbing.com/install#building-from-source'); log('See https://sharp.pixelplumbing.com/install#building-from-source');
const status = spawnRebuild(); const status = spawnRebuild();
if (status !== 0) { if (status !== 0) {
process.exit(status); process.exit(status);
} }
}; };
if (useGlobalLibvips()) { if (useGlobalLibvips()) {
buildFromSource(`Detected globally-installed libvips v${globalLibvipsVersion()}`); buildFromSource(`Detected globally-installed libvips v${globalLibvipsVersion()}`);
} else if (process.env.npm_config_build_from_source) { } else if (process.env.npm_config_build_from_source) {
buildFromSource('Detected --build-from-source flag'); buildFromSource('Detected --build-from-source flag');
}
} catch (err) {
const summary = err.message.split(/\n/).slice(0, 1);
console.log(`sharp: skipping install check: ${summary}`);
} }