Skip to content

Restructure to use ember-cli-deploy-plugin, and complete rename to em… #10

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

Merged
Merged
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
93 changes: 29 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,83 +3,48 @@

var Promise = require('ember-cli/lib/ext/promise');

var chalk = require('chalk');
var blue = chalk.blue;
var red = chalk.red;

var validateConfig = require('./lib/utilities/validate-config');
var DeployPluginBase = require('ember-cli-deploy-plugin');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lukemelia, I may be having a massive brain fart but where does ember-cli-deploy-plugin come from? I can't see it in the ember-cli-deploy/ember-cli-deploy#180 which is where I would have expected it to be.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


module.exports = {
name: 'ember-cli-deploy-revision-key',

createDeployPlugin: function(options) {
var generators = require('./lib/key-generators');

function _beginMessage(ui, type) {
ui.write(blue('| '));
ui.writeLine(blue('- generating revision key using `' + type + '`'));

return Promise.resolve();
}

function _successMessage(ui, key) {
ui.write(blue('| '));
ui.writeLine(blue('- generated revision key: `' + key + '`'));

return Promise.resolve(key);
}

function _errorMessage(ui, error) {
ui.write(blue('| '));
ui.write(red('- ' + error + '`\n'));

return Promise.reject(error);
}

return {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,

configure: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] = deployment.config[this.name] || {};

return this._resolvePipelineData(config, context)
.then(validateConfig.bind(this, ui, config));
defaultConfig: {
type: 'file-hash',
filePattern: 'index.html',
distDir: function(context) {
return context.distDir;
},
distFiles: function(context) {
return context.distFiles;
},
},

didBuild: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] = deployment.config[this.name] || {};
var type = config.type;

var KeyGenerator = generators[type];
var self = this;
var type = this.readConfig('type');
var KeyGenerator = require('./lib/key-generators')[type];
var keyGenerator = new KeyGenerator({
config: config,
context: context
plugin: this
});

return _beginMessage(ui, type)
.then(keyGenerator.generate.bind(keyGenerator))
.then(_successMessage.bind(this, ui))
.then(function(value) {
return { revisionKey: value };
this.log('creating revision key using `' + type + '`');
return keyGenerator.generate()
.then(function(revisionKey) {
self.log('generated revision key: `' + revisionKey + '`');
return revisionKey;
})
.then(function(revisionKey) {
return { revisionKey: revisionKey };
})
.catch(_errorMessage.bind(this, ui));
.catch(this._errorMessage.bind(this));
},

_resolvePipelineData: function(config, context) {
config.distDir = config.distDir || function(context) {
return context.distDir;
};

config.distFiles = config.distFiles || function(context) {
return context.distFiles;
};

return Promise.resolve();
_errorMessage: function(error) {
this.log(error, { color: 'red' });
return Promise.reject(error);
}
};
});
return new DeployPlugin();
}
};
9 changes: 4 additions & 5 deletions lib/key-generators/file-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ var readFile = denodeify(fs.readFile);

