From 1bfe10708d05bace6a4ba56545e86f96527eb98e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Thu, 13 Jul 2017 11:48:22 +0200 Subject: [PATCH 1/4] optimize code; changed export in version.js to ES6 export --- index.js | 51 +++++++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 4bd362c..dfbee5d 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,36 @@ // 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) { + const versionInfoString = JSON.stringify(versionInfo); + const moduleString = 'export const version = ' + versionInfoString.substr(1, versionInfoString.length - 2) + ';'; + + return moduleString; +} \ No newline at end of file From d39ef9bd88eec5c60f308825efebd20e6091f5af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Thu, 13 Jul 2017 13:51:16 +0200 Subject: [PATCH 2/4] fix buildModuleString() --- index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/index.js b/index.js index dfbee5d..f9cdf1c 100644 --- a/index.js +++ b/index.js @@ -54,8 +54,5 @@ var child = exec('git reflog --decorate -1', function (error, stdout, stderr) { }); function buildModuleString(versionInfo) { - const versionInfoString = JSON.stringify(versionInfo); - const moduleString = 'export const version = ' + versionInfoString.substr(1, versionInfoString.length - 2) + ';'; - - return moduleString; + return 'export const version = ' + JSON.stringify(versionInfo) + ';';; } \ No newline at end of file From 9f64e5f671805aa5d53793ab2caa76ca8de9a94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Fri, 14 Jul 2017 10:21:40 +0200 Subject: [PATCH 3/4] remove double semicolon --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index f9cdf1c..f793cfe 100644 --- a/index.js +++ b/index.js @@ -54,5 +54,5 @@ var child = exec('git reflog --decorate -1', function (error, stdout, stderr) { }); function buildModuleString(versionInfo) { - return 'export const version = ' + JSON.stringify(versionInfo) + ';';; + return 'export const version = ' + JSON.stringify(versionInfo) + ';'; } \ No newline at end of file From 0be0a8f35e28d1ca0384b8ee7605a44054b30798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schimmelpfennig?= Date: Fri, 14 Jul 2017 11:02:21 +0200 Subject: [PATCH 4/4] update readme --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 }; ```