Walking the Directory and Creating a TreeNode in Node.js
In this article, we will be exploring how to navigate or walk through a file directory in Node.js, creating a TreeNode
for each file or subdirectory we encounter. This will involve using Node’s built-in fs
(file system) and path
modules, as well as the crypto
module for hashing file contents.
First, let’s begin by creating an interface
for our file metadata:
|
|
Here, our FileMetadata
interface defines three properties: path
(the file or directory path), isDirectory
(a boolean indicating if the path is a directory), and hash
(a string representing a file hash, which will be optional since directories won’t have a hash).
Next, we create a TreeNode
class:
|
|
The TreeNode
class represents a node in our tree structure. Each node contains metadata (of type FileMetadata
) and an array of child nodes (children
). The print
method allows us to print the path of the node and its children, with indentation to represent the depth in the tree.
We’ll need a method to hash a file’s content. For this, we use Node.js’s built-in crypto
module:
|
|
The hashFile
function reads a file synchronously into a buffer, then uses crypto.createHash
to create a SHA-256 hash, which is updated with the file content buffer. The digest is then returned as a hexadecimal string.
Finally, we create the walkDir
function:
|
|
The walkDir
function synchronously gets the status of the input directory (dir
). If it’s a directory, it reads the directory’s contents and recursively walks each entry, creating a TreeNode
for each one. If the input dir
is a file, it creates a TreeNode
for the file, computing and storing the file’s hash.
With the use of these Node.js methods and our defined TreeNode
class, we can efficiently traverse a file directory, create a tree-like structure for it, and obtain the hash of each file. This is particularly useful when you need to track the state of files in a directory, for example in a version control system.
Cheers! 🍺