Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ $ // Go to your repo
$ node-git-version
$ cat version.js

module.exports = {
tag: '1.0.0'
hash: 'e037765'
export const version = {
tag: '1.0.0',
hash: 'e037765',
timestamp: 1425721222
};
```
48 changes: 18 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
* Date: March 2015
*/

var fs = require('fs');
var exec = require('child_process').exec;
var child = exec('git reflog --decorate -1', function (error, stdout, stderr)
{
if (error)
{
var fs = require('fs');
var exec = require('child_process').exec;
var child = exec('git reflog --decorate -1', function (error, stdout, stderr) {
if (error) {
// Shit
console.log('[FAILED]: Failed to run Git command');
process.exit(1);
Expand All @@ -28,43 +26,33 @@
// Example output: a32d6d8 (HEAD, tag: TAG-V.02, tag: TAG-V.01, master) HEAD@{0}: commit (initial): Asd
// Run regular expression to extract sha and tag
var sha = stdout.match(/[a-z0-9]+\s\(HEAD/g);
if (sha && sha.length > 0)
{
if (sha && sha.length > 0) {
sha = sha[0].slice(0, -6);
}

var tag = stdout.match(/tag\:\s[a-zA-Z0-9\-\.]+\,/g);
if (tag && tag.length > 0)
{
if (tag && tag.length > 0) {
tag = tag[0].slice(5, -1);
}

// Compose version file info
var versionInfo = 'module.exports = {';

if (tag)
{
versionInfo += '\n\ttag: \'' + tag + '\',';
}
else
{
versionInfo += '\n\ttag: null,';
}

versionInfo += '\n\thash: \'' + sha + '\',';
versionInfo += '\n\ttimestamp: ' + Math.floor(new Date().getTime()/1000);
versionInfo += '\n};\n';
const versionInfo = {
tag: tag ? tag : null,
hash: sha,
timestamp: Math.floor(new Date().getTime() / 1000)
};

// Create version.js file
fs.writeFile('version.js', versionInfo, function(err)
{
if(err)
{
fs.writeFile('version.js', buildModuleString(versionInfo), function (err) {
if (err) {
console.log('[FAILED]: can\'t create version.js file. Permission issue?');
}
else
{
else {
console.log('[OK]');
}
});
});

function buildModuleString(versionInfo) {
return 'export const version = ' + JSON.stringify(versionInfo) + ';';
}