Last active
March 23, 2016 23:00
-
-
Save nfreear/be980ef2491b3e2a74c3 to your computer and use it in GitHub Desktop.
Output version.JSON containing Git commit & other version info (Node.JS) (npm install: simple-git)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*! | |
Output version.JSON containing Git commit & other version info. | |
@copyright Nick Freear, 22 March 2016. | |
@see https://gist.github.com/nfreear/be980ef2491b3e2a74c3 | |
@see https://github.com/IET-OU/open-media-player-core/blob/master/src/Gitlib.php | |
*/ | |
// USAGE: npm install simple-git --save-dev && node bin/git-version.js --all | |
var directory = __dirname + '/..' | |
, json_file = directory + '/static/src' + '/version.json' | |
, git = require('simple-git')(directory) | |
, fs = require('fs') | |
, execSync = require('child_process').execSync | |
, tomcat = 'java -cp ${CATALINA_HOME}/lib/catalina.jar org.apache.catalina.util.ServerInfo' | |
, carryon = true | |
, split = true | |
, version = { | |
'#': 'ok', | |
file_date: new Date().toISOString(), | |
lib: 'git-version.js' | |
}; | |
if (matchArgv('--all')) { | |
version.extend = { | |
node_version: process.version, | |
npm_version: exec('npm --version', null, carryon), | |
maven_version: exec('mvn --version', split, carryon), | |
tomcat_version: exec(tomcat, split, carryon), | |
git_version: exec('git --version').replace(/(git )?(version )?/, '') | |
}; | |
} | |
version.describe = exec('git describe --tags', null, carryon); | |
version.branch = exec('git rev-parse --abbrev-ref HEAD'); | |
version.origin = exec('git config --get remote.origin.url'); | |
version.url = version.origin.replace(/git@/, 'https://').replace('.com:', '.com/'); | |
git.log([ '-1' ], function (err, data) { | |
handleError(err, 'git.log'); | |
var log = data.latest; | |
version.commit = log.hash; | |
version.date = log.date; | |
version.message = log.message; | |
version.author = '%s <%m>'.replace(/%s/, log.author_name).replace(/%m/, log.author_email); | |
}) | |
.then(function () { | |
fs.writeFileSync(json_file, JSON.stringify(version, null, '\t')); | |
console.error('File written: version.json'); | |
}); | |
// === Utilities === | |
function exec(command, split, carryon) { | |
var out; | |
try { | |
out = execSync(command).toString('utf-8').replace(/\n$/, ''); | |
} catch (ex) { | |
console.error(ex.name + ': ' + ex.message); | |
if (! carryon) { | |
process.exit(1); | |
} | |
} | |
return splitSplit(out, split); | |
} | |
function splitSplit(out, split) { | |
var i, t2, temp = out.split(/\n/), result = {}; | |
if (split && out) { | |
for (i = 0; i < temp.length; i++) { | |
t2 = temp[ i ].split(/: /); | |
if (t2[ 1 ]) { | |
result[ t2[ 0 ] ] = t2[ 1 ].trim(); | |
} else { | |
result[ '0' ] = temp[ i ]; | |
} | |
} | |
} | |
return split && out ? result : out; | |
} | |
function handleError(err, where) { | |
if (err) { | |
console.error("ERROR! Where: " + (where || '?')); | |
console.error(err); | |
process.exit(1); | |
} | |
} | |
function matchArgv(pattern) { | |
var size = process.argv.length; | |
return size > 2 && process.argv[ size - 1 ].match(pattern); | |
} | |
//End. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment