file.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. var fs = require('fs');
  2. var path = require('path');
  3. var glob = require('glob');
  4. var mkdirp = require('mkdirp');
  5. var file = module.exports;
  6. /**
  7. * Performs a glob search with the provided pattern and optional Hash of
  8. * options. Options can be any option supported by
  9. * [glob](https://github.com/isaacs/node-glob#options)
  10. *
  11. * @param {String} pattern
  12. * @param {Object} options
  13. */
  14. file.expand = function expand(pattern, options) {
  15. return glob.sync(pattern, options);
  16. };
  17. /**
  18. * Performs a glob search with the provided pattern and optional Hash of
  19. * options, filtering results to only return files (not directories). Options
  20. * can be any option supported by
  21. * [glob](https://github.com/isaacs/node-glob#options)
  22. *
  23. * @param {String} pattern
  24. * @param {Object} options
  25. */
  26. file.expandFiles = function expandFiles(pattern, options) {
  27. options = options || {};
  28. var cwd = options.cwd || process.cwd();
  29. return this.expand(pattern, options).filter(function (filepath) {
  30. return fs.statSync(path.join(cwd, filepath)).isFile();
  31. });
  32. };
  33. /**
  34. * Checks a given file path being absolute or not.
  35. * Borrowed from grunt's file API.
  36. */
  37. file.isPathAbsolute = function () {
  38. var filepath = path.join.apply(path, arguments);
  39. return path.resolve(filepath) === filepath;
  40. };
  41. file.mkdir = mkdirp.sync.bind(mkdirp);