I think it would be easier to write another starting file and read the version inside package.json
I have come out another solution by writing a start.sh.
folder structure
- my-project
|- build
|- 0.0.1
|- 0.1.0
|- 0.1.1
|- index.js
|-start.sh
|-package.json
my start.sh
#!/bin/bash
export p="./build/$(ls ./build | sort -r | head -n 1)"
node $p
My solution is quite simple, list all the directory under ./build and then sort in reverse. Now 0.1.1 is the biggest version and it would list at first. head -n 1
means get the first line which is the current biggest version number.
Then use the dir as the node execution path.
Inside the package.json
"scripts": {
"test": "bash start.sh"
}
I think it is similar way to change to your own command.
Note I have tried to put script content inside package.json, but the npm run would not interpret shell variables well. So I recommend write a standalone script.