web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.5 KiB

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