diff --git a/README.md b/README.md index 40cd32d..8dea720 100644 --- a/README.md +++ b/README.md @@ -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 }; ``` diff --git a/index.js b/index.js index 4bd362c..f793cfe 100644 --- a/index.js +++ b/index.js @@ -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); @@ -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) + ';'; +} \ No newline at end of file