New Tool: File Line Replacer

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.

DbSchema Example Model
DbSchema Example Model

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

id: {
  type: DataTypes.INTEGER.UNSIGNED,
  allowNull: false,
  primaryKey: true
},

… when they should have had this

id: {
  type: DataTypes.INTEGER.UNSIGNED, 
  autoIncrement: true, 
  primaryKey: true 
},

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…

file-line-replacer \
    --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" \
    --overwrite

The switches used here are the key. Here’s what they do…

--search-dir
Starting directory to search for files.

--backup-dir
Each file is stored in this location before it is modified.

--old-lines
Pipe-delimited list of text lines to search for within each file.

--new-lines
Replacement lines for each occurrence of the --old-lines

--overwrite
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…

--source-file
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.

--destination-file and --destination-dir
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.

--old-lines-file and --new-lines-file
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.”

--ignore-patterns and --ignore-patterns-file
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 speedbump 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.

Generating Mongo / Mongoose Models

Having come from the .NET world, I have always loved the ability to whip up a quick model diagram using the SQL Server Diagram Tool. It’s painless to model your data objects, and capture a good chunk of your business, for LOB applications. And, while in that world, I relied upon the CodeSmith Generator to spit out all sorts of documents from my database.

Alas, having moved to Mac, Linux, and MEAN Stack, all this is in the past.

… until now.

DbSchema is really what started me thinking down this line. It’s written in Java and, therefore, is cross-platform. I have used it successfully on all three platforms, to replace the SQL Server Diagram Tool, and it works flawlessly.

Here’s the cool part: unlike the M$ tool, DbSchema stores its data in good ole’ XML. So, of course, I’ve created a few tools to add some awesome sauce to it…

DbSchema Parser dbschema-parser

Long story short, dbschema-parser allows you to walk the data structures using NodeJS. You may navigate from Database, to Schema, to Table, to Column, and back up again, or in any direction.

DbSchema Parser CLI dbschema-parser-cli

Since I want to use the Parser to generate files, I’m gonna need a CLI. That’s what this project brings to thy table.

DbSchema Mongoose dbschema-mongoose

Under the hood this one is ugly as sin. However, it’s the thang that gives the two projects, above, some coolness. It basically looks at your DbSchema’s data file and spits out the equivalent Mongoose model files.

Side note…

I’m also using Keybaord Maestro, on Mac, and AutoHotkey, on Windows, to help me bang out complex data diagrams with only a few keystrokes. So, that helps a great deal.

Why create this?

In short, there’s nothing stable that provides this. DbSchema is the only tool that comes close to the stability and fluidity of the SQL Server Diagram Tool. And, as for generating models, there’s nothing out there that feeds from an elegant UI. Plus, although there’s a tonne of shtuff with Yarn and Yeoman, nothing feels fully baked.

Anywhoo, I hope this helps someone. It’s ugly. I know. If anyone shows genuine interest in it, I’ll see about extending it.

Wake Up Gently to the Sound of Nature

I absolutely love the feeling of waking up to birds singing in the cool summer breeze. Sleeping with the windows open, here in the northeastern United States, allows my body to be gently lulled awake as distant birds stretch their wings in search of an early morning snack.

Countless studies have proven the abrupt sound of a typical alarm clock causes you to feel tired, run-down, and may induce headaches. I have avoided these unpleasent feelings for years ensuring I always wake up gently to the sound of nature. When weather does not allow for the windows to remain open, or when I must be awake by a certain time, I rely on special high-quality recordings in lieu of Mother Natuer herself.

The recordings below are each one or two hours in length. Each original recording is accompanied by three others altered to gradually increase their volume (or “fade in”) from 0db (complete silence) to their full volume over time.

How to determine your new alarm time:
1. Pick a awake time using the time you need to wake up: 8:00 AM
2. Your waking duration is the amount of time to lull your body awake: 30 minutes
3. The alarm time is your waking duration subtracked from your awake time7:30 AM

How to use the recordings:
1. Listen to each Sample to find a recording you enjoy;
2. Download the Original version (used to set volumes);
3. Download the version with a fade-in time equal to your waking_ duration;
4. Play the Original version on your alarm clock to find a comfortable full volume level; and,
5. Set your alarm clock to play the version with the fade at your alarm time (above).

Example:
Actual time I must wake up by:     8:00 AM
Duration to lull my body awake:    30 minutes
Alarm's time (start playing MP3):  7:30 AM
Backup alarm's time:               8:15 AM (on critical days)

Note:
On critical days, I generally set a backup alarm, using a separate device, for 10-15 minutes after my awake time. The backup alarm does not use a gradual volume increase. This ensures I’ll be awake even on those days when I’m too tired to hear my alarm clock.

Rememeber to grab the original version!
I recommend also using the original version of the selected track to set the volume of your alarm clock or listening device. This will help prevent selecting a volume that is too quiet to actually wake you.


Available Tracks

Relaxing Bird Songs in Wood Birds Chirping (64 min total)
Original | Sample | 10 min fade-in | 30 min fade-in | 60 min fade-in

Forest Birdsong – Birds Chirping (120 min total)
Original | Sample | 10 min fade-in | 30 min fade-in | 60 min fade-in

Meditation Music Relax Mind Body (64 min total)
Original | Sample | 10 min fade-in | 30 min fade-in | 60 min fade-in


Please let me know if there are any other sounds or tracks that might help you with this technique. My email address is: fred.lackey@gmail.com