Skip to content

Ability to specify base url #73

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

Open
wants to merge 3 commits into
base: master
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
61 changes: 58 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,42 @@ module.exports = class BinWrapper {
}
}

/**
* Set base URL
*
* @param {String} url
* @api public
*/
baseUrl(url) {
if (arguments.length === 0) {
return this._baseUrl;
}

this._baseUrl = url;

return this;
}

/**
* Set the env var that overrides baseUrl
*
* @param {String} envName
* @api public
*/
baseUrlOverrideEnvName(envName) {
if (arguments.length === 0) {
return this._baseUrlOverrideEnvName;
}

if (!this._baseUrl) {
throw new Error('Need to configure baseUrl before configuring overriding it.');
}

this._baseUrlOverrideEnvName = envName;

return this;
}

/**
* Get or set files to download
*
Expand Down Expand Up @@ -138,8 +174,6 @@ module.exports = class BinWrapper {
if (this.version()) {
return binVersionCheck(this.path(), this.version());
}

return Promise.resolve();
});
}

Expand Down Expand Up @@ -171,7 +205,10 @@ module.exports = class BinWrapper {
return Promise.reject(new Error('No binary found matching your system. It\'s probably not supported.'));
}

files.forEach(file => urls.push(file.url));
const baseUrl = this.buildDownloadBaseUrl();
files.forEach(file => {
urls.push(baseUrl + file.url);
});

return Promise.all(urls.map(url => download(url, this.dest(), {
extract: true,
Expand All @@ -182,6 +219,7 @@ module.exports = class BinWrapper {
return item.map(file => file.path);
}

// eslint-disable-next-line node/no-deprecated-api
const parsedUrl = url.parse(files[index].url);
const parsedPath = path.parse(parsedUrl.pathname);

Expand All @@ -193,6 +231,23 @@ module.exports = class BinWrapper {
}));
});
}

/**
* Get the base url we fetch binaries from
*
* @api private
*/
buildDownloadBaseUrl() {
if (!this._baseUrl) {
return '';
}

if (this._baseUrlOverrideEnvName && process.env[this._baseUrlOverrideEnvName]) {
return process.env[this._baseUrlOverrideEnvName];
}

return this._baseUrl;
}
};

function flatten(arr) {
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"node": ">=6"
},
"scripts": {
"test": "xo && ava"
"test": "xo && ava",
"ba": "node --version"
},
"files": [
"index.js"
Expand All @@ -33,7 +34,7 @@
"pify": "^4.0.1"
},
"devDependencies": {
"ava": "*",
"ava": "1.4.1",
"executable": "^4.1.1",
"nock": "^10.0.2",
"path-exists": "^3.0.0",
Expand Down
51 changes: 51 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,57 @@ Default: `1`

Strip a number of leading paths from file names on extraction.

### .baseUrl(baseUrl)

Accepts a base URL that is prepended to everything added as a src using [.src](#.src(url, [os], [arch])).

#### baseUrl

Type: `string`

Accepts a URL pointing to use as the base URL.

#### Usage

```js
const BinWrapper = require('bin-wrapper');

const bin = new BinWrapper()
.baseUrl('https://github.com/imagemin/gifsicle-bin/raw/master/vendor')
.src(`/linux/x64/gifsicle`, 'linux', 'x64')
.dest(path.join('vendor'))
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
```

### .baseUrlOverrideEnvName(envName)

Accepts an enviroment variable name to look for. When set the value of this enviroment varibale overrides the value set using [.baseUrl](#.baseUrl(baseUrl)).

#### envName

Type: `string`

Accepts a enviroment variable name.

#### Usage

The below will download the binary from `http://example.com/private/mirror/linux/x64/gifsicle`

```sh
export GIFSICLE_BIN__MIRROR="http://example.com/private/mirror
```

```js
const BinWrapper = require('bin-wrapper');

const bin = new BinWrapper()
.baseUrl('https://github.com/imagemin/gifsicle-bin/raw/master/vendor')
.baseUrlOverrideEnvName('GIFSICLE_BIN__MIRROR')
.src(`/linux/x64/gifsicle`, 'linux', 'x64')
.dest(path.join('vendor'))
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle')
```

### .src(url, [os], [arch])

Adds a source to download.
Expand Down
57 changes: 56 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ test('expose a constructor', t => {
t.is(typeof Fn, 'function');
});

test('set the base url', t => {
const bin = new Fn().baseUrl('http://foo.com/');
t.is(bin._baseUrl, 'http://foo.com/');
});

test('set the base url environment variable override name', t => {
const bin = new Fn()
.baseUrl('http://foo.com')
.baseUrlOverrideEnvName('GIFSICLE_BINARY_MIRROR');
t.is(bin._baseUrlOverrideEnvName, 'GIFSICLE_BINARY_MIRROR');
});

test('can not set the base url env override as base url is not set', t => {
t.throws(() => new Fn().baseUrlOverrideEnvName('GIFSICLE_BINARY_MIRROR'));
});

test('add a source', t => {
const bin = new Fn().src('http://foo.com/bar.tar.gz');
t.is(bin._src[0].url, 'http://foo.com/bar.tar.gz');
Expand Down Expand Up @@ -72,6 +88,45 @@ test('verify that a binary is working', async t => {
await rimrafP(bin.dest());
});

test('verify that a binary is working with baseUrl', async t => {
const bin = new Fn()
.baseUrl('http://foo.com/')
.src('gifsicle.tar.gz')
.dest(tempy.directory())
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');

await bin.run();
t.true(await pathExists(bin.path()));
await rimrafP(bin.dest());
});

test('verify that a binary is working with baseUrl environment variable override', async t => {
process.env.GIFSICLE_BINARY_MIRROR = 'http://foo.com/';
const bin = new Fn()
.baseUrl('http://bar.com/')
.baseUrlOverrideEnvName('GIFSICLE_BINARY_MIRROR')
.src('gifsicle.tar.gz')
.dest(tempy.directory())
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');

await bin.run();
t.true(await pathExists(bin.path()));
await rimrafP(bin.dest());
});

test('verify that a binary is working with baseUrl environment variable override undefined', async t => {
const bin = new Fn()
.baseUrl('http://foo.com/')
.baseUrlOverrideEnvName('GIFSICLE_BINARY_MIRROR')
.src('gifsicle.tar.gz')
.dest(tempy.directory())
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');

await bin.run();
t.true(await pathExists(bin.path()));
await rimrafP(bin.dest());
});

test('meet the desired version', async t => {
const bin = new Fn()
.src('http://foo.com/gifsicle.tar.gz')
Expand Down Expand Up @@ -119,7 +174,7 @@ test('error if no binary is found and no source is provided', async t => {
.dest(tempy.directory())
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');

await t.throws(bin.run(), 'No binary found matching your system. It\'s probably not supported.');
await t.throwsAsync(() => bin.run(), 'No binary found matching your system. It\'s probably not supported.');
});

test('downloaded files are set to be executable', async t => {
Expand Down