Skip to content

traverse-fs/glob-traverse-fs

Repository files navigation

traverse-fs

Nodejs npm module to traverse files and folder using code, or cli, or use glob patterns

npm i traverse-fs --save

Usage

The API of traverse-fs can be used to traverse a folder or its subfolders recursively.

All the demos codes are here in the github demos folder: demos

Simple - Usage

You can use traverse.dir which by default traverses a single specified folder. However, you can change it to traverse recursively for it sub folders as well. You can find a simple usage of the api as below:


var traverse = require("traverse-fs);
var path = require("path");

// traverse.dir("./", callback);

// Alternatively, you can specify specific callbacks of your own and go recursive traversing
traverse.dir("./", (dir, file) => { return path.join(dir, file.name) }, shouldRecurse = true).then(console.log);

Simple - Return Nested Array

You can use the directory/ folder traversing and get a return of a nested array (array of arrays) as the result.


var traverse = require("traverse-fs);
traverse.traversePath("./", console.log, shouldRecurse = true)
// traverse.dir("./", console.log, shouldRecurse = true)

Simple - Return Single Level Array

You can use the directory/ folder traversing and get a return of a single level array as the result. The result will have the complete path of the file in case the file being in the sub directory.


var traversePath = require("traverse-fs").traversePath;
var path = require("path");
const TEMP_DIR = resolve('./temp_traversal_root');
async function main() {
    const generalCallback = async (path, name, isDir) => { console.log }
    try {
        await traversePath(TEMP_DIR, generalCallback);
    } catch (e) {
        console.error("Traversal Test 1 failed:", e.message);
    }
}

Simple - Return JSON

You can use the directory/ folder traversing and get a return of a json as the result. The result will have the complete path of the file in case the file being in the sub directory.

const userExampleStructure = {
    "dir": {
        "dir2": {},
        "file1": "",
        "file2": ""
    }
};

const userPaths = flattenStructureToPaths(userExampleStructure);
userPaths.forEach(p => console.log(p));


Simple - Simple Search usage


const fs = require('fs').promises;
const path = require('path');
const { resolve, dirname, join } = path;
const { traversePath, getDirectorySize, traverseFS } = require("../index.js")


const foundFiles = [];
const TEMP_DIR = resolve('./temp_traversal_root');
const searchCallback = async (path, name, isDir) => {
    if (!isDir && (name.endsWith('.jsx') || name.endsWith('.js') )) {
        // Found a match: store it in our results array
        foundFiles.push(path.replace(dirname(TEMP_DIR), '.'));
    }
    // No need for a print statement here, we collect results silently
};

async function main() {
    try {
        // Start search only in the 'src' subdirectory
        await traversePath(TEMP_DIR, searchCallback);

        console.log(`\nSearch Complete. Found ${foundFiles.length} JSX files:`);
        foundFiles.forEach(file => console.log(`\t- \x1b[33m${file}\x1b[0m`)); // Yellow
    } catch (e) {
        console.error("Traversal Test 2 failed:", e.message);
    }

}

main()

API for traverse-fs / fssys


  • traverse.dir Usage and Default implementations:
    traverse.dir(
      directory = "./",     // directory to traverse
      callback,             // to run
      recursive = false,    // whether to traverse nested and recursively
      
    )

Contribution

Please feel to make contributions or raise issues to the repository by creating a pull request or raising an issue

License

MIT License

About

Nodejs npm module to traverse folder using code or cli or use glob patterns. Install: `npm i traverse-fs`

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors