Jahed Ahmed

Terraform NodeJS Wrapper Improvements

I finally got around to making some long awaited improvements to @jahed/terraform, a NodeJS wrapper for managing Terraform versions automatically.

Accurate Test Scenarios

Previously, the tests used to run the bin script directly using:

1
node bin/terraform.js --version

This is of course not how most people will use the package. They'd use something more like:

1
yarn terraform --version

So I've updated the tests to do that. However, that also caught a bug. Which leads us too...

Stop Using npm/Yarn's Lifecycle Hooks

I've always disliked the use of lifecycle hooks in npm and Yarn. They're often unpredictable, poorly defined and difficult to trust. Especially when relying on both npm and Yarn to behave the same.

The new tests caught an issue. While npm will always call a dependency's install hook, Yarn won't if that dependency is already installed.

This was actually good news. A while back, I attempted to migrate the project to TypeScript. However because of the install hook, I couldn't build the project before executing npm install. So I couldn't install the dependencies needed to build the project to run the install hook. A sort of chicken-and-egg problem.

So the solution? Stop using lifecycle hooks.

Installing On-Demand

The install process for @jahed/terraform has changed. There's no magic install hook that downloads Terraform when you run npm install. Instead during any Terraform execution, it will download the correct version in a cache and execute it.

This approach also had the added advantage of fixing a few issues people had and makes the package a lot more portable.

Removed Logging

Now that the download phase happens during Terraform execution, I wanted to make it look as seamless as possible. So all logging done by @jahed/terraform is now using NodeJS's debuglog. This means it's not visible unless if NODE_DEBUG='@jahed/terraform' is added to the environment.

There's a downside to this. On a slow connection, the download might take a while, so you'll see nothing until it completes. I might improve this later if it's a common problem.

TypeScript Migration

As mentioned before, since there's no lifecycle hooks, I finally managed to migrate the project to TypeScript. While there's not much benefit for users, it's nice to have all of my projects use the same language and tools where possible.

Testing More NodeJS Versions

The GitHub Actions test workflow now tests the project against all NodeJS LTS versions. I was never sure if I maintained compatibility in the past, now I will.

Conclusion

That's about everything. A few of these changes are available in 1.0.6. Some won't be available until the next Terraform release. That's just how this package rolls.

Thanks for reading.

Related Tags