Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom generators #57

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ For detailed information on how configuration of plugins works, please refer to
```
ENV["revision-data"] = {
type: 'file-hash',
generator: null,
scm: function(context) {
return require('./lib/scm-data-generators')['git'];
}
Expand All @@ -64,6 +65,21 @@ The type of [Data Generator](#data-generators) to be used.
*Default:* `'file-hash'`
*Alternatives:* `'git-tag-commit'`, `'git-commit'`, `'version-commit'`

### generator

Custom generator that can be used to override results from selected [Data Generator](#data-generators).

*Default:* `null`
*Alternative:*
```js
function (data) {
return Object.assign({}, data, { revisionKey: 'custom-generated-key' });
}
```

Parameter `data` contains result of executing selected Data Generator.


### scm

The type of the [SCM Data Generator](#scm-data-generator) to be used
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
name: options.name,
defaultConfig: {
type: 'file-hash',
generator: null,
separator: '+',
filePattern: 'index.html',
versionFile: 'package.json',
Expand All @@ -31,6 +32,7 @@ module.exports = {

prepare: function(/*context*/) {
var self = this;
var customGenerator = this.pluginConfig['generator'];

var promises = {
data: this._getData(),
Expand All @@ -41,6 +43,11 @@ module.exports = {
.then(function(results) {
var data = results.data;
data.scm = results.scm;

if (customGenerator && typeof customGenerator === 'function') {
data = customGenerator(data);
}

self.log('generated revision data for revision: `' + data.revisionKey + '`', { verbose: true });
return data;
})
Expand All @@ -54,6 +61,7 @@ module.exports = {
var type = this.readConfig('type');
this.log('creating revision data using `' + type + '`', { verbose: true });
var DataGenerator = require('./lib/data-generators')[type];

return new DataGenerator({
plugin: this
}).generate();
Expand Down
42 changes: 41 additions & 1 deletion tests/unit/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ describe('the index', function() {
plugin.configure(context);
assert.ok(true); // it didn't throw
});

it('warns about missing optional config', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-data'
Expand All @@ -78,7 +79,7 @@ describe('the index', function() {
return previous;
}, []);

assert.equal(messages.length, 7);
assert.equal(messages.length, 8);
});

it('adds default config to the config object', function() {
Expand All @@ -100,6 +101,7 @@ describe('the index', function() {
assert.isDefined(context.config['revision-data'].type);
assert.isDefined(context.config['revision-data'].filePattern);
assert.isDefined(context.config['revision-data'].scm);
assert.isDefined(context.config['revision-data'].generator);
});
});

Expand Down Expand Up @@ -138,5 +140,43 @@ describe('the index', function() {
assert.isNotNull(result.revisionData.scm.email);
});
});

it('return the revisionData using custom generator', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-data'
});

var context = {
distDir: 'tests/fixtures',
distFiles: ['index.html'],
ui: mockUi,
config: {
"revision-data": {
type: 'file-hash',
generator: function(data) {
assert.equal(data.revisionKey, 'ae1569f72495012cd5e8588e0f2f5d49');
return Object.assign({}, data, { revisionKey: 'custom-key'});
},
filePattern: 'index.html',
scm: function(/* context */) {
return require('../../lib/scm-data-generators')['git'];
},
distDir: function(context) {
return context.distDir;
},
distFiles: function(context) {
return context.distFiles;
}
},
}
};

plugin.beforeHook(context);

return assert.isFulfilled(plugin.prepare(context))
.then(function(result) {
assert.equal(result.revisionData.revisionKey, 'custom-key');
});
});
});
});