libvips.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. 'use strict';
  2. const fs = require('fs');
  3. const os = require('os');
  4. const path = require('path');
  5. const spawnSync = require('child_process').spawnSync;
  6. const semver = require('semver');
  7. const platform = require('./platform');
  8. const env = process.env;
  9. const minimumLibvipsVersionLabelled = env.npm_package_config_libvips || /* istanbul ignore next */
  10. require('../package.json').config.libvips;
  11. const minimumLibvipsVersion = semver.coerce(minimumLibvipsVersionLabelled).version;
  12. const spawnSyncOptions = {
  13. encoding: 'utf8',
  14. shell: true
  15. };
  16. const mkdirSync = function (dirPath) {
  17. try {
  18. fs.mkdirSync(dirPath);
  19. } catch (err) {
  20. /* istanbul ignore if */
  21. if (err.code !== 'EEXIST') {
  22. throw err;
  23. }
  24. }
  25. };
  26. const cachePath = function () {
  27. const npmCachePath = env.npm_config_cache || /* istanbul ignore next */
  28. (env.APPDATA ? path.join(env.APPDATA, 'npm-cache') : path.join(os.homedir(), '.npm'));
  29. mkdirSync(npmCachePath);
  30. const libvipsCachePath = path.join(npmCachePath, '_libvips');
  31. mkdirSync(libvipsCachePath);
  32. return libvipsCachePath;
  33. };
  34. const globalLibvipsVersion = function () {
  35. if (process.platform !== 'win32') {
  36. const globalLibvipsVersion = spawnSync(`PKG_CONFIG_PATH="${pkgConfigPath()}" pkg-config --modversion vips-cpp`, spawnSyncOptions).stdout || '';
  37. return globalLibvipsVersion.trim();
  38. } else {
  39. return '';
  40. }
  41. };
  42. const hasVendoredLibvips = function () {
  43. const currentPlatformId = platform();
  44. const vendorPath = path.join(__dirname, '..', 'vendor');
  45. let vendorVersionId;
  46. let vendorPlatformId;
  47. try {
  48. vendorVersionId = require(path.join(vendorPath, 'versions.json')).vips;
  49. vendorPlatformId = require(path.join(vendorPath, 'platform.json'));
  50. } catch (err) {}
  51. /* istanbul ignore if */
  52. if (vendorVersionId && vendorVersionId !== minimumLibvipsVersion) {
  53. throw new Error(`Found vendored libvips v${vendorVersionId} but require v${minimumLibvipsVersion}. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.`);
  54. }
  55. /* istanbul ignore else */
  56. if (vendorPlatformId) {
  57. /* istanbul ignore else */
  58. if (currentPlatformId === vendorPlatformId) {
  59. return true;
  60. } else {
  61. throw new Error(`'${vendorPlatformId}' binaries cannot be used on the '${currentPlatformId}' platform. Please remove the 'node_modules/sharp/vendor' directory and run 'npm install'.`);
  62. }
  63. } else {
  64. return false;
  65. }
  66. };
  67. const pkgConfigPath = function () {
  68. if (process.platform !== 'win32') {
  69. const brewPkgConfigPath = spawnSync('which brew >/dev/null 2>&1 && eval $(brew --env) && echo $PKG_CONFIG_LIBDIR', spawnSyncOptions).stdout || '';
  70. return [brewPkgConfigPath.trim(), env.PKG_CONFIG_PATH, '/usr/local/lib/pkgconfig', '/usr/lib/pkgconfig']
  71. .filter(function (p) { return !!p; })
  72. .join(':');
  73. } else {
  74. return '';
  75. }
  76. };
  77. const useGlobalLibvips = function () {
  78. if (Boolean(env.SHARP_IGNORE_GLOBAL_LIBVIPS) === true) {
  79. return false;
  80. }
  81. const globalVipsVersion = globalLibvipsVersion();
  82. return !!globalVipsVersion && /* istanbul ignore next */
  83. semver.gte(globalVipsVersion, minimumLibvipsVersion);
  84. };
  85. module.exports = {
  86. minimumLibvipsVersion,
  87. minimumLibvipsVersionLabelled,
  88. cachePath,
  89. globalLibvipsVersion,
  90. hasVendoredLibvips,
  91. pkgConfigPath,
  92. useGlobalLibvips,
  93. mkdirSync
  94. };