git-revision-webpack-plugin
Advanced tools
Comparing version 2.3.1 to 2.4.0
var buildFile = require('./build-file') | ||
var runGitCommand = require('./helpers/run-git-command') | ||
var commithashCommand = 'rev-parse HEAD' | ||
var versionCommand = 'describe --always' | ||
var COMMITHASH_COMMAND = 'rev-parse HEAD' | ||
var VERSION_COMMAND = 'describe --always' | ||
function GitRevisionPlugin (options) { | ||
this.gitWorkTree = options && options.gitWorkTree | ||
this.lightweightTags = options && options.lightweightTags || false | ||
options = options || {} | ||
this.gitWorkTree = options.gitWorkTree | ||
this.commithashCommand = options.commithashCommand || | ||
COMMITHASH_COMMAND | ||
this.versionCommand = options.versionCommand || | ||
VERSION_COMMAND + (options.lightweightTags ? ' --tags' : '') | ||
if (options.versionCommand && options.lightweightTags) { | ||
throw new Error('lightweightTags can\'t be used together versionCommand') | ||
} | ||
} | ||
GitRevisionPlugin.prototype.apply = function (compiler) { | ||
buildFile(compiler, this.gitWorkTree, commithashCommand, /\[git-revision-hash\]/gi, 'COMMITHASH') | ||
buildFile(compiler, this.gitWorkTree, versionCommand + (this.lightweightTags ? ' --tags' : ''), /\[git-revision-version\]/gi, 'VERSION') | ||
buildFile( | ||
compiler, | ||
this.gitWorkTree, | ||
this.commithashCommand, | ||
/\[git-revision-hash\]/gi, | ||
'COMMITHASH' | ||
) | ||
buildFile( | ||
compiler, | ||
this.gitWorkTree, | ||
this.versionCommand, | ||
/\[git-revision-version\]/gi, | ||
'VERSION' | ||
) | ||
} | ||
GitRevisionPlugin.prototype.commithash = function (callback) { | ||
return runGitCommand(this.gitWorkTree, commithashCommand) | ||
GitRevisionPlugin.prototype.commithash = function () { | ||
return runGitCommand( | ||
this.gitWorkTree, | ||
this.commithashCommand | ||
) | ||
} | ||
GitRevisionPlugin.prototype.version = function (callback) { | ||
return runGitCommand(this.gitWorkTree, versionCommand + (this.lightweightTags ? ' --tags' : '')) | ||
GitRevisionPlugin.prototype.version = function () { | ||
return runGitCommand( | ||
this.gitWorkTree, | ||
this.versionCommand | ||
) | ||
} | ||
module.exports = GitRevisionPlugin |
@@ -1,149 +0,75 @@ | ||
/* global describe, beforeEach, it */ | ||
/* global describe, it */ | ||
var rewire = require('rewire') | ||
var sinon = require('sinon') | ||
var expect = require('chai').expect | ||
var webpack = require('webpack') | ||
var fs = require('fs-extra') | ||
var path = require('path') | ||
var GitRevisionPlugin = require('.') | ||
var sourceProject = path.join(__dirname, '../fixtures/project') | ||
var sourceGitRepository = path.join(__dirname, '../fixtures/git-repository') | ||
var GitRevisionPlugin = rewire('.') | ||
var targetProject = path.join(__dirname, '../tmp/project') | ||
var targetProjectConfig = path.join(targetProject, 'webpack.config.js') | ||
var targetGitRepository = path.join(__dirname, '../tmp/project/.git') | ||
describe('git-revision-webpack-plugin (unit)', function () { | ||
describe('on setting custom commithash command', function () { | ||
it('should run the build on .apply', function () { | ||
var buildFile = sinon.spy() | ||
GitRevisionPlugin.__set__('buildFile', buildFile) | ||
var targetBuild = path.join(__dirname, '../tmp/build') | ||
new GitRevisionPlugin({ | ||
commithashCommand: 'custom commithash command' | ||
}).apply() | ||
describe('git-revision-webpack-plugin', function () { | ||
beforeEach(function (done) { | ||
fs.emptyDirSync(targetProject) | ||
fs.copySync(sourceProject, targetProject) | ||
var commithashCall = buildFile.args.find(function (calls) { | ||
return calls[4] === 'COMMITHASH' | ||
}) | ||
fs.emptyDirSync(targetGitRepository) | ||
fs.copySync(sourceGitRepository, targetGitRepository) | ||
fs.remove(targetBuild) | ||
var config = require(targetProjectConfig) | ||
config.context = targetProject | ||
config.output.path = targetBuild | ||
config.plugins = [ | ||
new GitRevisionPlugin({ gitWorkTree: targetProject }) | ||
] | ||
webpack(config, function () { | ||
done() | ||
expect(commithashCall[2]).to.eql('custom commithash command') | ||
}) | ||
}) | ||
it('should create the VERSION file', function () { | ||
var versionPath = path.join(targetBuild, 'VERSION') | ||
var VERSION = fs.readFileSync(versionPath) | ||
it('should run the custom git command on .commithash', function () { | ||
var runGitCommand = sinon.spy() | ||
GitRevisionPlugin.__set__('runGitCommand', runGitCommand) | ||
expect(VERSION.toString()).to.eql('v1.0.0-1-g9a15b3b') | ||
}) | ||
new GitRevisionPlugin({ | ||
commithashCommand: 'custom commithash command' | ||
}).commithash() | ||
it('should create the COMMITHASH file', function () { | ||
var versionPath = path.join(targetBuild, 'COMMITHASH') | ||
var COMMITHASH = fs.readFileSync(versionPath) | ||
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2') | ||
}) | ||
describe('[git-revision-version] and [git-revision-hash] templates', function () { | ||
it('should support templates in the output.filename', function () { | ||
var versionPath = path.join(targetBuild, 'main-v1.0.0-1-g9a15b3b.js') | ||
fs.readFileSync(versionPath) | ||
expect(runGitCommand.args[0][1]).to.eql('custom commithash command') | ||
}) | ||
it('should support setting the public path', function () { | ||
var versionPath = path.join(targetBuild, 'main-v1.0.0-1-g9a15b3b.js') | ||
var mainJs = fs.readFileSync(versionPath) | ||
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/v1.0.0-1-g9a15b3b/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";' | ||
expect(mainJs.indexOf(expectedPublicPath) !== -1).to.eql(true) | ||
}) | ||
}) | ||
describe('public API', () => { | ||
it('should expose the commithash', () => { | ||
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject }) | ||
expect(plugin.commithash()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2') | ||
describe('on setting custom version command', function () { | ||
it('should prevent setting lightweightTags flag', function () { | ||
expect(function () { | ||
/* eslint no-new: 0 */ | ||
new GitRevisionPlugin({ | ||
versionCommand: 'custom version command', | ||
lightweightTags: true | ||
}) | ||
}).to.throw('lightweightTags can\'t be used together versionCommand') | ||
}) | ||
it('should expose the version', () => { | ||
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject }) | ||
expect(plugin.version()).to.eql('v1.0.0-1-g9a15b3b') | ||
}) | ||
}) | ||
}) | ||
it('should run the build on .apply', function () { | ||
var buildFile = sinon.spy() | ||
GitRevisionPlugin.__set__('buildFile', buildFile) | ||
describe('git-revision-webpack-plugin with lightweightTags option', function () { | ||
beforeEach(function (done) { | ||
fs.emptyDirSync(targetProject) | ||
fs.copySync(sourceProject, targetProject) | ||
new GitRevisionPlugin({ | ||
versionCommand: 'custom version command' | ||
}).apply() | ||
fs.emptyDirSync(targetGitRepository) | ||
fs.copySync(sourceGitRepository, targetGitRepository) | ||
var commithashCall = buildFile.args.find(function (calls) { | ||
return calls[4] === 'VERSION' | ||
}) | ||
fs.remove(targetBuild) | ||
var config = require(targetProjectConfig) | ||
config.context = targetProject | ||
config.output.path = targetBuild | ||
config.plugins = [ | ||
new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true }) | ||
] | ||
webpack(config, function () { | ||
done() | ||
expect(commithashCall[2]).to.eql('custom version command') | ||
}) | ||
}) | ||
it('should create the VERSION file', function () { | ||
var versionPath = path.join(targetBuild, 'VERSION') | ||
var VERSION = fs.readFileSync(versionPath) | ||
it('should run the custom git command on .version', function () { | ||
var runGitCommand = sinon.spy() | ||
GitRevisionPlugin.__set__('runGitCommand', runGitCommand) | ||
expect(VERSION.toString()).to.eql('v2.0.0-beta') | ||
}) | ||
new GitRevisionPlugin({ | ||
versionCommand: 'custom version command' | ||
}).version() | ||
it('should create the COMMITHASH file', function () { | ||
var versionPath = path.join(targetBuild, 'COMMITHASH') | ||
var COMMITHASH = fs.readFileSync(versionPath) | ||
expect(COMMITHASH.toString()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2') | ||
}) | ||
describe('[git-revision-version] and [git-revision-hash] templates', function () { | ||
it('should support templates in the output.filename', function () { | ||
var versionPath = path.join(targetBuild, 'main-v2.0.0-beta.js') | ||
fs.readFileSync(versionPath) | ||
expect(runGitCommand.args[0][1]).to.eql('custom version command') | ||
}) | ||
it('should support setting the public path', function () { | ||
var versionPath = path.join(targetBuild, 'main-v2.0.0-beta.js') | ||
var mainJs = fs.readFileSync(versionPath) | ||
var expectedPublicPath = '__webpack_require__.p = "http://cdn.com/assets/v2.0.0-beta/9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2";' | ||
expect(mainJs.indexOf(expectedPublicPath) !== -1).to.eql(true) | ||
}) | ||
}) | ||
describe('public API', () => { | ||
it('should expose the commithash', () => { | ||
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true }) | ||
expect(plugin.commithash()).to.eql('9a15b3ba1f8c347f9db94bcfde9630ed4fdeb1b2') | ||
}) | ||
it('should expose the version', () => { | ||
var plugin = new GitRevisionPlugin({ gitWorkTree: targetProject, lightweightTags: true }) | ||
expect(plugin.version()).to.eql('v2.0.0-beta') | ||
}) | ||
}) | ||
}) | ||
{ | ||
"name": "git-revision-webpack-plugin", | ||
"version": "2.3.1", | ||
"version": "2.4.0", | ||
"description": "Simple webpack plugin that generates VERSION and COMMITHASH files during build", | ||
@@ -37,2 +37,4 @@ "main": "lib/index.js", | ||
"mocha": "^2.4.5", | ||
"rewire": "^2.5.2", | ||
"sinon": "^1.17.5", | ||
"standard": "^6.0.5", | ||
@@ -39,0 +41,0 @@ "webpack": "^1.13.0" |
@@ -42,2 +42,8 @@ # Git Revision Webpack Plugin | ||
## Configuration | ||
The plugin requires no configuration by default, but it is possible to configure it to support custom git workflows. | ||
### `lightweightTags: false` | ||
If you need [lightweight tags](https://git-scm.com/book/en/v2/Git-Basics-Tagging#Lightweight-Tags) support, you may turn on `lighweithTags` option in this way: | ||
@@ -50,3 +56,5 @@ | ||
plugins: [ | ||
new GitRevisionPlugin({ lightweightTags: true }) | ||
new GitRevisionPlugin({ | ||
lightweightTags: true | ||
}) | ||
] | ||
@@ -56,4 +64,36 @@ } | ||
### Path Substitutions | ||
### `commithashCommand: 'rev-parse HEAD'` | ||
To change the default `git` command used to read the value of `COMMITHASH`: | ||
```javascript | ||
var GitRevisionPlugin = require('git-revision-webpack-plugin') | ||
module.exports = { | ||
plugins: [ | ||
new GitRevisionPlugin({ | ||
commithashCommand: 'rev-list --max-count=1 --no-merges HEAD' | ||
}) | ||
] | ||
} | ||
``` | ||
### `versionCommand: 'describe --always'` | ||
To change the default `git` command used to read the value of `VERSION`: | ||
```javascript | ||
var GitRevisionPlugin = require('git-revision-webpack-plugin') | ||
module.exports = { | ||
plugins: [ | ||
new GitRevisionPlugin({ | ||
versionCommand: 'describe --always --tags --dirty' | ||
}) | ||
] | ||
} | ||
``` | ||
## Path Substitutions | ||
It is also possible to use two [path substituitions](http://webpack.github.io/docs/configuration.html#output-filename) on build to get either the revision or version as part of output paths. | ||
@@ -75,3 +115,3 @@ | ||
### Public API | ||
## Plugin API | ||
@@ -78,0 +118,0 @@ The `VERSION` and `COMMITHASH` are also exposed through a public API. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15788
9
269
130
8
3