So for todays node cli tool example I will be writing about a basic file system walker that I have put together for this series of posts. I have wrote a few posts on file system walkers such as klaw, and just simply walk, as well as the many native nodejs module methods that can be used to make a file system walker from the ground up. This is a post on making node cli tools so for todays post I will be making a file system walker using the node file system method like readdir, stat, and so forth.
This is an example of a basic file system walker that can be used from the command line. The idea of the project is to have a command that I can call from the command line where I pass a target folder to walk, and a script that I would like to apply for each file found in that target folder. This command example is one of many node cli examples that I have made for my node_cli_tools project that I am working on these days. This command line interface tool, along with many others is still a work in progress along with everything else in the project.
If you would like to reproduce what I am writing about here you might want to start with my main post on the node_cli_tools project, as well as check out the repo itself where you can just clone it down, install it globally, and be done with it.
The index.js file for the nc-walk command of my node_cli_tools project uses yargs to parse options for the command, as with all my other commands in this project. This is the script that will run first when the nc-walk command is used, so that is what I have the nodejs shebang at the top of the file.
So with that out of the way lets move on to the default.js file that contains the rest of the logic for the single default command.
So here I have the default command module for the nc-walk node cli tool example. The builder object of the command outlines the options for the command. The t option is for the target folder to walk, there is a recursive option, and of course a s option to set the script to run for each file in the target folder.
The handler calls the main method of the walk.js shared lib that I have worked out for the nc-walk command, as well as file system walking in general for every command in the project. So now it is just a question of taking a look at that module, and that is all here is to this command.
So here is the state of my walk.js module as of this writing at least. The general idea here is that I have a walk method that is called just once for the contents of a folder without following any nested folders, but can also be called recursively.