You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 1.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. const exec = (cmd, cwd) => pMapSeries(cmd, x => execa.shell(x, {cwd}));
  8. exports.directory = (dir, cmd) => {
  9. if (typeof dir !== 'string') {
  10. return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof dir}\``));
  11. }
  12. return exec(cmd, dir);
  13. };
  14. exports.file = (file, cmd, opts) => {
  15. opts = Object.assign({strip: 1}, opts);
  16. if (typeof file !== 'string') {
  17. return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof file}\``));
  18. }
  19. const tmp = tempfile();
  20. return decompress(file, tmp, opts).then(() => exec(cmd, tmp));
  21. };
  22. exports.url = (url, cmd, opts) => {
  23. opts = Object.assign({
  24. extract: true,
  25. strip: 1
  26. }, opts);
  27. if (typeof url !== 'string') {
  28. return Promise.reject(new TypeError(`Expected a \`string\`, got \`${typeof url}\``));
  29. }
  30. const tmp = tempfile();
  31. return download(url, tmp, opts).then(() => exec(cmd, tmp));
  32. };