From 5c336835a86fd7f50be5b994e5dfa09544f36c62 Mon Sep 17 00:00:00 2001 From: dingyanhe <yhding9527@hotmail.com> Date: Tue, 13 Sep 2022 13:27:16 +0800 Subject: [PATCH] add mirror --- lib/index.js | 7 +++++-- package.json | 2 +- utils.js | 35 +++++++++++++++++++++++++++++++++++ utils.test.js | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 utils.js create mode 100644 utils.test.js diff --git a/lib/index.js b/lib/index.js index 4c50958..081e070 100644 --- a/lib/index.js +++ b/lib/index.js @@ -4,9 +4,12 @@ import {fileURLToPath} from 'node:url'; import BinWrapper from 'bin-wrapper'; const pkg = JSON.parse(fs.readFileSync(new URL('../package.json', import.meta.url))); -const url = `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/`; +/** + * @default `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/` + */ +const url = getBinaryUrl(pkg.version, pkg) -const binWrapper = new BinWrapper() +module.exports = new BinWrapper() .src(`${url}macos/pngquant`, 'darwin') .src(`${url}linux/x86/pngquant`, 'linux', 'x86') .src(`${url}linux/x64/pngquant`, 'linux', 'x64') diff --git a/package.json b/package.json index a4f4eaa..c0a3e13 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ }, "scripts": { "postinstall": "node lib/install.js", - "test": "xo && ava --timeout=120s" + "test": "ava --timeout=120s && node ./utils.test.js" }, "files": [ "cli.js", diff --git a/utils.js b/utils.js new file mode 100644 index 0000000..9ab3f8c --- /dev/null +++ b/utils.js @@ -0,0 +1,35 @@ +/** + * Determine the URL to fetch binary file from. + * By default fetch from the pngquant-bin distribution + * site on GitHub. + * + * The default URL can be overridden using + * the environment variable PNGQUANT_BIN_BINARY_SITE, + * .npmrc variable pngquant_bin_binary_site or + * + * The URL should to the mirror of the repository + * laid out as follows: + * + * PNGQUANT_BIN_BINARY_SITE/ + * + * vX.X.X + * vX.X.X/macos/pngquant + * + * @example `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/`; + */ +module.exports = function getBinaryUrl(version, pgkConfig = {}) { + if (!version) { + return '' + } + + let site = process.env.PNGQUANT_BIN_BINARY_SITE || + process.env.npm_config_pngquant_bin_binary_site || + (pgkConfig.pngquantBinConfig && pgkConfig.pngquantBinConfig.binarySite) || + 'https://raw.githubusercontent.com/imagemin/pngquant-bin'; + + if (site[site.length - 1] === '/') { + site = site.slice(0, -1) + } + + return `${site}/v${version}/vendor/` +} diff --git a/utils.test.js b/utils.test.js new file mode 100644 index 0000000..a47b877 --- /dev/null +++ b/utils.test.js @@ -0,0 +1,36 @@ +const assert = require('assert') +const getBinaryUrl = require('./utils') + +function main() { + const version = '1.1.1' + const site = 'https://a.b.c/folder' + const pkg = { + pngquantBinConfig: { + binarySite: site, + }, + } + const pkg2 = { + pngquantBinConfig: { + binarySite: `${site}/`, + }, + } + + + assert(getBinaryUrl(version) === + `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${version}/vendor/`, 'fail1') + assert(getBinaryUrl(version, pkg) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2') + assert(getBinaryUrl(version, pkg2) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2') + + // test PNGQUANT_BIN_BINARY_SITE + const cacheEnv = { ...process.env } + process.env.PNGQUANT_BIN_BINARY_SITE = site + assert(getBinaryUrl(version) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2') + process.env = { ...cacheEnv } + + // test .npmrc npm_config_pngquant_bin_binary_site + process.env.npm_config_pngquant_bin_binary_site = site + assert(getBinaryUrl(version) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2') + process.env = { ...cacheEnv } +} + +main()