Skip to content

Commit 59d6d8a

Browse files
authored
feat: add decompress option support (#18)
1 parent ee6812b commit 59d6d8a

6 files changed

Lines changed: 67 additions & 2 deletions

File tree

fixtures/gifsicle-linux.tar.xz

186 KB
Binary file not shown.

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import osFilterObject from '@xhmikosr/os-filter-obj';
99
* @typedef {Object} BinWrapperOptions
1010
* @property {number} [strip=1] - Number of leading paths to strip from the archive.
1111
* @property {boolean} [skipCheck=false] - Skip binary checks.
12+
* @property {object} [decompress={}] - Extra options forwarded to @xhmikosr/decompress (e.g. `{ plugins: [...] }`). The `strip` key here is ignored; use the top-level `strip` option.
1213
*/
1314

1415
/**
@@ -23,11 +24,12 @@ export default class BinWrapper {
2324
* @param {BinWrapperOptions} [options]
2425
*/
2526
constructor(options = {}) {
26-
const {strip = 1, skipCheck = false} = options;
27+
const {strip = 1, skipCheck = false, decompress = {}} = options;
2728

2829
this.options = {
2930
strip: Math.max(0, strip),
3031
skipCheck,
32+
decompress: {...decompress},
3133
};
3234
}
3335

@@ -185,6 +187,7 @@ export default class BinWrapper {
185187
downloader(url, this.dest(), {
186188
extract: true,
187189
decompress: {
190+
...this.options.decompress,
188191
strip: this.options.strip,
189192
},
190193
})));

package-lock.json

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"binary-version-check": "^6.1.0"
4848
},
4949
"devDependencies": {
50+
"@felipecrs/decompress-tarxz": "^5.0.6",
5051
"ava": "^7.0.0",
5152
"c8": "^11.0.0",
5253
"isexe": "^4.0.0",

readme.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ Whether to skip the binary check or not.
6363

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

66+
##### decompress
67+
68+
* Type: `Object`
69+
* Default: `{}`
70+
71+
Extra options forwarded to [`@xhmikosr/decompress`](https://github.com/XhmikosR/decompress) e.g. `plugins`.
72+
The `strip` key is ignored here; use the top-level `strip` option instead.
73+
6674
### .src(url, [os], [arch])
6775

6876
Adds a source to download.

test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {isexe} from 'isexe';
66
import nock from 'nock';
77
import {temporaryDirectory} from 'tempy';
88
import test from 'ava';
9+
import decompressTarxz from '@felipecrs/decompress-tarxz';
910
import BinWrapper from './index.js';
1011

1112
const __filename = fileURLToPath(import.meta.url);
@@ -35,7 +36,9 @@ test.beforeEach(() => {
3536
.get('/gifsicle-win32.tar.gz')
3637
.replyWithFile(200, fixture('gifsicle-win32.tar.gz'))
3738
.get('/test.js')
38-
.replyWithFile(200, __filename);
39+
.replyWithFile(200, __filename)
40+
.get('/gifsicle-linux.tar.xz')
41+
.replyWithFile(200, fixture('gifsicle-linux.tar.xz'));
3942
});
4043

4144
test('expose a constructor', t => {
@@ -202,6 +205,30 @@ test('downloaded files are set to be executable', async t => {
202205
}
203206
});
204207

208+
test('use custom decompress plugins', async t => {
209+
const temporaryDir = temporaryDirectory();
210+
const bin = new BinWrapper({skipCheck: true, decompress: {plugins: [decompressTarxz()]}})
211+
.src('http://foo.com/gifsicle-linux.tar.xz')
212+
.dest(temporaryDir)
213+
.use('gifsicle');
214+
215+
await bin.run();
216+
t.true(await pathExists(bin.path()));
217+
await removeDir(temporaryDir);
218+
});
219+
220+
test('forward decompress options to the downloader', async t => {
221+
const temporaryDir = temporaryDirectory();
222+
const bin = new BinWrapper({skipCheck: true, decompress: {filter: () => false}})
223+
.src('http://foo.com/gifsicle.tar.gz')
224+
.dest(temporaryDir)
225+
.use(binary);
226+
227+
await bin.run();
228+
t.false(await pathExists(bin.path()));
229+
await removeDir(temporaryDir);
230+
});
231+
205232
test('tolerate a non-archive file saved under a different name than the URL', async t => {
206233
const temporaryDir = temporaryDirectory();
207234

0 commit comments

Comments
 (0)