Word Count History nodejs example
This nodejs example is a project that I wanted to start a long time ago, but kept putting off. It is a script that will use a git log command to get a list of commit hash ids from the latest commit on master. Once it has a list of commit hash ids it will use a git checkout command to switch to the oldest commit in the list. From there is will loop up back to the newest commit in the list again.
1 - The git lib of the nodejs example
I have a single module for this nodejs example thus far that has a whole bunch of methods to help me work with a git folder. I have a method that will just check if a given folder is a git folder, and return a resolve or rejected promise depending on that. I have another method that will create a list of commits from the current commit back a number of commits, and other such methods. So in this section I will be going over this module, and how it helps with this word count history nodejs example.
1.1 - the start of the module, and the folder check method
So at the top of the module I am referencing in the exec child process modules method. If you are not familiar with this method as well as other alternatives such as spawn I recommend reading up on them. The methods can be used to call extremal commands on the operating system such as, but certainly not limited to git.
In any case this method makes use of the git status command to just find out if the given folder is a git folder or not, and that is it.
1.2 - The get commit list method
Here I have a method that will create a list of commit objects, where each commit property of a commit object is the hash id of a given commit. In addition a commit object will have at least a few more properties maybe, but for now the only one I am pretty sure I am going to want to have is date.
This method makes use of the git log command that would be what is used to create a list of commits manually in the command line. I am also making use of the format option of that command to get a clean output that is just the commit hash ids and dates.
1.3 - the to Commit method
The to commit method that I worked out for this module can be used to set the commit of a git folder by giving the hash id for the commit, or something like master. This method makes use of the git checkout command that would be used to set the current commit of a git folder manually.
2 - The index.js file for the nodejs example
So now that I have my git module worked out I can now use it in the main index.js file for this nodejs example. Here I am using the folder check method of the git module to check if I am event working in a git folder to begin with, after, that I make sure that the current commit is the latest commit on the master branch. I then use the git commit list method to get an array of objects for each commit going back a set number of commits to the latest commit on master.
Once I have my Commit Object list I can the use the commit hash id of each object to go back to the point that is a set number of commits back, check something out, and then move up to the latest commit. For now I am just reading the contents of the folder at each point, but the idea is to create a word count history for each markdown file in the folder.
3 - Conclusion
I have not yet finished this canvas example so there is more work that I need to put into this one before I can really conclude how things went for this example. So far it is working as expected though, I have found that it might be best to try to break a project like this down into many small fine grain minor release rather than trying to get the whole thing done within a few hours or so.
This is the first time I have made a nodejs project like this in which I am using the child process module to call git a whole bunch of times to change what the current commit is of a got folder. For now it is not doing much, but the basic core idea of what I wanted is working at least for what it is worth.