Simplify logic to determine installation help

This commit is contained in:
Lovell Fuller 2023-11-06 16:49:53 +00:00
parent 8c5a493f68
commit 074004392c
3 changed files with 67 additions and 58 deletions

View File

@ -118,11 +118,11 @@ When building your deployment package on a machine that differs from the target
you will need to install either `@img/sharp-linux-x64` or `@img/sharp-linux-arm64` package. you will need to install either `@img/sharp-linux-x64` or `@img/sharp-linux-arm64` package.
```sh ```sh
npm install --os=linux --cpu=x64 npm install --os=linux --cpu=x64 sharp
``` ```
```sh ```sh
npm install --os=linux --cpu=arm64 npm install --os=linux --cpu=arm64 sharp
``` ```
When using npm 9 or earlier, this can be achieved using the following: When using npm 9 or earlier, this can be achieved using the following:

View File

@ -9,36 +9,46 @@ const { familySync, versionSync } = require('detect-libc');
const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips'); const { runtimePlatformArch, prebuiltPlatforms, minimumLibvipsVersion } = require('./libvips');
const runtimePlatform = runtimePlatformArch(); const runtimePlatform = runtimePlatformArch();
const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
const paths = [
`../src/build/Release/sharp-${runtimePlatform}.node`,
`@img/sharp-${runtimePlatform}/sharp.node`
];
const errors = [];
for (const path of paths) {
try {
module.exports = require(path);
break;
} catch (err) {
/* istanbul ignore next */
errors.push(err);
}
}
/* istanbul ignore next */ /* istanbul ignore next */
try { if (!module.exports) {
// Check for local build const [isLinux, isMacOs, isWindows] = ['linux', 'darwin', 'win32'].map(os => runtimePlatform.startsWith(os));
module.exports = require(`../src/build/Release/sharp-${runtimePlatform}.node`);
} catch (errLocal) { const help = [`Could not load the "sharp" module using the ${runtimePlatform} runtime`];
try { errors.forEach(err => {
// Check for runtime package if (err.code !== 'MODULE_NOT_FOUND') {
module.exports = require(`@img/sharp-${runtimePlatform}/sharp.node`); help.push(`${err.code}: ${err.message}`);
} catch (errPackage) {
const help = ['Could not load the "sharp" module at runtime'];
if (errLocal.code !== 'MODULE_NOT_FOUND') {
help.push(`${errLocal.code}: ${errLocal.message}`);
}
if (errPackage.code !== 'MODULE_NOT_FOUND') {
help.push(`${errPackage.code}: ${errPackage.message}`);
} }
});
const messages = errors.map(err => err.message).join(' ');
help.push('Possible solutions:'); help.push('Possible solutions:');
// Common error messages // Common error messages
if (prebuiltPlatforms.includes(runtimePlatform)) { if (prebuiltPlatforms.includes(runtimePlatform)) {
const [os, cpu] = runtimePlatform.split('-'); const [os, cpu] = runtimePlatform.split('-');
help.push('- Add an explicit dependency for the runtime platform:'); help.push('- Add platform-specific dependencies:');
help.push(` npm install --os=${os} --cpu=${cpu} sharp`); help.push(` npm install --os=${os} --cpu=${cpu} sharp`);
help.push(' or'); help.push(' or');
help.push(` npm install --force @img/sharp-${runtimePlatform}`); help.push(` npm install --force @img/sharp-${runtimePlatform}`);
} else { } else {
help.push(`- The ${runtimePlatform} platform requires manual installation of libvips >= ${minimumLibvipsVersion}`); help.push(`- Manually install libvips >= ${minimumLibvipsVersion}`);
} }
if (isLinux && /symbol not found/i.test(errPackage)) { if (isLinux && /symbol not found/i.test(messages)) {
try { try {
const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`); const { engines } = require(`@img/sharp-libvips-${runtimePlatform}/package`);
const libcFound = `${familySync()} ${versionSync()}`; const libcFound = `${familySync()} ${versionSync()}`;
@ -48,11 +58,11 @@ try {
help.push(` Requires ${libcRequires}`); help.push(` Requires ${libcRequires}`);
} catch (errEngines) {} } catch (errEngines) {}
} }
if (isMacOs && /Incompatible library version/.test(errLocal.message)) { if (isMacOs && /Incompatible library version/.test(messages)) {
help.push('- Update Homebrew:'); help.push('- Update Homebrew:');
help.push(' brew update && brew upgrade vips'); help.push(' brew update && brew upgrade vips');
} }
if (errPackage.code === 'ERR_DLOPEN_DISABLED') { if (errors.some(err => err.code === 'ERR_DLOPEN_DISABLED')) {
help.push('- Run Node.js without using the --no-addons flag'); help.push('- Run Node.js without using the --no-addons flag');
} }
if (process.versions.pnp) { if (process.versions.pnp) {
@ -60,11 +70,10 @@ try {
help.push(' yarn config set nodeLinker node-modules'); help.push(' yarn config set nodeLinker node-modules');
} }
// Link to installation docs // Link to installation docs
if (isWindows && /The specified procedure could not be found/.test(errPackage.message)) { if (isWindows && /The specified procedure could not be found/.test(messages)) {
help.push('- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows'); help.push('- Using the canvas package on Windows? See https://sharp.pixelplumbing.com/install#canvas-and-windows');
} else { } else {
help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install'); help.push('- Consult the installation documentation: https://sharp.pixelplumbing.com/install');
} }
throw new Error(help.join('\n')); throw new Error(help.join('\n'));
}
} }

View File

@ -164,7 +164,7 @@
"@img/sharp-libvips-win32-ia32": "0.0.3", "@img/sharp-libvips-win32-ia32": "0.0.3",
"@img/sharp-libvips-win32-x64": "0.0.3", "@img/sharp-libvips-win32-x64": "0.0.3",
"@types/node": "*", "@types/node": "*",
"async": "^3.2.4", "async": "^3.2.5",
"cc": "^3.0.1", "cc": "^3.0.1",
"exif-reader": "^2.0.0", "exif-reader": "^2.0.0",
"extract-zip": "^2.0.1", "extract-zip": "^2.0.1",