Skip to content

Commit 5eeff10

Browse files
committed
add mirror
1 parent 8231b7e commit 5eeff10

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

lib/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
const path = require('path');
33
const BinWrapper = require('bin-wrapper');
44
const pkg = require('../package.json');
5+
const getBinaryUrl = require('../utils');
56

6-
const url = `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/`;
7+
const url = getBinaryUrl(pkg.version, pkg)
8+
// `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/`;
79

810
module.exports = new BinWrapper()
911
.src(`${url}macos/pngquant`, 'darwin')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"scripts": {
3030
"postinstall": "node lib/install.js",
31-
"test": "xo && ava --timeout=120s"
31+
"test": "ava --timeout=120s && node ./utils.test.js"
3232
},
3333
"files": [
3434
"cli.js",

utils.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Determine the URL to fetch binary file from.
3+
* By default fetch from the pngquant-bin distribution
4+
* site on GitHub.
5+
*
6+
* The default URL can be overridden using
7+
* the environment variable PNGQUANT_BIN_BINARY_SITE,
8+
* .npmrc variable pngquant_bin_binary_site or
9+
*
10+
* The URL should to the mirror of the repository
11+
* laid out as follows:
12+
*
13+
* PNGQUANT_BIN_BINARY_SITE/
14+
*
15+
* vX.X.X
16+
* vX.X.X/macos/pngquant
17+
*
18+
* @example `https://raw.githubusercontent.com/imagemin/pngquant-bin/v${pkg.version}/vendor/`;
19+
*/
20+
module.exports = function getBinaryUrl(version, pgkConfig = {}) {
21+
if (!version) {
22+
return ''
23+
}
24+
25+
let site = process.env.PNGQUANT_BIN_BINARY_SITE ||
26+
process.env.npm_config_pngquant_bin_binary_site ||
27+
(pgkConfig.pngquantBinConfig && pgkConfig.pngquantBinConfig.binarySite) ||
28+
'https://raw.githubusercontent.com/imagemin/pngquant-bin';
29+
30+
if (site[site.length - 1] === '/') {
31+
site = site.slice(0, -1)
32+
}
33+
34+
return `${site}/v${version}/vendor/`
35+
}

utils.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const assert = require('assert')
2+
const getBinaryUrl = require('./utils')
3+
4+
function main() {
5+
const version = '1.1.1'
6+
const site = 'https://a.b.c/folder'
7+
const pkg = {
8+
pngquantBinConfig: {
9+
binarySite: site,
10+
},
11+
}
12+
const pkg2 = {
13+
pngquantBinConfig: {
14+
binarySite: `${site}/`,
15+
},
16+
}
17+
18+
19+
assert(getBinaryUrl(version) ===
20+
`https://raw.githubusercontent.com/imagemin/pngquant-bin/v${version}/vendor/`, 'fail1')
21+
assert(getBinaryUrl(version, pkg) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2')
22+
assert(getBinaryUrl(version, pkg2) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2')
23+
24+
// test PNGQUANT_BIN_BINARY_SITE
25+
const cacheEnv = { ...process.env }
26+
process.env.PNGQUANT_BIN_BINARY_SITE = site
27+
assert(getBinaryUrl(version) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2')
28+
process.env = { ...cacheEnv }
29+
30+
// test .npmrc npm_config_pngquant_bin_binary_site
31+
process.env.npm_config_pngquant_bin_binary_site = site
32+
assert(getBinaryUrl(version) === `https://a.b.c/folder/v${version}/vendor/`, 'fail2')
33+
process.env = { ...cacheEnv }
34+
}
35+
36+
main()

0 commit comments

Comments
 (0)