There a new command-line tool for searching files, scanning those files for blocks of multi-line content and then replacing those blocks with different lines. Some benefits of this are…
- works on Windows, Mac, and Linux
- no nasty RegEx or escape characters to specify multi-line values
- backs up original files before making changes (if desired)
- whitespace is either ignored or preserved … your choice
- supports text files of virtually any size
Yes, I realize there are other utilities out there that will replace text… sed, awk, etc. PowerShell will even do it if you know the switches. However, in my opinion, all of them are heavily opinionated and take the geek-first approach. I wanted something I could give to a junior or mid-level person and know they can get the job done without spending their time researching how to structure some overly complex command.
One of the best tool sets for prototyping a relational data service is…
DbSchema : for brainstorming an designing the entities;
ExpressJS : probably the best web framework for hosting the web service; and,
Sequelize ORM : to generate the models and handle the data calls.
My original need came while using Sequelize to generate model files while for a new data service. I’m not sure what caused it (maybe switching between MySQL and PostgreSQL) but the models did not include logic for auto-incrementing primary key fields. So, models ended up having this…
… when they should have had this …
So, why not contribute to the Sequelize project and submit a fix? The short answer is that the need to search & replace multiple lines is not specific to Sequelize. As a developer, all of your work is done with text files… the source code. And, over the years, I’ve had reason to perform this type of task several times. Creating
file-line-replacer allowed me to get past the hiccup and be ready for the time when I need it again, outside of Sequelize.
Installing the utility is a snap. Once you have Node on your machine, simply install the command with…
npm install -g file-line-replacer
This installs the project and allows it to be used just like any other command-line utility. Then, correcting the model files was as simple as issuing one lil’ command…
--search-dir "/Users/flackey/my-project/src/data/models" \
--backup-dir "/Users/flackey/my-project/_backup" \
--old-lines "allowNull: false,|primaryKey: true" \
--new-lines "autoIncrement: true,|primaryKey: true" \
The switches used here are the key. Here’s what they do…
Starting directory to search for files.
Each file is stored in this location before it is modified.
Pipe-delimited list of text lines to search for within each file.
Replacement lines for each occurrence of the –old-lines
Ensures we know the files will be overwritten (flags are set to true by simply adding the flag name to the command).
There are tons of other flags and features listed on the project page here. Some of them include…
Not everyone wants to search for files. You are able to specify the exact file to tweak. This is great if you want to use
file-line-replacer in a BASH script.
Maybe you don’t want to overwrite your files. Specifying the “destination” allows you to tweak your files and send them to a specific folder. This is great for working with source templates where overwriting or modifying the template is not desired.
Allows you to store the “old” and “new” lines inside of text files. You would provide a path to the file instead of supplying the actual values. This is handy for complex lines and making your scripts more “human-readable.”
The default search pattern is **/. (aka “all files, recursively”). Specifying “ignore” patterns allows more granular control on files and directories to skip.
In the grand scheme of things, I could have accomplished all of this with a BASH script. However, then I would have had more of a “uni-tasker” and not really gained anything in my developer toolbox.
Overall, I think this is a great lil’ utility. It performs a task that is quite common with developers and IT people while preventing folks from having to remember the complex syntax for outdated commands. It also allows me to personally overcome a speed bump that has been occasionally bothering me for years.
In the end, I hope whoever finds the utility is helped in some way. After all, that is why I love development so much.