get-file-details.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. /*
  3. Copyright 2021 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. var __importDefault = (this && this.__importDefault) || function (mod) {
  9. return (mod && mod.__esModule) ? mod : { "default": mod };
  10. };
  11. Object.defineProperty(exports, "__esModule", { value: true });
  12. exports.getFileDetails = void 0;
  13. const glob_1 = __importDefault(require("glob"));
  14. const upath_1 = __importDefault(require("upath"));
  15. const errors_1 = require("./errors");
  16. const get_file_size_1 = require("./get-file-size");
  17. const get_file_hash_1 = require("./get-file-hash");
  18. function getFileDetails({ globDirectory, globFollow, globIgnores, globPattern, globStrict, }) {
  19. let globbedFiles;
  20. let warning = '';
  21. try {
  22. globbedFiles = glob_1.default.sync(globPattern, {
  23. cwd: globDirectory,
  24. follow: globFollow,
  25. ignore: globIgnores,
  26. strict: globStrict,
  27. });
  28. }
  29. catch (err) {
  30. throw new Error(errors_1.errors['unable-to-glob-files'] +
  31. ` '${err instanceof Error && err.message ? err.message : ''}'`);
  32. }
  33. if (globbedFiles.length === 0) {
  34. warning =
  35. errors_1.errors['useless-glob-pattern'] +
  36. ' ' +
  37. JSON.stringify({ globDirectory, globPattern, globIgnores }, null, 2);
  38. }
  39. const globbedFileDetails = [];
  40. for (const file of globbedFiles) {
  41. const fullPath = upath_1.default.join(globDirectory, file);
  42. const fileSize = (0, get_file_size_1.getFileSize)(fullPath);
  43. if (fileSize !== null) {
  44. const fileHash = (0, get_file_hash_1.getFileHash)(fullPath);
  45. globbedFileDetails.push({
  46. file: `${upath_1.default.relative(globDirectory, fullPath)}`,
  47. hash: fileHash,
  48. size: fileSize,
  49. });
  50. }
  51. }
  52. return { globbedFileDetails, warning };
  53. }
  54. exports.getFileDetails = getFileDetails;