Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

async.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const fsStat = require("@nodelib/fs.stat");
  4. const rpl = require("run-parallel");
  5. const constants_1 = require("../constants");
  6. const utils = require("../utils");
  7. function read(directory, settings, callback) {
  8. if (!settings.stats && constants_1.IS_SUPPORT_READDIR_WITH_FILE_TYPES) {
  9. return readdirWithFileTypes(directory, settings, callback);
  10. }
  11. return readdir(directory, settings, callback);
  12. }
  13. exports.read = read;
  14. function readdirWithFileTypes(directory, settings, callback) {
  15. settings.fs.readdir(directory, { withFileTypes: true }, (readdirError, dirents) => {
  16. if (readdirError !== null) {
  17. return callFailureCallback(callback, readdirError);
  18. }
  19. const entries = dirents.map((dirent) => ({
  20. dirent,
  21. name: dirent.name,
  22. path: `${directory}${settings.pathSegmentSeparator}${dirent.name}`
  23. }));
  24. if (!settings.followSymbolicLinks) {
  25. return callSuccessCallback(callback, entries);
  26. }
  27. const tasks = entries.map((entry) => makeRplTaskEntry(entry, settings));
  28. rpl(tasks, (rplError, rplEntries) => {
  29. if (rplError !== null) {
  30. return callFailureCallback(callback, rplError);
  31. }
  32. callSuccessCallback(callback, rplEntries);
  33. });
  34. });
  35. }
  36. exports.readdirWithFileTypes = readdirWithFileTypes;
  37. function makeRplTaskEntry(entry, settings) {
  38. return (done) => {
  39. if (!entry.dirent.isSymbolicLink()) {
  40. return done(null, entry);
  41. }
  42. settings.fs.stat(entry.path, (statError, stats) => {
  43. if (statError !== null) {
  44. if (settings.throwErrorOnBrokenSymbolicLink) {
  45. return done(statError);
  46. }
  47. return done(null, entry);
  48. }
  49. entry.dirent = utils.fs.createDirentFromStats(entry.name, stats);
  50. return done(null, entry);
  51. });
  52. };
  53. }
  54. function readdir(directory, settings, callback) {
  55. settings.fs.readdir(directory, (readdirError, names) => {
  56. if (readdirError !== null) {
  57. return callFailureCallback(callback, readdirError);
  58. }
  59. const filepaths = names.map((name) => `${directory}${settings.pathSegmentSeparator}${name}`);
  60. const tasks = filepaths.map((filepath) => {
  61. return (done) => fsStat.stat(filepath, settings.fsStatSettings, done);
  62. });
  63. rpl(tasks, (rplError, results) => {
  64. if (rplError !== null) {
  65. return callFailureCallback(callback, rplError);
  66. }
  67. const entries = [];
  68. names.forEach((name, index) => {
  69. const stats = results[index];
  70. const entry = {
  71. name,
  72. path: filepaths[index],
  73. dirent: utils.fs.createDirentFromStats(name, stats)
  74. };
  75. if (settings.stats) {
  76. entry.stats = stats;
  77. }
  78. entries.push(entry);
  79. });
  80. callSuccessCallback(callback, entries);
  81. });
  82. });
  83. }
  84. exports.readdir = readdir;
  85. function callFailureCallback(callback, error) {
  86. callback(error);
  87. }
  88. function callSuccessCallback(callback, result) {
  89. callback(null, result);
  90. }