index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toTreeSync = void 0;
  4. const tree_dump_1 = require("tree-dump");
  5. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  6. const toTreeSync = (fs, opts = {}) => {
  7. const separator = opts.separator || '/';
  8. let dir = opts.dir || separator;
  9. if (dir[dir.length - 1] !== separator)
  10. dir += separator;
  11. const tab = opts.tab || '';
  12. const depth = opts.depth ?? 10;
  13. const sort = opts.sort ?? true;
  14. let subtree = ' (...)';
  15. if (depth > 0) {
  16. const list = fs.readdirSync(dir, { withFileTypes: true });
  17. if (sort) {
  18. list.sort((a, b) => {
  19. if (a.isDirectory() && b.isDirectory()) {
  20. return a.name.toString().localeCompare(b.name.toString());
  21. }
  22. else if (a.isDirectory()) {
  23. return -1;
  24. }
  25. else if (b.isDirectory()) {
  26. return 1;
  27. }
  28. else {
  29. return a.name.toString().localeCompare(b.name.toString());
  30. }
  31. });
  32. }
  33. subtree = (0, tree_dump_1.printTree)(tab, list.map(entry => tab => {
  34. if (entry.isDirectory()) {
  35. return (0, exports.toTreeSync)(fs, { dir: dir + entry.name, depth: depth - 1, tab });
  36. }
  37. else if (entry.isSymbolicLink()) {
  38. return '' + entry.name + ' → ' + fs.readlinkSync(dir + entry.name);
  39. }
  40. else {
  41. return '' + entry.name;
  42. }
  43. }));
  44. }
  45. const base = (0, fs_node_utils_1.basename)(dir, separator) + separator;
  46. return base + subtree;
  47. };
  48. exports.toTreeSync = toTreeSync;
  49. //# sourceMappingURL=index.js.map