Node.js access to your app's version and release metadata
MIT License
Easy access to your version and build metadata from within Node.js
Simple, reliable, no-runtime-dependency access to version and build information from within node and Electron apps should be easy.
Even if you push git SHAs into your package.json
, after
minification, asar
ification and installation into who-knows-where
platform-specific directory structures, you'll still be fighting
__dirname
bugs trying to find where your package.json
went.
In TypeScript and ES6 Module worlds, there's a super simple, minification-compatible and asar-compatible solution to importing information from outside your current file, and it's great.
It's called import
. Or for you old-skool
kids, require
.
If we can write build-specific information as constants as code, living in our codebase, consumption of this metadata becomes trivial. Add it to your build pipeline, import the thing, and then solve the Big Problems.
mkver
produces either:
Version.ts
(the default, for TypeScriptversion.mjs
(for JavaScriptversion.js
(if you're using// Version.ts
export const version = "1.2.3-beta.4"
export const versionMajor = 1
export const versionMinor = 2
export const versionPatch = 3
export const versionPrerelease = ["beta", 4]
export const release = "1.2.3-beta.4+20220101105815"
export const gitSha = "dc336bc8e1ea6b4e2f393f98233839b6c23cb812"
export const gitDate = new Date(1641063495000)
export default {
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 a SemVer-compatible release
tag that looks like
${version}+${YYYYMMDDhhmmss of gitDate}
, and a gitDate
, which is a Date
instance of when that last git commit happened.
mkver
to your package.jsonnpm i --save-dev mkver
or yarn add -D mkver
Add a pre...
npm script to your package.json
that runs
mkver
:
"scripts": {
...
"precompile": "mkver",
"compile": "tsc",
...
}
Add mkver
as a pre...
script for your test script and/or your
webpack/gulp/grunt/browserify pipeline in your package.json
:
"scripts": {
...
"prebuild": "mkver ./lib/version.mjs", // or ./lib/version.js
"build": "webpack", // or whatever you use
...
}
You should add your Version.ts
, version.mjs
, or version.js
file to
your project's .gitignore
.
mkver
is a pretty simple, no-dependencies, three-step, one-trick pony:
mkver
first looks for a package.json
in .
, then ..
, then ../..
,version
value.mkver
thengit rev-parse HEAD
to get the last commit SHA. Having git
available tomkver
writes the contents to the first argument given to mkver
,./Version.ts
.mkver
uses the fileIf anything goes wrong, expect output on stderr
, 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 to mkver version.js
in your npm script (see the Installation's "Step 2" above!)
Need access to your release
from, say, your deploy script written in bash
?
release=$(node -e "console.log(require('./path/to/Version.js').release)")
See CHANGELOG.md.