DEV Community

Cover image for Build counter
Kolja
Kolja

Posted on

Build counter

I asked myself, "how often did I build this project"?

Therefore, I'm using Vite (with SvelteKit), I searched for a build-in build-counter, but it seems, that there is nothing like that.

Okay, I didn't search that long, because this is a well working solution:

#!/bin/bash

# read the .env file
source .env

# Increase the version number in .env
NEW=$(($PUBLIC_BUILD_COUNT+1))

# write the new version number to .env
sed -i~ '/^PUBLIC_BUILD_COUNT=/s/=.*/='$NEW'/' .env
Enter fullscreen mode Exit fullscreen mode

So in my package.json I added this:

"build": "bash increase.sh && vite build",
Enter fullscreen mode Exit fullscreen mode

and the .ENV file will have a counter:

PUBLIC_BUILD_COUNT=39
PUBLIC_BUILD_DATE=22.03.2023-15:52
Enter fullscreen mode Exit fullscreen mode

Oh wait, there is more:

# Get the current date and time
DATE=$(date +'%d.%m.%Y-%H:%M')

# write the new date to .env
sed -i~ '/^PUBLIC_BUILD_DATE=/s/=.*/='$DATE'/' .env
Enter fullscreen mode Exit fullscreen mode

I think, you got it ;-)

Top comments (1)

Collapse
 
nomouse profile image
slade-london

just what i was looking for!