module.exports = CoreObject.extend({
init: function(options) {
this._context = options.context || {};
this._config = options.config || {};
this._plugin = options.plugin;
},

generate: function() {
var filePattern = this._config.filePattern;
var distDir = this._resolveConfigValue('distDir', this._config, this._context);
var distFiles = this._resolveConfigValue('distFiles', this._config, this._context);
var filePattern = this._plugin.readConfig('filePattern');
var distDir = this._plugin.readConfig('distDir');
var distFiles = this._plugin.readConfig('distFiles');
var fingerprint;

var filePaths = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
Expand Down
29 changes: 0 additions & 29 deletions lib/utilities/validate-config.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"dependencies": {
"chalk": "^1.0.0",
"core-object": "^1.1.0",
"ember-cli-deploy-plugin": "0.1.1",
"ember-cli-babel": "^5.0.0",
"minimatch": "^2.0.4",
"rsvp": "^3.0.18"
Expand Down
141 changes: 76 additions & 65 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

var assert = require('ember-cli/tests/helpers/assert');

function hooks(plugin) {
return Object.keys(plugin).filter(function(key) {
return (key !== 'name') && (key.charAt(0) !== '_') && (typeof plugin[key] === 'function');
});
}

describe('the index', function() {
var subject;
var subject, mockUi;

before(function() {
beforeEach(function() {
subject = require('../../index');
mockUi = {
messages: [],
write: function() { },
writeLine: function(message) {
this.messages.push(message);
}
};
});

it('has a name', function() {
Expand All @@ -28,8 +29,8 @@ describe('the index', function() {
name: 'test-plugin'
});

assert.equal(hooks(plugin).length, 2);
assert.sameMembers(hooks(plugin), ['configure', 'didBuild']);
assert.typeOf(plugin.configure, 'function');
assert.typeOf(plugin.didBuild, 'function');
});

describe('configure hook', function() {
Expand All @@ -39,83 +40,93 @@ describe('the index', function() {
});

var context = {
deployment: {
ui: {
write: function() {},
writeLine: function() {}
},
config: {
'revision-key': {
type: 'file-hash',
filePattern: 'eeee'
}
ui: mockUi,
config: {
"revision-key": {
type: 'file-hash',
filePattern: 'eeee'
}
}
};

return assert.isFulfilled(plugin.configure.call(plugin, context))
plugin.beforeHook(context);
plugin.configure(context);
assert.ok(true); // it didn't throw
});
it('warns about missing optional config', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-key'
});

describe('resolving data from the pipeline', function() {
it('resolves the config data from the context', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-key'
});
var context = {
ui: mockUi,
config: {
"revision-key": {
}
}
};

var config = {
type: 'file-hash',
filePattern: 'eeee'
};
var context = {
deployment: {
ui: {
write: function() {},
writeLine: function() {}
},
config: {
'revision-key': config
}
},
plugin.beforeHook(context);
plugin.configure(context);

var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}

distDir: 'some-dir',
distFiles: ['a.js', 'b.css']
};

return assert.isFulfilled(plugin.configure.call(plugin, context))
.then(function() {
assert.typeOf(config.distDir, 'function');
assert.typeOf(config.distFiles, 'function');
assert.equal(config.distDir(context), 'some-dir');
assert.sameMembers(config.distFiles(context), ['a.js', 'b.css']);
});
return previous;
}, []);

assert.equal(messages.length, 4);
});

it('adds default config to the config object', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-key'
});

var context = {
ui: mockUi,
config: {
"revision-key": {
}
}
};

plugin.beforeHook(context);
plugin.configure(context);

assert.isDefined(context.config['revision-key'].type);
assert.isDefined(context.config['revision-key'].filePattern);
});
});

describe('didBuild hook', function() {
it ('returns the revision key data', function() {
it('returns the revisionKey', function() {
var plugin = subject.createDeployPlugin({
name: 'revision-key'
});

var context = {
deployment: {
ui: {
write: function() {},
writeLine: function() {}
},
config: {
'revision-key': {
type: 'file-hash',
filePattern: 'index.html',
distDir: 'tests/fixtures',
distFiles: ['index.html'],
distDir: 'tests/fixtures',
distFiles: ['index.html'],
ui: mockUi,
config: {
"revision-key": {
type: 'file-hash',
filePattern: 'index.html',
distDir: function(context) {
return context.distDir;
},
}
distFiles: function(context) {
return context.distFiles;
}
},
}
};
plugin.beforeHook(context);

return assert.isFulfilled(plugin.didBuild.call(plugin, context))
return assert.isFulfilled(plugin.didBuild(context))
.then(function(result) {
assert.equal(result.revisionKey, 'ae1569f72495012cd5e8588e0f2f5d49');
});
Expand Down
Loading