- Notifications
You must be signed in to change notification settings - Fork1
Node.js access to your app's version and release metadata
License
photostructure/mkver
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Easy access to your version and build metadata from withinNode.js
Simple, reliable, no-runtime-dependency access to version and buildinformation from within node and Electron apps should be easy.
Even if you push git SHAs into yourpackage.json
, afterminification,asar
ification and installation into who-knows-whereplatform-specific directory structures, you'll still be fighting__dirname
bugs trying to find where yourpackage.json
went.
In TypeScript and ES6 Module worlds, there's a super simple,minification-compatible and asar-compatible solution to importing informationfrom outside your current file, and it's great.
It's calledimport
. Or for youold-skoolkids,require
.
If we can write build-specific information as constantsas code, living inour codebase, consumption of this metadata becomes trivial. Add it to your buildpipeline, import the thing, and then solve the Big Problems.
mkver
produces either:
- a
Version.ts
(the default, forTypeScriptusers), - a
version.mjs
(forJavaScriptmoduleusers),or - a
version.js
(if you're usingCommonJS) with your git SHA andversion information exported as constants.
// Version.tsexportconstversion="1.2.3-beta.4"exportconstversionMajor=1exportconstversionMinor=2exportconstversionPatch=3exportconstversionPrerelease=["beta",4]exportconstrelease="1.2.3-beta.4+20220101105815"exportconstgitSha="dc336bc8e1ea6b4e2f393f98233839b6c23cb812"exportconstgitDate=newDate(1641063495000)exportdefault{ version, versionMajor, versionMinor, versionPatch, versionPrerelease, release, gitSha, gitDate,}
The filename can be anything you want, but the file extension must be.ts
,.mjs
, or.js
.
For extra credit, it also creates aSemVer-compatiblerelease
tag that looks like${version}+${YYYYMMDDhhmmss of gitDate}
, and agitDate
, which is aDate
instance of when that last git commit happened.
npm i --save-dev mkver
oryarn add -D mkver
Add apre...
npm script to yourpackage.json
that runsmkver
:
"scripts": {..."precompile":"mkver","compile":"tsc",... }
Addmkver
as apre...
script for your test script and/or yourwebpack/gulp/grunt/browserify pipeline in yourpackage.json
:
"scripts":{ ..."prebuild":"mkver ./lib/version.mjs",// or ./lib/version.js"build":"webpack",// or whatever you use ...}
You should add yourVersion.ts
,version.mjs
, orversion.js
file toyour project's.gitignore
.
mkver
is a pretty simple, no-dependencies, three-step, one-trick pony:
mkver
first looks for apackage.json
in.
, then..
, then../..
,etc, and extracts theversion
value.mkver
thenexecsgit rev-parse HEAD
to get the last commit SHA. Havinggit
available tothe calling shell is a prerequisite. Please don't file a bug report for this.- Finally,
mkver
writes the contents to the first argument given tomkver
,which can include a subdirectory. The default output is./Version.ts
.Existing files with that name will be overwritten.mkver
uses the fileextension to determine what format (TypeScript, module, or es6) to render theoutput.
If anything goes wrong, expect output onstderr
, and a non-zero exit code.
import{version,release}from"./Version"
const{ version, release}=require("./version")// < mind the case matches whatever you give mkver
Remember tomkver version.js
in your npm script (see the Installation's "Step 2" above!)
Need access to yourrelease
from, say, your deploy script written inbash
?
release=$(node -e"console.log(require('./path/to/Version.js').release)")
SeeCHANGELOG.md.
About
Node.js access to your app's version and release metadata