Skip to content

Commit ce19c37

Browse files
committed
Merge pull request #5 from Nevon/expose-commithash-and-version
Expose commithash and version
2 parents 2d364d5 + a7f74de commit ce19c37

5 files changed

Lines changed: 74 additions & 19 deletions

File tree

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,21 @@ module.exports = {
5656
}
5757
}
5858
```
59+
60+
### Public API
61+
62+
The `VERSION` and `COMMITHASH` are also exposed through a public API.
63+
64+
Example:
65+
66+
```javascript
67+
var gitRevisionPlugin = new GitRevisionPlugin()
68+
module.exports = {
69+
plugins: [
70+
new DefinePlugin({
71+
'VERSION': JSON.stringify(gitRevisionPlugin.version()),
72+
'COMMITHASH': JSON.stringify(gitRevisionPlugin.commithash()),
73+
})
74+
]
75+
}
76+
```

lib/build-file.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
1-
var path = require('path')
2-
var exec = require('child_process').exec
3-
var removeEmptyLines = require('./helpers/remove-empty-lines.js')
1+
var runGitCommand = require('./helpers/run-git-command')
42

53
module.exports = function buildFile (compiler, gitWorkTree, command, replacePattern, asset) {
64
var data = ''
75

86
compiler.plugin('compilation', function (compilation) {
9-
var gitCommand = gitWorkTree
10-
? [
11-
'git',
12-
'--git-dir=' + path.join(gitWorkTree, '.git'),
13-
'--work-tree=' + gitWorkTree,
14-
command
15-
].join(' ')
16-
: [
17-
'git',
18-
command
19-
].join(' ')
20-
217
compilation.plugin('optimize-tree', function (chunks, modules, callback) {
22-
exec(gitCommand, function (err, stdout) {
8+
runGitCommand(gitWorkTree, command, function (err, res) {
239
if (err) { return callback(err) }
24-
data = removeEmptyLines(stdout)
10+
data = res
2511

2612
callback()
2713
})

lib/helpers/run-git-command.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
var exec = require('child_process').exec
2+
var execSync = require('child_process').execSync
3+
var path = require('path')
4+
var removeEmptyLines = require('./remove-empty-lines')
5+
6+
module.exports = function (gitWorkTree, command, callback) {
7+
var gitCommand = gitWorkTree
8+
? [
9+
'git',
10+
'--git-dir=' + path.join(gitWorkTree, '.git'),
11+
'--work-tree=' + gitWorkTree,
12+
command
13+
].join(' ')
14+
: [
15+
'git',
16+
command
17+
].join(' ')
18+
19+
if (callback) {
20+
exec(gitCommand, function (err, stdout) {
21+
if (err) { return callback(err) }
22+
callback(null, removeEmptyLines(stdout))
23+
})
24+
} else {
25+
return removeEmptyLines('' + execSync(gitCommand))
26+
}
27+
}

lib/index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
var buildFile = require('./build-file')
2+
var runGitCommand = require('./helpers/run-git-command')
3+
4+
var commithashCommand = 'rev-parse HEAD'
5+
var versionCommand = 'describe --always'
26

37
function GitRevisionPlugin (options) {
48
this.gitWorkTree = options && options.gitWorkTree
59
}
610

711
GitRevisionPlugin.prototype.apply = function (compiler) {
8-
buildFile(compiler, this.gitWorkTree, 'rev-parse HEAD', /\[git-revision-hash\]/gi, 'COMMITHASH')
9-
buildFile(compiler, this.gitWorkTree, 'describe --always', /\[git-revision-version\]/gi, 'VERSION')
12+
buildFile(compiler, this.gitWorkTree, commithashCommand, /\[git-revision-hash\]/gi, 'COMMITHASH')
13+
buildFile(compiler, this.gitWorkTree, versionCommand, /\[git-revision-version\]/gi, 'VERSION')
14+
}
15+
16+
GitRevisionPlugin.prototype.commithash = function (callback) {
17+
return runGitCommand(this.gitWorkTree, commithashCommand)
18+
}
19+
20+
GitRevisionPlugin.prototype.version = function (callback) {
21+
return runGitCommand(this.gitWorkTree, versionCommand)
1022
}
1123

1224
module.exports = GitRevisionPlugin

lib/index.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,16 @@ describe('git-revision-webpack-plugin', function () {
6767
expect(mainJs.indexOf(expectedPublicPath) !== -1).to.eql(true)
6868
})
6969
})
70+
71+
describe('public API', () => {
72+
it('should expose the commithash', () => {
73+
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject })
74+
expect(plugin.commithash()).to.eql('10e1ff4c17ad1f12241b5c4d9a708a76e98289d8')
75+
})
76+
77+
it('should expose the version', () => {
78+
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject })
79+
expect(plugin.version()).to.eql('v1.0.0')
80+
})
81+
})
7082
})

0 commit comments

Comments
 (0)