@n0safe/indirectus
Version:
Directus Tools CLI.
128 lines • 4.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fsDeepTree = exports.Tree = void 0;
const fs = __importStar(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
class Tree {
constructor() { }
branches = new Map();
traverse(cb, parent = this, path = []) {
this.branches.forEach((branch, key) => {
cb(branch, [...path, key.toString()], true, parent ?? this);
if (branch instanceof Tree) {
branch.traverse(cb, parent, [...path, key]);
}
});
}
forEach(cb, path = []) {
this.traverse((branch, path) => {
if (!(branch instanceof Tree)) {
cb(branch, path, true, this);
}
}, this);
}
map(cb, path = []) {
const tree = new Tree();
this.branches.forEach((branch, key) => {
if (branch instanceof Tree) {
tree.branches.set(key, branch.map(cb));
}
else {
tree.branches.set(key, cb(branch, [...path, key], true, this));
}
});
return tree;
}
filter(cb, path = []) {
const tree = new Tree();
this.branches.forEach((branch, key) => {
if (branch instanceof Tree) {
const filtered = branch.filter(cb, [...path, key]);
if (filtered.branches.size) {
tree.branches.set(key, filtered);
}
}
else {
if (cb(branch, [...path, key], true, this)) {
tree.branches.set(key, branch);
}
}
});
return tree;
}
reduce(cb, acc) {
let result = acc;
this.forEach((value, path, isEnd, tree) => {
result = cb(result, value, path, isEnd, tree);
});
return result;
}
toArrayWithMeta() {
const arr = [];
this.forEach((value, path, isEnd) => {
arr.push({ value, path, isEnd });
});
return arr;
}
toArray() {
const arr = [];
this.forEach((value) => {
arr.push(value);
});
return arr;
}
}
exports.Tree = Tree;
const fsDeepTree = (dir) => {
const files = fs.readdirSync(dir);
const tree = new Tree();
for (const file of files) {
const fullPath = node_path_1.default.join(dir, file);
const stats = fs.statSync(fullPath);
if (stats.isDirectory()) {
tree.branches.set(file, (0, exports.fsDeepTree)(fullPath));
}
else if (stats.isFile()) {
tree.branches.set(file, fullPath);
}
}
return tree;
};
exports.fsDeepTree = fsDeepTree;
//# sourceMappingURL=tree.js.map