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

Update all the stuff #80

Open
wants to merge 2 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
35 changes: 35 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: CI

on:
push:
pull_request:
workflow_dispatch:

env:
FORCE_COLOR: 2

jobs:
test:
name: Node ${{ matrix.node }} on ${{ matrix.os }}
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
node: [12, 14, 16, 18]
os: [ubuntu-latest, windows-latest]

steps:
- name: Clone repository
uses: actions/checkout@v3

- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}

- name: Install npm dependencies
run: npm install

- name: Run tests
run: npm run test-ci
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
yarn.lock
/coverage
5 changes: 0 additions & 5 deletions .travis.yml

This file was deleted.

62 changes: 20 additions & 42 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,17 @@
'use strict';
const fs = require('fs');
const path = require('path');
const url = require('url');
const pify = require('pify');
const importLazy = require('import-lazy')(require);

const binCheck = importLazy('bin-check');
const binVersionCheck = importLazy('bin-version-check');
const download = importLazy('download');
const osFilterObj = importLazy('os-filter-obj');

const statAsync = pify(fs.stat);
const chmodAsync = pify(fs.chmod);
import {promises as fs} from 'node:fs';
import path from 'node:path';
import binCheck from 'bin-check';
import binVersionCheck from 'bin-version-check';
import download from 'download';
import osFilterObject from 'os-filter-obj';

/**
* Initialize a new `BinWrapper`
*
* @param {Object} options
* @api public
*/
module.exports = class BinWrapper {
export default class BinWrapper {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index.js

constructor(options = {}) {
this.options = options;

Expand Down Expand Up @@ -47,7 +39,7 @@ module.exports = class BinWrapper {
this._src.push({
url: src,
os,
arch
arch,
});

return this;
Expand Down Expand Up @@ -138,8 +130,6 @@ module.exports = class BinWrapper {
if (this.version()) {
return binVersionCheck(this.path(), this.version());
}

return Promise.resolve();
});
}

Expand All @@ -149,12 +139,12 @@ module.exports = class BinWrapper {
* @api private
*/
findExisting() {
return statAsync(this.path()).catch(error => {
return fs.stat(this.path()).catch(error => {
if (error && error.code === 'ENOENT') {
return this.download();
}

return Promise.reject(error);
throw error;
});
}

Expand All @@ -164,45 +154,33 @@ module.exports = class BinWrapper {
* @api private
*/
download() {
const files = osFilterObj(this.src() || []);
const urls = [];
const files = osFilterObject(this.src() || []);

if (files.length === 0) {
return Promise.reject(new Error('No binary found matching your system. It\'s probably not supported.'));
}

files.forEach(file => urls.push(file.url));
const urls = [];
for (const file of files) {
urls.push(file.url);
}

return Promise.all(urls.map(url => download(url, this.dest(), {
extract: true,
strip: this.options.strip
strip: this.options.strip,
}))).then(result => {
const resultingFiles = flatten(result.map((item, index) => {
const resultingFiles = result.flatMap((item, index) => {
Copy link
Contributor Author

@XhmikosR XhmikosR Jan 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched this to flatMap and removed the custom flatten function, but I could use a confirmation.

if (Array.isArray(item)) {
return item.map(file => file.path);
}

const parsedUrl = url.parse(files[index].url);
const parsedUrl = new URL(files[index].url);
const parsedPath = path.parse(parsedUrl.pathname);

return parsedPath.base;
}));
});

return Promise.all(resultingFiles.map(fileName => {
return chmodAsync(path.join(this.dest(), fileName), 0o755);
}));
return Promise.all(resultingFiles.map(fileName => fs.chmod(path.join(this.dest(), fileName), 0o755)));
});
}
};

function flatten(arr) {
return arr.reduce((acc, elem) => {
if (Array.isArray(elem)) {
acc.push(...elem);
} else {
acc.push(elem);
}

return acc;
}, []);
}
39 changes: 26 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,18 @@
"url": "https://github.com/kevva"
},
"engines": {
"node": ">=6"
"node": "^12.20.0 || ^14.14.0 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
"ava": "ava",
"xo": "xo",
"test": "npm run xo && npm run ava",
"test-ci": "npm run xo && c8 ava"
},
"main": "index.js",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure if main is even needed anymore

"type": "module",
"exports": {
".": "./index.js"
},
"files": [
"index.js"
Expand All @@ -24,21 +32,26 @@
"local",
"wrapper"
],
"c8": {
"reporter": [
"lcovonly",
"text"
]
},
"dependencies": {
"bin-check": "^4.1.0",
"bin-version-check": "^4.0.0",
"download": "^7.1.0",
"import-lazy": "^3.1.0",
"os-filter-obj": "^2.0.0",
"pify": "^4.0.1"
"bin-version-check": "^5.0.0",
"download": "^8.0.0",
"os-filter-obj": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"ava": "^4.3.0",
"c8": "^7.11.3",
"executable": "^4.1.1",
"nock": "^10.0.2",
"path-exists": "^3.0.0",
"rimraf": "^2.6.2",
"tempy": "^0.2.1",
"xo": "*"
"nock": "^13.2.6",
"path-exists": "^5.0.0",
"rimraf": "^3.0.2",
"tempy": "^2.0.0",
"xo": "^0.49.0"
}
}
25 changes: 13 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
# bin-wrapper [![Build Status](https://travis-ci.org/kevva/bin-wrapper.svg?branch=master)](https://travis-ci.org/kevva/bin-wrapper)
# bin-wrapper [![CI](https://github.com/kevva/bin-wrapper/actions/workflows/ci.yml/badge.svg)](https://github.com/kevva/bin-wrapper/actions/workflows/ci.yml)

> Binary wrapper that makes your programs seamlessly available as local dependencies


## Install

```
$ npm install bin-wrapper
```sh
npm install bin-wrapper
```


## Usage

```js
const BinWrapper = require('bin-wrapper');
import path from 'node:path';
import BinWrapper from 'bin-wrapper';

const base = 'https://github.com/imagemin/gifsicle-bin/raw/master/vendor';
const base = 'https://github.com/imagemin/gifsicle-bin/raw/main/vendor';
const bin = new BinWrapper()
.src(`${base}/macos/gifsicle`, 'darwin')
.src(`${base}/linux/x64/gifsicle`, 'linux', 'x64')
Expand Down Expand Up @@ -50,15 +51,15 @@ Type: `Object`

##### skipCheck

Type: `boolean`<br>
Default: `false`
* Type: `boolean`
* Default: `false`

Whether to skip the binary check or not.

##### strip

Type: `number`<br>
Default: `1`
* Type: `number`
* Default: `1`

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

Expand Down Expand Up @@ -110,7 +111,7 @@ Returns the full path to your binary.

Type: `string`

Define a [semver range](https://github.com/isaacs/node-semver#ranges) to check
Define a [semver range](https://github.com/npm/node-semver#ranges) to check
the binary against.

### .run([arguments])
Expand All @@ -120,8 +121,8 @@ using the URL provided in `.src()`.

#### arguments

Type: `Array`<br>
Default: `['--version']`
* Type: `Array`
* Default: `['--version']`

Command to run the binary with. If it exits with code `0` it means that the
binary is working.
Expand Down
Loading