| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | const decompress = require('decompress'); |
| 3 | const download = require('download'); |
| 4 | const execa = require('execa'); |
| 5 | const pMapSeries = require('p-map-series'); |
| 6 | const tempfile = require('tempfile'); |
| 7 | |
| 8 | const exec = (cmd, cwd) => pMapSeries(cmd, x => execa.shell(x, {cwd})); |
| 9 | |
| 10 | exports.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 | |
| 18 | exports.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 | |
| 30 | exports.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 | }; |