Skip to content

Commit

Permalink
add mirror
Browse files Browse the repository at this point in the history
  • Loading branch information
dingyanhe committed Sep 14, 2022
1 parent 36d5808 commit 5c33683
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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://github.com/raw/imagemin/pngquant-bin/v${pkg.version}/vendor/`;
/**
* @default `https://github.com/raw/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')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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 5c33683

Please sign in to comment.