"use strict"; // This is very hacky, but this should be temporary hack // until we make "sharp" a peerDependency and can be removed afterwards. // It's not done yet because it would cause too much friction. // Image processing is important part of Gatsby ecosystem // and there's lot of guides and tutorials that we don't control // that would be outdated. Moving "sharp" to be peerDependency // is scheduled for gatsby@3. // This file is duplicated in multiple location in this repository, // So make sure to apply same changes every "safe-sharp" file. var childProcess = require("child_process"); var path = require("path"); var fs = require("fs"); var semver = require("semver"); var originalConsoleError = console.error; var restoreConsoleError = function restoreConsoleError() { console.error = originalConsoleError; }; var getDetailedMessage = function getDetailedMessage() { try { // `npm list` seems to work in yarn installed projects as long // as there is no package-lock.json, so let's bail out // if both lock files exist if (fs.existsSync(path.join(process.cwd(), "package-lock.json")) && fs.existsSync(path.join(process.cwd(), "yarn.lock"))) { return null; } var msg = []; var _JSON$parse = JSON.parse(childProcess.execSync("npm list sharp --json", { encoding: "utf-8" })), dependencies = _JSON$parse.dependencies; var findSharpVersion = function findSharpVersion(dependency) { if (dependency.dependencies.sharp) { return dependency.dependencies.sharp.version; } var _arr = Object.keys(dependency.dependencies); for (var _i = 0; _i < _arr.length; _i++) { var depName = _arr[_i]; var v = findSharpVersion(dependency.dependencies[depName]); if (v) { return v; } } return null; }; var _Object$keys$reduce = Object.keys(dependencies).reduce(function (acc, depName) { var sharpVersion = findSharpVersion(dependencies[depName]); if (sharpVersion) { acc.topLevelPackages[depName] = sharpVersion; if (!acc.latestVersion || semver.gt(sharpVersion, acc.latestVersion)) { acc.latestVersion = sharpVersion; } } return acc; }, { latestVersion: undefined, topLevelPackages: {} }), latestVersion = _Object$keys$reduce.latestVersion, topLevelPackages = _Object$keys$reduce.topLevelPackages; var packagesToUpdate = []; // list top level dependencies msg = msg.concat(["List of installed packages that depend on sharp:"].concat(Object.keys(topLevelPackages).map(function (depName) { var sharpVersion = topLevelPackages[depName]; if (sharpVersion !== latestVersion) { packagesToUpdate.push(depName); } return " - " + depName + (sharpVersion ? " (" + sharpVersion + ")" + (sharpVersion !== latestVersion ? " - needs update" : "") : ""); }))); if (packagesToUpdate.length > 0) { msg = msg.concat(["", "If you are using npm, run:", "", "npm install " + packagesToUpdate.join(" "), "", "If you are using yarn, run:", "", "yarn add " + packagesToUpdate.join(" ")]); } return msg; // eslint-disable-next-line no-empty } catch (_unused) { return null; } }; var handleMessage = function handleMessage(msg) { if (msg.includes("Incompatible library version: sharp.node requires")) { restoreConsoleError(); var _msg = ["It looks like there are multiple versions of \"sharp\" module installed.", "Please update any packages that depend on \"sharp\".", ""]; var detailedMessage = getDetailedMessage(); if (!detailedMessage) { _msg = _msg.concat(["To get a list of installed packages that depend on \"sharp\" try running:", " - npm list sharp (if you use npm)", " - yarn why sharp (if you use yarn)", " and update packages that depend on version older than latest listed in output of above command."]); } else { _msg = _msg.concat(detailedMessage); } _msg = _msg.concat(["", "If an older version of \"sharp\" still persists and this error is displayed after updating your packages, open an issue in the package's repository and request them to update the \"sharp\" dependency."]); console.error(_msg.join("\n")); } }; var sharp; try { // sharp@0.22.1 uses console.error and then process.exit and doesn't throw // so to capture error and provide meaningful troubleshooting guide // we intercept console.error calls and add special handling. console.error = function (msg) { for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { args[_key - 1] = arguments[_key]; } originalConsoleError.apply(void 0, [msg].concat(args)); handleMessage(msg); }; sharp = require("sharp"); } catch (e) { handleMessage(e.toString()); throw e; } finally { restoreConsoleError(); } module.exports = sharp;