constatic
Version:
Constatic is a CLI for creating and managing modern TypeScript projects, providing an organized structure and features that streamline development.
51 lines (50 loc) • 1.49 kB
JavaScript
// src/helpers/format.ts
import ck from "chalk";
function buildFileTree(paths) {
const root = [];
for (const path of paths) {
const parts = path.split("/");
let currentLevel = root;
for (const part of parts) {
let existing = currentLevel.find((node) => node.name === part);
if (!existing) {
existing = { name: part, children: [] };
currentLevel.push(existing);
}
currentLevel = existing.children;
}
}
return root;
}
function formatFileTree(nodes, prefix = "") {
let result = "";
nodes.forEach((node, index) => {
const isLastNode = index === nodes.length - 1;
const branch = isLastNode ? "└─ " : "├─ ";
const isDirectory = node.children && node.children.length > 0;
const nodeName = isDirectory ? `${node.name}/` : ck.yellowBright(node.name);
result += `${prefix}${branch}${nodeName}
`;
if (isDirectory) {
const newPrefix = prefix + (isLastNode ? " " : "│ ");
result += formatFileTree(node.children, newPrefix);
}
});
return result;
}
function buildAndFormatTree(paths) {
const tree = buildFileTree(paths);
return formatFileTree(tree);
}
function printRecordTree(title, deps) {
console.log(title);
const entries = Object.entries(deps);
entries.forEach(([key, value], index) => {
const prefix = index === entries.length - 1 ? "└─" : "├─";
console.log(`${prefix} ${key}@${value}`);
});
}
export {
printRecordTree,
buildAndFormatTree
};