Note: This is a post shilling one of my github repos, but luckily for you, a shill for broke kids like me is free so if you like it try it
Have you heard, git worktrees is a thing. Of course you have, you're a hip young developer, following all the tweet-tweets. Worktrees, a simple concept that has been around for years has been gaining traction over the past couple of years, it's taking the internet by… well a Twitter thread here and there… on occasion… apparently… so I've been told. A single bare repo that you can you create branches as directories off of. It's revolutionary! If you don't understand why people who use worktrees often sing its praises you obviously do not have to deal with a lot of the cumbersome aspects of working with large codebases in large organizations. You lucky sonova….
For the rest of us, the amount of times that I had stash my progress in order to checkout a co-workers "urgent" PR is countless, and the mere fact that I no longer have to stash a bunch of things and pray to the our loard and savior Linus Torvalds and can instead keep a worktree called "PR" that I use to checkout PR's is now as easy as well cd ../pr
.
There is one big caveat to worktrees, and a good majority of you readers, especially those who have used worktrees with any JS/TS project before are probably thinking it. I see you, you know whats up, you're looking might fine today.
You see, for those who are uninitiated, every worktree is just a clean pull from your remote repo. Meaning just like using git clone
you have to at least run a clean npm install for every new worktree, not to mention if you have any .env files or anything else that is in your .gitignore that your environment need you have to copy those over from wherever they might be.
This is actually not such a big deal you can get both your node_modules
folder and dotfiles from another previous worktree so its just a simple cp .env node_modules
and your done. but if you just cloned that that bare naked repo (naughty) you still need to get those pesky dotfiles from somewhere, and you still need to run that dang npm install.
However, like any true developer the mere fact that you have to think about all that and type the same same thing over and over for every new worktree should give shivers up your spine. And this is probably a good reason why a lot of web developers shy away from worktrees. Why go through that pain? We are pioneers! How dare someone set up a system that makes us have to type more!
What if we unloaded our dotfiles to a completely different directory. and we kept that separate from everything else, you already do that with your config files(yeah I know whats going on that root directory that you can't be bothered to clean). I like my machine to be sleek and clean and only be running the projects I am working on at a single time. This means I could remove a repo once I was done with it on my local machine but still hold on to the dotfiles so that in the future when a client asks to change back that terrible idea that they had and wouldn't listen to reason I can quickly pull it down and still have the dotfiles. Great.
But let's take that one step further. What if we could do that with the node modules as well. Sure it bloats our computer a bit depending on how many projects we have but it saves that time of running npm install on every new worktree. And let's be honest here, if you're working with Javascript or Typescript you accept the bloat, nay, you embrace is. STONKS BABY!
Next, let's imagine that its the first worktree in a new repo that we just cloned down for the first time. We don't have the node_modules
directory for it so naturally we have to run an npm install. It's a pain but it's the first time we pulled the repo down, it can't be helped. However, I'm a one and done kinda guy. so let's make it so that if we run an npm install it should copy over those newly minted node_modules
to the correct dotfiles directory, because why should we go through the struggle of pressing those half-dozen or so keys. Do I go to work to work? NO, We want that automated.
finally, what if we are working with a couple different environments and… well our node configs are different for each of those environments well we should probably keep all those in our dotfiles directory as well. And we should probably realize that as developers our ability to name those environments extends to "prod", "test", "dev", "staging", "dev-feature", "dev-feature2"… I named these, but do you think I can be bothered to remember which is which? No, and I refuse to. Again, why work on bettering myself? The computer should better itself… and then, you know, eventually, maybe help me do my job better… But its on the computer!
Enter, a fuzzy-finder to help coax my numb little brain into remembering which "dev-feature-blah-blah" I want.
so if your with me, and you have to work with package managers then you should understand that what we are looking for is a simple command that we can enter that will:
- open a fuzzy finder of dotfile directories
- checks if there is a node_modules directory in the chosen dotfiles
- if there isn't, run npm install(again, I'm lazy. I cant be bothered to type npm i)
- if npm install was ran, copy over the new node_modules to that same dotfiles directory
- if there is already a node_modules directory just copy that over to the current worktree(why waste time and bandwidth, if we got it already)
- copy over any .env files we might have to the current worktree
Oh and probably as a last little thing, we might want a check to just exit out if conditions aren't met. If I make this script and it has some shortcut my fat little stubby fingers are bound to accidentally bump those keys at some point and I don't need some script to come harass me or try to do all this work in folders that don't need it.
Enter, NW. Short for New Worktree… or North West if you're looking at a map or compass. A small little bash script that solves the above issue in the manner that we just outlined. And as a script, you can easily make a git hook or shortcut or add it to something like your editor to run automatically when you create a new worktree. I'm trying to solve a problem here people. My fingers, are short, stubby, there's no finger treadmill to get them slim and fit enough for writing npm install and cp .env . every time I make a new branch on my local machine. No, my fingers prefer to lay around all day and do nothing.
So if you are interested in looking at this. Trying it out for yourself, seeing how it feels. heck if you want give your own fingers a push up(a single one, because thats all the work out they should need) and make some changes to the script to fit your own needs go for it, maybe even make a PR if your feeling adventurous.
Top comments (0)