Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File download from mirror station path #141

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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://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')
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://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/`
}
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://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()