Skip to content

Commit 97dfcff

Browse files
committed
Initial version
0 parents  commit 97dfcff

5 files changed

Lines changed: 134 additions & 0 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/*
2+
._*
3+
version.js

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Prizma
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## node-git-version
2+
3+
This module creates version.js file in current directory containing object with following information:
4+
5+
*tag* - Git tag attached to HEAD (if any)
6+
*hash* - SHA-1 hash of HEAD
7+
*timestamp* - current timestamp
8+
9+
This information is presented as Node.js module and can be used by Node.js app for getting version information
10+
11+
## Usage
12+
13+
```
14+
npm install node-git-version
15+
node ./node_modules/node-git-version/index.js
16+
cat version.js
17+
```

index.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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-z0-9]+\s\(HEAD/g);
29+
if (sha && sha.length > 0)
30+
{
31+
sha = sha[0].slice(0, -6);
32+
}
33+
34+
var tag = stdout.match(/tag\:\s[a-zA-Z0-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+
});

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "node-git-version",
3+
"description": "Node.js application for creating version file of git repository",
4+
"version": "0.1.0",
5+
"main": "index.js",
6+
"bin": "index.js",
7+
"dependencies":
8+
{
9+
},
10+
"keywords": [
11+
"git",
12+
"nodejs",
13+
"node.js",
14+
"version",
15+
"prizma"
16+
],
17+
"author": "maxxie <[email protected]> (https://prizma.mobi)",
18+
"license": "MIT",
19+
"repository" :
20+
{
21+
"type" : "git",
22+
"url" : "https://github.com/prizma/node-git-version.git"
23+
}
24+
}

0 commit comments

Comments
 (0)