Skip to content

Commit

Permalink
add mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyanhe committed Sep 13, 2022
1 parent 8231b7e commit 5eeff10
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
const path = require('path');
const BinWrapper = require('bin-wrapper');
const pkg = require('../package.json');
const getBinaryUrl = require('../utils');

const url = `https://github.com/raw/imagemin/pngquant-bin/v${pkg.version}/vendor/`;
const url = getBinaryUrl(pkg.version, pkg)
// `https://github.com/raw/imagemin/pngquant-bin/v${pkg.version}/vendor/`;

module.exports = new BinWrapper()
.src(`${url}macos/pngquant`, 'darwin')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"scripts": {
"postinstall": "node lib/install.js",
"test": "xo && ava --timeout=120s"
"test": "ava --timeout=120s && node ./utils.test.js"
},
"files": [
"cli.js",
Expand Down
35 changes: 35 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -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://github.com/raw/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://github.com/raw/imagemin/pngquant-bin';

if (site[site.length - 1] === '/') {
site = site.slice(0, -1)
}

return `${site}/v${version}/vendor/`
}
36 changes: 36 additions & 0 deletions utils.test.js
Original file line number Diff line number Diff line change
@@ -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://github.com/raw/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()

0 comments on commit 5eeff10

Please sign in to comment.