Skip to content

Instantly share code, notes, and snippets.

@danielsharvey
Forked from amcdnl/version.ts
Last active June 6, 2019 02:47
Show Gist options
  • Save danielsharvey/3b2a2c86ff9a67a98b7200ae955259e8 to your computer and use it in GitHub Desktop.
Save danielsharvey/3b2a2c86ff9a67a98b7200ae955259e8 to your computer and use it in GitHub Desktop.
Automatic version stamping with build number
{
"buildNumber": 1
}
{
"scripts": {
"prestart": "node version.js"
}
}
// See https://medium.com/@amcdnl/version-stamping-your-app-with-the-angular-cli-d563284bb94d
const { gitDescribeSync } = require('git-describe');
const { resolve, relative } = require('path');
const { writeFileSync } = require('fs-extra');
const moment = require('moment');
const { version } = require('./package.json');
var { buildNumber } = require('./build_number.json');
buildNumber++;
const gitInfo = gitDescribeSync({
dirtyMark: false,
dirtySemver: false
});
let prerelease = version.split('-');
let _version = prerelease.shift();
let [major, minor, patch] = _version.split('.');
if(gitInfo.dirty) prerelease.push('dirty');
let raw = [_version, ...prerelease].join('-');
let build = [buildNumber, gitInfo.distance, gitInfo.hash].filter(v => v).join('.');
// full: <version>[-prerelease][-dirty]+<buildNumber>.<distance>.<sha>
let versionInfo = {
raw: raw, // '4.0.0-rc.6',
major: major, // 4,
minor: minor, // 0,
patch: patch, // 0,
prerelease: prerelease, // [ 'local' ],
buildNumber: buildNumber,
build: build, // 'sha.73808dd',
version: version, // '4.0.0-local',
// codeName: 'snapshot',
// isSnapshot: true,
full: [raw, build].join('+'), // '4.0.0-local+sha.73808dd',
// branch: 'master',
// commitSHA: '73808dd38b5ccd729404936834d1568bd066de81'
hash: gitInfo.hash,
buildDate: moment().format(),
};
const bnFile = resolve(__dirname, 'build_number.json');
writeFileSync(bnFile,
`${JSON.stringify({buildNumber:buildNumber}, null, 4)}
`, { encoding: 'utf-8' });
const file = resolve(__dirname, 'angular_src', 'environments', 'version.ts');
writeFileSync(file,
`// IMPORTANT: THIS FILE IS AUTO GENERATED! DO NOT MANUALLY EDIT OR CHECKIN!
/* tslint:disable */
export const VERSION = ${JSON.stringify(versionInfo, null, 4)};
/* tslint:enable */
`, { encoding: 'utf-8' });
console.log(`Wrote version info ${versionInfo.version} to ${relative(resolve(__dirname, '.'), file)}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment