get-file-details.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. /*
  3. Copyright 2018 Google LLC
  4. Use of this source code is governed by an MIT-style
  5. license that can be found in the LICENSE file or at
  6. https://opensource.org/licenses/MIT.
  7. */
  8. const glob = require('glob');
  9. const upath = require('upath');
  10. const errors = require('./errors');
  11. const getFileSize = require('./get-file-size');
  12. const getFileHash = require('./get-file-hash');
  13. module.exports = ({
  14. globDirectory,
  15. globFollow,
  16. globIgnores,
  17. globPattern,
  18. globStrict
  19. }) => {
  20. let globbedFiles;
  21. let warning;
  22. try {
  23. globbedFiles = glob.sync(globPattern, {
  24. cwd: globDirectory,
  25. follow: globFollow,
  26. ignore: globIgnores,
  27. strict: globStrict
  28. });
  29. } catch (err) {
  30. throw new Error(errors['unable-to-glob-files'] + ` '${err.message}'`);
  31. }
  32. if (globbedFiles.length === 0) {
  33. warning = errors['useless-glob-pattern'] + ' ' + JSON.stringify({
  34. globDirectory,
  35. globPattern,
  36. globIgnores
  37. }, null, 2);
  38. }
  39. const fileDetails = globbedFiles.map(file => {
  40. const fullPath = upath.join(globDirectory, file);
  41. const fileSize = getFileSize(fullPath);
  42. if (fileSize === null) {
  43. return null;
  44. }
  45. const fileHash = getFileHash(fullPath);
  46. return {
  47. file: `${upath.relative(globDirectory, fullPath)}`,
  48. hash: fileHash,
  49. size: fileSize
  50. };
  51. }); // If !== null, means it's a valid file.
  52. const globbedFileDetails = fileDetails.filter(details => details !== null);
  53. return {
  54. globbedFileDetails,
  55. warning
  56. };
  57. };