Skip to content

Commit b068200

Browse files
realitykingsindresorhus
authored andcommitted
Set dowloaded files to be executable (#69)
This matches the behavior of bin-wrapper 3.0 Fixes #67
1 parent e4cfef2 commit b068200

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
const fs = require('fs');
33
const path = require('path');
4+
const url = require('url');
45
const pify = require('pify');
56
const importLazy = require('import-lazy')(require);
67

@@ -10,6 +11,7 @@ const download = importLazy('download');
1011
const osFilterObj = importLazy('os-filter-obj');
1112

1213
const statAsync = pify(fs.stat);
14+
const chmodAsync = pify(fs.chmod);
1315

1416
/**
1517
* Initialize a new `BinWrapper`
@@ -173,8 +175,34 @@ module.exports = class BinWrapper {
173175

174176
return Promise.all(urls.map(url => download(url, this.dest(), {
175177
extract: true,
176-
mode: '755',
177178
strip: this.options.strip
178-
})));
179+
}))).then(result => {
180+
const resultingFiles = flatten(result.map((item, index) => {
181+
if (Array.isArray(item)) {
182+
return item.map(file => file.path);
183+
}
184+
185+
const parsedUrl = url.parse(files[index].url);
186+
const parsedPath = path.parse(parsedUrl.pathname);
187+
188+
return parsedPath.base;
189+
}));
190+
191+
return Promise.all(resultingFiles.map(fileName => {
192+
return chmodAsync(path.join(this.dest(), fileName), 0o755);
193+
}));
194+
});
179195
}
180196
};
197+
198+
function flatten(arr) {
199+
return arr.reduce((acc, elem) => {
200+
if (Array.isArray(elem)) {
201+
acc.push(...elem);
202+
} else {
203+
acc.push(elem);
204+
}
205+
206+
return acc;
207+
}, []);
208+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
},
3535
"devDependencies": {
3636
"ava": "*",
37+
"executable": "^4.1.1",
3738
"nock": "^9.4.2",
3839
"path-exists": "^3.0.0",
3940
"rimraf": "^2.6.2",

test.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import pify from 'pify';
66
import rimraf from 'rimraf';
77
import test from 'ava';
88
import tempy from 'tempy';
9+
import executable from 'executable';
910
import Fn from '.';
1011

11-
const fsP = pify(fs);
1212
const rimrafP = pify(rimraf);
1313
const fixture = path.join.bind(path, __dirname, 'fixtures');
1414

@@ -93,7 +93,7 @@ test('download files even if they are not used', async t => {
9393
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
9494

9595
await bin.run();
96-
const files = await fsP.readdirSync(bin.dest());
96+
const files = fs.readdirSync(bin.dest());
9797

9898
t.is(files.length, 3);
9999
t.is(files[0], 'gifsicle');
@@ -121,3 +121,20 @@ test('error if no binary is found and no source is provided', async t => {
121121

122122
await t.throws(bin.run(), 'No binary found matching your system. It\'s probably not supported.');
123123
});
124+
125+
test('downloaded files are set to be executable', async t => {
126+
const bin = new Fn({strip: 0, skipCheck: true})
127+
.src('http://foo.com/gifsicle-darwin.tar.gz')
128+
.src('http://foo.com/gifsicle-win32.tar.gz')
129+
.src('http://foo.com/test.js')
130+
.dest(tempy.directory())
131+
.use(process.platform === 'win32' ? 'gifsicle.exe' : 'gifsicle');
132+
133+
await bin.run();
134+
135+
const files = fs.readdirSync(bin.dest());
136+
137+
files.forEach(fileName => {
138+
t.true(executable.sync(path.join(bin.dest(), fileName)));
139+
});
140+
});

0 commit comments

Comments
 (0)