glob-async.js 717 B

12345678910111213141516171819202122232425262728293031323334
  1. 'use strict';
  2. /**
  3. * Dependencies
  4. */
  5. const glob = require('glob');
  6. /**
  7. * Async wrapper for glob
  8. */
  9. module.exports = function globAsync(pattern, ignore, allowEmptyPaths, cfg) {
  10. //Prepare glob config
  11. cfg = Object.assign({ignore}, cfg, {nodir: true});
  12. //Return promise
  13. return new Promise((resolve, reject) => {
  14. glob(pattern, cfg, (error, files) => {
  15. //istanbul ignore if: hard to make glob error
  16. if (error) {
  17. return reject(error);
  18. }
  19. //Error if no files match, unless allowed
  20. if (!allowEmptyPaths && files.length === 0) {
  21. return reject(new Error('No files match the pattern: ' + pattern));
  22. }
  23. //Resolve
  24. resolve(files);
  25. });
  26. });
  27. };