get-file-details.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 path = require('path');
  10. const errors = require('./errors');
  11. const getFileSize = require('./get-file-size');
  12. const getFileHash = require('./get-file-hash');
  13. module.exports = globOptions => {
  14. const globDirectory = globOptions.globDirectory,
  15. globFollow = globOptions.globFollow,
  16. globIgnores = globOptions.globIgnores,
  17. globPattern = globOptions.globPattern,
  18. globStrict = globOptions.globStrict;
  19. let globbedFiles;
  20. try {
  21. globbedFiles = glob.sync(globPattern, {
  22. cwd: globDirectory,
  23. follow: globFollow,
  24. ignore: globIgnores,
  25. strict: globStrict
  26. });
  27. } catch (err) {
  28. throw new Error(errors['unable-to-glob-files'] + ` '${err.message}'`);
  29. }
  30. if (globbedFiles.length === 0) {
  31. throw new Error(errors['useless-glob-pattern'] + ' ' + JSON.stringify({
  32. globDirectory,
  33. globPattern,
  34. globIgnores
  35. }, null, 2));
  36. }
  37. const fileDetails = globbedFiles.map(file => {
  38. const fullPath = path.join(globDirectory, file);
  39. const fileSize = getFileSize(fullPath);
  40. if (fileSize === null) {
  41. return null;
  42. }
  43. const fileHash = getFileHash(fullPath);
  44. return {
  45. file: `${path.relative(globDirectory, fullPath)}`,
  46. hash: fileHash,
  47. size: fileSize
  48. };
  49. }); // If !== null, means it's a valid file.
  50. return fileDetails.filter(details => details !== null);
  51. };