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

feat: Support for local files #84

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
58 changes: 57 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const osFilterObj = importLazy('os-filter-obj');

const statAsync = pify(fs.stat);
const chmodAsync = pify(fs.chmod);
const lstatAsync = pify(fs.lstat);
const copyFileAsync = pify(fs.copyFile);

/**
* Initialize a new `BinWrapper`
Expand All @@ -30,6 +32,29 @@ module.exports = class BinWrapper {
}
}

/**
* Get or set local files to use before download
*
* @param {String} src
* @param {String} os
* @param {String} arch
* @api public
*/
localSrc(src, os, arch) {
if (arguments.length === 0) {
return this._localSrc;
}

this._localSrc = this._localSrc || [];
this._localSrc.push({
url: src,
os,
arch
});

return this;
}

/**
* Get or set files to download
*
Expand Down Expand Up @@ -151,13 +176,44 @@ module.exports = class BinWrapper {
findExisting() {
return statAsync(this.path()).catch(error => {
if (error && error.code === 'ENOENT') {
return this.download();
return this.copyLocal().catch(() => this.download());
}

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

/**
* Copying local files
*
* @api private
*/
copyLocal() {
const files = osFilterObj(this.localSrc() || []);
const urls = [];

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));

return Promise.all(urls.map(url => lstatAsync(url).then(stats => {
if (stats.isDirectory()) {
return Promise.reject(new Error('Local source path is a directory.'));
}
if (stats.isSymbolicLink()) {
return Promise.reject(new Error('Local source path is a symbolic link.'));
}
if (!stats.isFile()) {
return Promise.reject(new Error('Local source path is not a file.'));
}

return copyFileAsync(url, path.join(this.dest(), path.basename(url)))
.then(() => chmodAsync(path.join(this.dest(), fileName), 0o755));
})));
}

/**
* Download files
*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bin-wrapper",
"version": "4.1.0",
"version": "4.2.0",
"description": "Binary wrapper that makes your programs seamlessly available as local dependencies",
"license": "MIT",
"repository": "kevva/bin-wrapper",
Expand Down
15 changes: 14 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const BinWrapper = require('bin-wrapper');

const base = 'https://github.com/imagemin/gifsicle-bin/raw/master/vendor';
const bin = new BinWrapper()
.localSrc(`vendor/macos/gifsicle`, 'darwin')
.localSrc(`vendor/linux/x64/gifsicle`, 'linux', 'x64')
.localSrc(`vendor/win/x64/gifsicle.exe`, 'win32', 'x64')
.src(`${base}/macos/gifsicle`, 'darwin')
.src(`${base}/linux/x64/gifsicle`, 'linux', 'x64')
.src(`${base}/win/x64/gifsicle.exe`, 'win32', 'x64')
Expand Down Expand Up @@ -84,13 +87,23 @@ Type: `string`

Tie the source to a specific arch.

### .localSrc(filepath, [os], [arch])

Same as `.src()`, but adds a local source to copy, before trying any downloads.

#### filepath

Type: `string`

Accepts a filepath pointing to a local file.

### .dest(destination)

#### destination

Type: `string`

Accepts a path which the files will be downloaded to.
Accepts a path which the files will be copied/downloaded to.

### .use(binary)

Expand Down