From 8dffa28b4d19c01cd8d367061f56289920d20cf8 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Sat, 27 Feb 2021 20:23:25 +0000 Subject: [PATCH] Remove npmlog as a direct dependency It remains a transitive dependency via prebuild-install --- install/dll-copy.js | 8 ++++---- install/libvips.js | 21 ++++++++++----------- lib/libvips.js | 9 +++++++++ package.json | 1 - test/unit/libvips.js | 26 ++++++++++++++++++++++++++ 5 files changed, 49 insertions(+), 16 deletions(-) diff --git a/install/dll-copy.js b/install/dll-copy.js index 1e88b1dc..41449ddd 100644 --- a/install/dll-copy.js +++ b/install/dll-copy.js @@ -4,7 +4,6 @@ const fs = require('fs'); const path = require('path'); const libvips = require('../lib/libvips'); -const npmLog = require('npmlog'); const minimumLibvipsVersion = libvips.minimumLibvipsVersion; @@ -12,13 +11,13 @@ const platform = process.env.npm_config_platform || process.platform; if (platform === 'win32') { const buildDir = path.join(__dirname, '..', 'build'); const buildReleaseDir = path.join(buildDir, 'Release'); - npmLog.info('sharp', `Creating ${buildReleaseDir}`); + libvips.log(`Creating ${buildReleaseDir}`); try { libvips.mkdirSync(buildDir); libvips.mkdirSync(buildReleaseDir); } catch (err) {} const vendorLibDir = path.join(__dirname, '..', 'vendor', minimumLibvipsVersion, 'lib'); - npmLog.info('sharp', `Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`); + libvips.log(`Copying DLLs from ${vendorLibDir} to ${buildReleaseDir}`); try { fs .readdirSync(vendorLibDir) @@ -32,6 +31,7 @@ if (platform === 'win32') { ); }); } catch (err) { - npmLog.error('sharp', err.message); + libvips.log(err); + process.exit(1); } } diff --git a/install/libvips.js b/install/libvips.js index 328c868d..e9cd6d0e 100644 --- a/install/libvips.js +++ b/install/libvips.js @@ -7,7 +7,6 @@ const stream = require('stream'); const zlib = require('zlib'); const detectLibc = require('detect-libc'); -const npmLog = require('npmlog'); const semver = require('semver'); const simpleGet = require('simple-get'); const tarFs = require('tar-fs'); @@ -37,12 +36,12 @@ const distBaseUrl = process.env.npm_config_sharp_dist_base_url || process.env.SH const supportsBrotli = ('BrotliDecompress' in zlib); const fail = function (err) { - npmLog.error('sharp', err.message); + libvips.log(err); if (err.code === 'EACCES') { - npmLog.info('sharp', 'Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag'); + libvips.log('Are you trying to install as a root or sudo user? Try again with the --unsafe-perm flag'); } - npmLog.info('sharp', 'Attempting to build from source via node-gyp but this may fail due to the above error'); - npmLog.info('sharp', 'Please see https://sharp.pixelplumbing.com/install for required dependencies'); + libvips.log('Attempting to build from source via node-gyp but this may fail due to the above error'); + libvips.log('Please see https://sharp.pixelplumbing.com/install for required dependencies'); process.exit(1); }; @@ -64,7 +63,7 @@ const extractTarball = function (tarPath, platformAndArch) { function (err) { if (err) { if (/unexpected end of file/.test(err.message)) { - npmLog.error('sharp', `Please delete ${tarPath} as it is not a valid tarball`); + fail(new Error(`Please delete ${tarPath} as it is not a valid tarball`)); } fail(err); } @@ -77,11 +76,11 @@ try { if (useGlobalLibvips) { const globalLibvipsVersion = libvips.globalLibvipsVersion(); - npmLog.info('sharp', `Detected globally-installed libvips v${globalLibvipsVersion}`); - npmLog.info('sharp', 'Building from source via node-gyp'); + libvips.log(`Detected globally-installed libvips v${globalLibvipsVersion}`); + libvips.log('Building from source via node-gyp'); process.exit(1); } else if (libvips.hasVendoredLibvips()) { - npmLog.info('sharp', `Using existing vendored libvips v${minimumLibvipsVersion}`); + libvips.log(`Using existing vendored libvips v${minimumLibvipsVersion}`); } else { // Is this arch/platform supported? const arch = process.env.npm_config_arch || process.arch; @@ -117,11 +116,11 @@ try { const tarFilename = ['libvips', minimumLibvipsVersion, platformAndArch].join('-') + '.tar.' + extension; const tarPathCache = path.join(libvips.cachePath(), tarFilename); if (fs.existsSync(tarPathCache)) { - npmLog.info('sharp', `Using cached ${tarPathCache}`); + libvips.log(`Using cached ${tarPathCache}`); extractTarball(tarPathCache, platformAndArch); } else { const url = distBaseUrl + tarFilename; - npmLog.info('sharp', `Downloading ${url}`); + libvips.log(`Downloading ${url}`); simpleGet({ url: url, agent: agent() }, function (err, response) { if (err) { fail(err); diff --git a/lib/libvips.js b/lib/libvips.js index 6316f96f..6ee0554b 100644 --- a/lib/libvips.js +++ b/lib/libvips.js @@ -37,6 +37,14 @@ const cachePath = function () { return libvipsCachePath; }; +const log = function (item) { + if (item instanceof Error) { + console.error(`sharp: ${item.message}`); + } else { + console.log(`sharp: ${item}`); + } +}; + const isRosetta = function () { /* istanbul ignore next */ if (process.platform === 'darwin' && process.arch === 'x64') { @@ -104,6 +112,7 @@ module.exports = { minimumLibvipsVersion, minimumLibvipsVersionLabelled, cachePath, + log, globalLibvipsVersion, hasVendoredLibvips, pkgConfigPath, diff --git a/package.json b/package.json index fd70d685..65f9ba81 100644 --- a/package.json +++ b/package.json @@ -120,7 +120,6 @@ "color": "^3.1.3", "detect-libc": "^1.0.3", "node-addon-api": "^3.1.0", - "npmlog": "^4.1.2", "prebuild-install": "^6.0.1", "semver": "^7.3.4", "simple-get": "^3.1.0", diff --git a/test/unit/libvips.js b/test/unit/libvips.js index 10f0dad0..60d56658 100644 --- a/test/unit/libvips.js +++ b/test/unit/libvips.js @@ -105,4 +105,30 @@ describe('libvips binaries', function () { assert.strictEqual(true, fs.existsSync(nestedDirPath)); }); }); + + describe('logger', function () { + const consoleLog = console.log; + const consoleError = console.error; + + after(function () { + console.log = consoleLog; + console.error = consoleError; + }); + + it('logs an info message', function (done) { + console.log = function (msg) { + assert.strictEqual(msg, 'sharp: progress'); + done(); + }; + libvips.log('progress'); + }); + + it('logs an error message', function (done) { + console.error = function (msg) { + assert.strictEqual(msg, 'sharp: problem'); + done(); + }; + libvips.log(new Error('problem')); + }); + }); });