Skip to content

Commit 057e0dc

Browse files
committed
Add branch flag
1 parent 18624d8 commit 057e0dc

3 files changed

Lines changed: 67 additions & 9 deletions

File tree

lib/index.integration.spec.js

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ describe('git-revision-webpack-plugin (integration)', function () {
3030
config.context = targetProject
3131
config.output.path = targetBuild
3232
config.plugins = [
33-
new GitRevisionPlugin({ gitWorkTree: targetProject })
33+
new GitRevisionPlugin({
34+
gitWorkTree: targetProject,
35+
branch: true
36+
})
3437
]
3538

3639
webpack(config, function () {
@@ -108,7 +111,11 @@ describe('git-revision-webpack-plugin with lightweightTags option', function ()
108111
config.context = targetProject
109112
config.output.path = targetBuild
110113
config.plugins = [
111-
new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true })
114+
new GitRevisionPlugin({
115+
gitWorkTree: targetProject,
116+
lightweightTags: true,
117+
branch: true
118+
})
112119
]
113120

114121
webpack(config, function () {
@@ -170,3 +177,48 @@ describe('git-revision-webpack-plugin with lightweightTags option', function ()
170177
})
171178
})
172179
})
180+
181+
describe('git-revision-webpack-plugin without branch option', function () {
182+
beforeEach(function (done) {
183+
fs.emptyDirSync(targetProject)
184+
fs.copySync(sourceProject, targetProject)
185+
186+
fs.emptyDirSync(targetGitRepository)
187+
fs.copySync(sourceGitRepository, targetGitRepository)
188+
189+
fs.remove(targetBuild)
190+
191+
var config = require(targetProjectConfig)
192+
193+
config.context = targetProject
194+
config.output.path = targetBuild
195+
config.plugins = [
196+
new GitRevisionPlugin({
197+
gitWorkTree: targetProject
198+
})
199+
]
200+
201+
webpack(config, function () {
202+
done()
203+
})
204+
})
205+
206+
it('should create the VERSION file', function () {
207+
var versionPath = path.join(targetBuild, 'VERSION')
208+
var VERSION = fs.readFileSync(versionPath)
209+
210+
expect(VERSION.toString()).to.eql('v1.0.0-1-g9a15b3b')
211+
})
212+
213+
it('should create the COMMITHASH file', function () {
214+
var versionPath = path.join(targetBuild, 'COMMITHASH')
215+
var COMMITHASH = fs.readFileSync(versionPath)
216+
217+
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2')
218+
})
219+
220+
it('should not create the BRANCH file', function () {
221+
var branchPath = path.join(targetBuild, 'BRANCH')
222+
expect(fs.existsSync(branchPath)).to.eql(false)
223+
})
224+
})

lib/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ function GitRevisionPlugin (options) {
1616
this.versionCommand = options.versionCommand ||
1717
VERSION_COMMAND + (options.lightweightTags ? ' --tags' : '')
1818

19+
this.createBranchFile = options.branch || false
20+
1921
this.branchCommand = options.branchCommand ||
2022
BRANCH_COMMAND
2123

@@ -41,13 +43,15 @@ GitRevisionPlugin.prototype.apply = function (compiler) {
4143
'VERSION'
4244
)
4345

44-
buildFile(
45-
compiler,
46-
this.gitWorkTree,
47-
this.branchCommand,
48-
/\[git-revision-branch\]/gi,
49-
'BRANCH'
50-
)
46+
if (this.createBranchFile) {
47+
buildFile(
48+
compiler,
49+
this.gitWorkTree,
50+
this.branchCommand,
51+
/\[git-revision-branch\]/gi,
52+
'BRANCH'
53+
)
54+
}
5155
}
5256

5357
GitRevisionPlugin.prototype.commithash = function () {

lib/index.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ describe('git-revision-webpack-plugin (unit)', function () {
7979
GitRevisionPlugin.__set__('buildFile', buildFile)
8080

8181
new GitRevisionPlugin({
82+
branch: true,
8283
branchCommand: 'custom branch command'
8384
}).apply()
8485

@@ -94,6 +95,7 @@ describe('git-revision-webpack-plugin (unit)', function () {
9495
GitRevisionPlugin.__set__('runGitCommand', runGitCommand)
9596

9697
new GitRevisionPlugin({
98+
branch: true,
9799
branchCommand: 'custom branch command'
98100
}).branch()
99101

0 commit comments

Comments
 (0)