1+ /**
2+ * This module creates version.js file in current directory
3+ * containing object with following information:
4+ * tag - Git tag attached to HEAD (if any)
5+ * hash - SHA-1 hash of HEAD
6+ * timestamp - current timestamp
7+ *
8+ * This information is presented as Node.js module and can be used
9+ * by Node.js app for getting version information
10+ *
11+ * Created: Maxim Stepanov
12+ * Date: March 2015
13+ */
14+
15+ var fs = require ( 'fs' ) ;
16+ var exec = require ( 'child_process' ) . exec ;
17+ var child = exec ( 'git reflog --decorate -1' , function ( error , stdout , stderr )
18+ {
19+ if ( error )
20+ {
21+ // Shit
22+ console . log ( '[FAILED]: Failed to run Git command' ) ;
23+ process . exit ( 1 ) ;
24+ }
25+
26+ // Example output: a32d6d8 (HEAD, tag: TAG-V.02, tag: TAG-V.01, master) HEAD@{0}: commit (initial): Asd
27+ // Run regular expression to extract sha and tag
28+ var sha = stdout . match ( / [ a - z 0 - 9 ] + \s \( H E A D / g) ;
29+ if ( sha && sha . length > 0 )
30+ {
31+ sha = sha [ 0 ] . slice ( 0 , - 6 ) ;
32+ }
33+
34+ var tag = stdout . match ( / t a g \: \s [ a - z A - Z 0 - 9 \- \. ] + \, / g) ;
35+ if ( tag && tag . length > 0 )
36+ {
37+ tag = tag [ 0 ] . slice ( 5 , - 1 ) ;
38+ }
39+
40+ // Compose version file info
41+ var versionInfo = 'module.exports = {' ;
42+
43+ if ( tag )
44+ {
45+ versionInfo += '\n\ttag: \'' + tag + '\'' ;
46+ }
47+ else
48+ {
49+ versionInfo += '\n\ttag: null' ;
50+ }
51+
52+ versionInfo += '\n\thash: \'' + sha + '\'' ;
53+ versionInfo += '\n\ttimestamp: ' + Math . floor ( new Date ( ) . getTime ( ) / 1000 ) ;
54+ versionInfo += '\n};\n' ;
55+
56+ // Create version.js file
57+ fs . writeFile ( 'version.js' , versionInfo , function ( err )
58+ {
59+ if ( err )
60+ {
61+ console . log ( '[FAILED]: can\'t create version.js file. Permission issue?' ) ;
62+ }
63+ else
64+ {
65+ console . log ( '[OK]' ) ;
66+ }
67+ } ) ;
68+ } ) ;
0 commit comments