Skip to content

Commit

Permalink
refactor: create dirTree.js
Browse files Browse the repository at this point in the history
  • Loading branch information
lumirlumir committed Sep 16, 2024
1 parent 479910f commit 2b9f5a3
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/utils/dirTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { promises as fs } from 'fs';
import { join } from 'path';

/**
* @typedef {object} DirTreeNode
*
* @property {string} name The name of the node.
* @property {Array<DirTreeNode>} [children] The array of child nodes if the node is a directory. This is a recursive structure.
*/

/**
* Asynchronously retrieves the directory tree structure.
*
* @async
* @param {string} dirPath The path of the directory.
* @returns {Promise<Array<DirTreeNode>>} A promise that resolves to an array of `DirTreeNode`.
*
* @example
* // Get the directory tree structure
* const dirTree = await getDirTree('/path/to/dir');
* console.log(dirTree);
*/
export async function getDirTree(dirPath) {
// `readdir` automatically throws an error when `dirPath` is not a directory.
const dirents = await fs.readdir(dirPath, { withFileTypes: true });

return await Promise.all(
dirents.map(async dirent => ({
name: dirent.name,
...(dirent.isDirectory()
? { children: await getDirTree(join(dirPath, dirent.name)) }
: {}),
})),
);
}

/**
* Checks if a `DirTreeNode` is a directory.
*
* @param {DirTreeNode} dirTreeNode The `DirTreeNode` object.
* @returns {boolean} `true` if the node is a directory. otherwise, `false`.
*
* @example
* // Check if a node is a directory
* const isDir = isDirectory({ name: 'folder', children: [...] });
* console.log(isDir); // true
*/
export function isDirectory(dirTreeNode) {
return Boolean(dirTreeNode.children);
}

0 comments on commit 2b9f5a3

Please sign in to comment.