Close

Versioning NPM Packages in TFS

So I recently needed to build an NPM package with TFS, and wanted the version number to be updated automatically on build. But after searching, I couldn’t find a good example of how to do it, and TFS seemed to have no tools built in to help.

So I finally I just wrote a simple Powershell script to handle it, and wanted to reproduce it here for the benefit of others who have the same issue:

cd $env:BUILD_SOURCESDIRECTORY
$a = Get-Content 'package.json' -raw | ConvertFrom-Json
$a.version=$a.version.Substring(0, $a.version.LastIndexOf(\".\")) + \".\" + $env:BUILD_BUILDID
$a | ConvertTo-Json  | set-content 'package.json'

Or, if you use GitVersion like we do, after running the GitVersion build step, you will have new environment variables with version numbers in different formats available, which can be used like so:

cd $env:BUILD_SOURCESDIRECTORY
$a = Get-Content 'package.json' -raw | ConvertFrom-Json
$a.version = $env:GITVERSION_NuGetVersion
$a | ConvertTo-Json  | set-content 'package.json'

Hope this helps anyone trying to build and publish NPM packages as part of a TFS build!

2 thoughts on “Versioning NPM Packages in TFS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Back to top
%d bloggers like this: