blob: 525968ac01e0d2dafab97be83889e54b505e35c2 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict';
2const decompress = require('decompress');
3const download = require('download');
4const execa = require('execa');
5const pMapSeries = require('p-map-series');
6const tempfile = require('tempfile');
7
8const exec = (cmd, cwd) => pMapSeries(cmd, x => execa.shell(x, {cwd}));
9
10exports.directory = (dir, cmd) => {
11 if (typeof dir !== 'string') {
12 return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof dir}\``));
13 }
14
15 return exec(cmd, dir);
16};
17
18exports.file = (file, cmd, opts) => {
19 opts = Object.assign({strip: 1}, opts);
20
21 if (typeof file !== 'string') {
22 return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof file}\``));
23 }
24
25 const tmp = tempfile();
26
27 return decompress(file, tmp, opts).then(() => exec(cmd, tmp));
28};
29
30exports.url = (url, cmd, opts) => {
31 opts = Object.assign({
32 extract: true,
33 strip: 1
34 }, opts);
35
36 if (typeof url !== 'string') {
37 return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof url}\``));
38 }
39
40 const tmp = tempfile();
41
42 return download(url, tmp, opts).then(() => exec(cmd, tmp));
43};