Skip to content

Commit 439d4b5

Browse files
committed
Restructure to use ember-cli-deploy-plugin, and complete rename to ember-cli-deploy-revision-key
1 parent 2d99960 commit 439d4b5

File tree

7 files changed

+141
-241
lines changed

7 files changed

+141
-241
lines changed

index.js

+29-64
Original file line numberDiff line numberDiff line change
@@ -3,83 +3,48 @@
33

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

6-
var chalk = require('chalk');
7-
var blue = chalk.blue;
8-
var red = chalk.red;
9-
10-
var validateConfig = require('./lib/utilities/validate-config');
6+
var DeployPluginBase = require('ember-cli-deploy-plugin');
117

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

1511
createDeployPlugin: function(options) {
16-
var generators = require('./lib/key-generators');
17-
18-
function _beginMessage(ui, type) {
19-
ui.write(blue('| '));
20-
ui.writeLine(blue('- generating revision key using `' + type + '`'));
21-
22-
return Promise.resolve();
23-
}
24-
25-
function _successMessage(ui, key) {
26-
ui.write(blue('| '));
27-
ui.writeLine(blue('- generated revision key: `' + key + '`'));
28-
29-
return Promise.resolve(key);
30-
}
31-
32-
function _errorMessage(ui, error) {
33-
ui.write(blue('| '));
34-
ui.write(red('- ' + error + '`\n'));
35-
36-
return Promise.reject(error);
37-
}
38-
39-
return {
12+
var DeployPlugin = DeployPluginBase.extend({
4013
name: options.name,
41-
42-
configure: function(context) {
43-
var deployment = context.deployment;
44-
var ui = deployment.ui;
45-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
46-
47-
return this._resolvePipelineData(config, context)
48-
.then(validateConfig.bind(this, ui, config));
14+
defaultConfig: {
15+
type: 'file-hash',
16+
filePattern: 'index.html',
17+
distDir: function(context) {
18+
return context.distDir;
19+
},
20+
distFiles: function(context) {
21+
return context.distFiles;
22+
},
4923
},
50-
5124
didBuild: function(context) {
52-
var deployment = context.deployment;
53-
var ui = deployment.ui;
54-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
55-
var type = config.type;
56-
57-
var KeyGenerator = generators[type];
25+
var self = this;
26+
var type = this.readConfig('type');
27+
var KeyGenerator = require('./lib/key-generators')[type];
5828
var keyGenerator = new KeyGenerator({
59-
config: config,
60-
context: context
29+
plugin: this
6130
});
6231

63-
return _beginMessage(ui, type)
64-
.then(keyGenerator.generate.bind(keyGenerator))
65-
.then(_successMessage.bind(this, ui))
66-
.then(function(value) {
67-
return { revisionKey: value };
32+
this.log('creating revision key using `' + type + '`');
33+
return keyGenerator.generate()
34+
.then(function(revisionKey) {
35+
self.log('generated revision key: `' + revisionKey + '`');
36+
return revisionKey;
37+
})
38+
.then(function(revisionKey) {
39+
return { revisionKey: revisionKey };
6840
})
69-
.catch(_errorMessage.bind(this, ui));
41+
.catch(this._errorMessage.bind(this));
7042
},
71-
72-
_resolvePipelineData: function(config, context) {
73-
config.distDir = config.distDir || function(context) {
74-
return context.distDir;
75-
};
76-
77-
config.distFiles = config.distFiles || function(context) {
78-
return context.distFiles;
79-
};
80-
81-
return Promise.resolve();
43+
_errorMessage: function(error) {
44+
this.log(error, { color: 'red' });
45+
return Promise.reject(error);
8246
}
83-
};
47+
});
48+
return new DeployPlugin();
8449
}
8550
};

lib/key-generators/file-hash.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ var readFile = denodeify(fs.readFile);
1010

1111
module.exports = CoreObject.extend({
1212
init: function(options) {
13-
this._context = options.context || {};
14-
this._config = options.config || {};
13+
this._plugin = options.plugin;
1514
},
1615

1716
generate: function() {
18-
var filePattern = this._config.filePattern;
19-
var distDir = this._resolveConfigValue('distDir', this._config, this._context);
20-
var distFiles = this._resolveConfigValue('distFiles', this._config, this._context);
17+
var filePattern = this._plugin.readConfig('filePattern');
18+
var distDir = this._plugin.readConfig('distDir');
19+
var distFiles = this._plugin.readConfig('distFiles');
2120
var fingerprint;
2221

2322
var filePaths = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));

lib/utilities/validate-config.js

-29
This file was deleted.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"dependencies": {
4545
"chalk": "^1.0.0",
4646
"core-object": "^1.1.0",
47+
"ember-cli-deploy-plugin": "0.1.0",
4748
"ember-cli-babel": "^5.0.0",
4849
"minimatch": "^2.0.4",
4950
"rsvp": "^3.0.18"

tests/unit/index-nodetest.js

+76-65
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

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

5-
function hooks(plugin) {
6-
return Object.keys(plugin).filter(function(key) {
7-
return (key !== 'name') && (key.charAt(0) !== '_') && (typeof plugin[key] === 'function');
8-
});
9-
}
10-
115
describe('the index', function() {
12-
var subject;
6+
var subject, mockUi;
137

14-
before(function() {
8+
beforeEach(function() {
159
subject = require('../../index');
10+
mockUi = {
11+
messages: [],
12+
write: function() { },
13+
writeLine: function(message) {
14+
this.messages.push(message);
15+
}
16+
};
1617
});
1718

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

31-
assert.equal(hooks(plugin).length, 2);
32-
assert.sameMembers(hooks(plugin), ['configure', 'didBuild']);
32+
assert.typeOf(plugin.configure, 'function');
33+
assert.typeOf(plugin.didBuild, 'function');
3334
});
3435

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

4142
var context = {
42-
deployment: {
43-
ui: {
44-
write: function() {},
45-
writeLine: function() {}
46-
},
47-
config: {
48-
'revision-key': {
49-
type: 'file-hash',
50-
filePattern: 'eeee'
51-
}
43+
ui: mockUi,
44+
config: {
45+
"revision-key": {
46+
type: 'file-hash',
47+
filePattern: 'eeee'
5248
}
5349
}
5450
};
5551

56-
return assert.isFulfilled(plugin.configure.call(plugin, context))
52+
plugin.beforeHook(context);
53+
plugin.configure(context);
54+
assert.ok(true); // it didn't throw
5755
});
56+
it('warns about missing optional config', function() {
57+
var plugin = subject.createDeployPlugin({
58+
name: 'revision-key'
59+
});
5860

59-
describe('resolving data from the pipeline', function() {
60-
it('resolves the config data from the context', function() {
61-
var plugin = subject.createDeployPlugin({
62-
name: 'revision-key'
63-
});
61+
var context = {
62+
ui: mockUi,
63+
config: {
64+
"revision-key": {
65+
}
66+
}
67+
};
6468

65-
var config = {
66-
type: 'file-hash',
67-
filePattern: 'eeee'
68-
};
69-
var context = {
70-
deployment: {
71-
ui: {
72-
write: function() {},
73-
writeLine: function() {}
74-
},
75-
config: {
76-
'revision-key': config
77-
}
78-
},
69+
plugin.beforeHook(context);
70+
plugin.configure(context);
71+
72+
var messages = mockUi.messages.reduce(function(previous, current) {
73+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
74+
previous.push(current);
75+
}
7976

80-
distDir: 'some-dir',
81-
distFiles: ['a.js', 'b.css']
82-
};
83-
84-
return assert.isFulfilled(plugin.configure.call(plugin, context))
85-
.then(function() {
86-
assert.typeOf(config.distDir, 'function');
87-
assert.typeOf(config.distFiles, 'function');
88-
assert.equal(config.distDir(context), 'some-dir');
89-
assert.sameMembers(config.distFiles(context), ['a.js', 'b.css']);
90-
});
77+
return previous;
78+
}, []);
79+
80+
assert.equal(messages.length, 4);
81+
});
82+
83+
it('adds default config to the config object', function() {
84+
var plugin = subject.createDeployPlugin({
85+
name: 'revision-key'
9186
});
87+
88+
var context = {
89+
ui: mockUi,
90+
config: {
91+
"revision-key": {
92+
}
93+
}
94+
};
95+
96+
plugin.beforeHook(context);
97+
plugin.configure(context);
98+
99+
assert.isDefined(context.config['revision-key'].type);
100+
assert.isDefined(context.config['revision-key'].filePattern);
92101
});
93102
});
94103

95104
describe('didBuild hook', function() {
96-
it ('returns the revision key data', function() {
105+
it('returns the revisionKey', function() {
97106
var plugin = subject.createDeployPlugin({
98107
name: 'revision-key'
99108
});
100109

101110
var context = {
102-
deployment: {
103-
ui: {
104-
write: function() {},
105-
writeLine: function() {}
106-
},
107-
config: {
108-
'revision-key': {
109-
type: 'file-hash',
110-
filePattern: 'index.html',
111-
distDir: 'tests/fixtures',
112-
distFiles: ['index.html'],
111+
distDir: 'tests/fixtures',
112+
distFiles: ['index.html'],
113+
ui: mockUi,
114+
config: {
115+
"revision-key": {
116+
type: 'file-hash',
117+
filePattern: 'index.html',
118+
distDir: function(context) {
119+
return context.distDir;
113120
},
114-
}
121+
distFiles: function(context) {
122+
return context.distFiles;
123+
}
124+
},
115125
}
116126
};
127+
plugin.beforeHook(context);
117128

118-
return assert.isFulfilled(plugin.didBuild.call(plugin, context))
129+
return assert.isFulfilled(plugin.didBuild(context))
119130
.then(function(result) {
120131
assert.equal(result.revisionKey, 'ae1569f72495012cd5e8588e0f2f5d49');
121132
});

0 commit comments

Comments
 (0)