I'm working on a module package that generates text files, and I'm appending the current module version to the generated files. This package helped me to deal with reading the package.json file as expected, I'm really grateful for it.
I'm using TypeScript, and wanted to, as the "Advanced Integration" documentation suggests, generate the version file after the patch.
I've tried to use a postversion.sh as indicated but this caused some issues. For example, this scripts requires you to fiddle with the git tags after creating, and also for my case of using Typescript it wasn't so useful because at this step the code has already been built and this file wasn't there in the process.
I've found a practical way to use it, which I think other people might find useful, which is:
- create a "gv" script to generate the file and add it to the build process:
// file: package.json
{
...
"scripts": {
...
"gv": "genversion --esm --property name,version,homepage src/constants/project.ts",
"build": npm run gv && tsc"
...
}
}
- Add a "version" script which runs after the version was bumped but before the tagging which I use to build the module and to add the generated file to the commit:
// file: package.json
{
...
"scripts": {
...
"gv": "genversion --esm --property name,version,homepage src/constants/project.ts",
"build": npm run gv && tsc",
"version": "npm run build && git add -A",
...
}
}
And now, each time you run npm version patch for example, the file is generated and pushed along with the package.json update. Besides that, you've already built the project with the recently generated version, so you can just publish it right away.
I'm not an expert on this, so I might have overlooked something, but this was really helpful on my project, and I didn't had to create a shell file, made it executable and so on, it just works right out of the clone.
Cheers!
I'm working on a module package that generates text files, and I'm appending the current module version to the generated files. This package helped me to deal with reading the
package.jsonfile as expected, I'm really grateful for it.I'm using TypeScript, and wanted to, as the "Advanced Integration" documentation suggests, generate the version file after the patch.
I've tried to use a
postversion.shas indicated but this caused some issues. For example, this scripts requires you to fiddle with the git tags after creating, and also for my case of using Typescript it wasn't so useful because at this step the code has already been built and this file wasn't there in the process.I've found a practical way to use it, which I think other people might find useful, which is:
And now, each time you run
npm version patchfor example, the file is generated and pushed along with thepackage.jsonupdate. Besides that, you've already built the project with the recently generated version, so you can just publish it right away.I'm not an expert on this, so I might have overlooked something, but this was really helpful on my project, and I didn't had to create a shell file, made it executable and so on, it just works right out of the clone.
Cheers!