Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Node.js access to your app's version and release metadata

License

NotificationsYou must be signed in to change notification settings

photostructure/mkver

Repository files navigation

Easy access to your version and build metadata from withinNode.js

npm versionNode.js CICodeQL

Why?

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,asarification 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.

What?

mkver produces either:

Example output

// 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-compatiblereleasetag that looks like${version}+${YYYYMMDDhhmmss of gitDate}, and agitDate, which is aDateinstance of when that last git commit happened.

Installation

Step 1: addmkver to your package.json

npm i --save-dev mkver oryarn add -D mkver

Step 2: For TypeScript users

Add apre... npm script to yourpackage.json that runsmkver:

"scripts": {..."precompile":"mkver","compile":"tsc",...  }

Step 2: For JavaScript module or CommonJS users

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    ...}

Step 3: Add to .gitignore

You should add yourVersion.ts,version.mjs, orversion.js file toyour project's.gitignore.

How

mkver is a pretty simple, no-dependencies, three-step, one-trick pony:

  1. mkver first looks for apackage.json in., then.., then../..,etc, and extracts theversion value.
  2. 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.
  3. 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.

Use with TypeScript or MJS modules

import{version,release}from"./Version"

Use with <= ES6 javascript

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!)

Bash access to your version info

Need access to yourrelease from, say, your deploy script written inbash?

  release=$(node -e"console.log(require('./path/to/Version.js').release)")

Changelog

SeeCHANGELOG.md.


[8]ページ先頭

©2009-2025 Movatter.jp