Skip to content

Commit 11ee781

Browse files
author
Aaron Chambers
committed
Merge pull request #4 from strange-studios/remove-dependency-on-index-path
Remove dependency on index path
2 parents af220c5 + ddb868e commit 11ee781

File tree

7 files changed

+215
-28
lines changed

7 files changed

+215
-28
lines changed

index.js

+46-6
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,68 @@
11
/* jshint node: true */
22
'use strict';
33

4+
var Promise = require('ember-cli/lib/ext/promise');
5+
6+
var chalk = require('chalk');
7+
var blue = chalk.blue;
8+
9+
var validateConfig = require('./lib/utilities/validate-config');
10+
411
module.exports = {
512
name: 'ember-cli-deploy-tag',
613

714
createDeployPlugin: function(options) {
815
var tags = require('./lib/tags');
916

10-
function tagFor(type) {
11-
var defaultType = 'index-hash';
12-
return tags[type] || tags[defaultType];
17+
function _beginMessage(ui, type) {
18+
ui.write(blue('| '));
19+
ui.writeLine(blue('- creating tag using `' + type + '`'));
20+
21+
return Promise.resolve();
22+
}
23+
24+
function _successMessage(ui, tag) {
25+
ui.write(blue('| '));
26+
ui.writeLine(blue('- generated tag: `' + tag + '`'));
27+
28+
return Promise.resolve(tag);
1329
}
1430

1531
return {
1632
name: options.name,
1733

34+
willDeploy: function(context) {
35+
var deployment = context.deployment;
36+
var ui = deployment.ui;
37+
var config = deployment.config[this.name] || {};
38+
39+
return validateConfig(ui, config)
40+
.then(function() {
41+
ui.write(blue('| '));
42+
ui.writeLine(blue('- config ok'));
43+
});
44+
},
45+
1846
didBuild: function(context) {
1947
var deployment = context.deployment;
48+
var ui = deployment.ui;
2049
var config = deployment.config[this.name] || {};
50+
var type = config.type;
2151

22-
var Tag = tagFor(config.type);
23-
var tag = new Tag(context);
52+
var Tag = tags[type];
53+
var tag = new Tag({
54+
config: config,
55+
context: context
56+
});
2457

25-
return { tag: tag.generate() };
58+
return _beginMessage(ui, type)
59+
.then(function() {
60+
return Promise.resolve(tag.generate());
61+
})
62+
.then(_successMessage.bind(this, ui))
63+
.then(function(value) {
64+
return { tag: value };
65+
});
2666
}
2767
};
2868
}

lib/tags/index-hash.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1+
var CoreObject = require('core-object');
12
var fs = require('fs');
23
var Fingerprint = require('broccoli-asset-rev/lib/fingerprint');
34

4-
function IndexHashTag(options) {
5-
this.options = options || {};
6-
}
5+
module.exports = CoreObject.extend({
6+
init: function(options) {
7+
this._context = options.context || {};
8+
this._config = options.config || {};
9+
},
710

8-
IndexHashTag.prototype.generate = function() {
9-
var indexPath = this.options.indexPath;
11+
generate: function() {
12+
var filePattern = this._config.filePattern;
13+
var distFiles = this._context.distFiles;
14+
var index = distFiles.indexOf(filePattern);
15+
var indexPath;
16+
var contents;
17+
var fingerprint;
1018

11-
if (!fs.existsSync(indexPath)) {
12-
return '';
13-
}
19+
if (index === -1) {
20+
return '';
21+
}
22+
23+
indexPath = distFiles[index];
1424

15-
var contents = fs.readFileSync(indexPath);
16-
var fingerprint = new Fingerprint();
25+
if (!fs.existsSync(indexPath)) {
26+
return '';
27+
}
1728

18-
return fingerprint.hashFn(contents);
19-
};
29+
contents = fs.readFileSync(indexPath);
30+
fingerprint = new Fingerprint();
2031

21-
module.exports = IndexHashTag;
32+
return fingerprint.hashFn(contents);
33+
}
34+
});

lib/utilities/validate-config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var Promise = require('ember-cli/lib/ext/promise');
2+
3+
var chalk = require('chalk');
4+
var yellow = chalk.yellow;
5+
var blue = chalk.blue;
6+
7+
module.exports = function(ui, config) {
8+
ui.write(blue('| '));
9+
ui.writeLine(blue('- validating config'));
10+
11+
var defaultConfig = {
12+
type: 'index-hash',
13+
filePattern: 'dist/index.html'
14+
};
15+
16+
['type', 'filePattern'].forEach(function(prop) {
17+
if (!config[prop]) {
18+
var value = defaultConfig[prop];
19+
config[prop] = value;
20+
ui.write(blue('| '));
21+
ui.writeLine(yellow('- Missing config: `' + prop + '`, using default: `' + value + '`'));
22+
}
23+
});
24+
25+
return Promise.resolve();
26+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@
4242
"ember-cli-deploy-plugin"
4343
],
4444
"dependencies": {
45+
"chalk": "^1.0.0",
46+
"core-object": "^1.1.0",
4547
"ember-cli-babel": "^5.0.0"
4648
},
4749
"ember-addon": {
4850
"configPath": "tests/dummy/config",
49-
"before": "ember-cli-deploy-json-config"
51+
"after": "ember-cli-deploy-json-config"
5052
}
5153
}

tests/unit/index-nodetest.js

+40-6
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,57 @@ describe('the index', function() {
2525
assert.equal(typeof result.didBuild, 'function');
2626
});
2727

28+
describe('willDeploy hook', function() {
29+
it('resolves if config is ok', function() {
30+
var plugin = subject.createDeployPlugin({
31+
name: 'tag'
32+
});
33+
34+
var context = {
35+
deployment: {
36+
ui: {
37+
write: function() {},
38+
writeLine: function() {}
39+
},
40+
config: {
41+
tag: {
42+
type: 'index-hash',
43+
filePattern: 'eeee'
44+
}
45+
}
46+
}
47+
};
48+
49+
return assert.isFulfilled(plugin.willDeploy.call(plugin, context))
50+
});
51+
});
2852

2953
describe('didBuild hook', function() {
3054
it ('returns the tag data', function() {
3155
var plugin = subject.createDeployPlugin({
32-
name: 'test-plugin'
56+
name: 'tag'
3357
});
3458

3559
var context = {
3660
deployment: {
37-
config: {}
61+
ui: {
62+
write: function() {},
63+
writeLine: function() {}
64+
},
65+
config: {
66+
tag: {
67+
type: 'index-hash',
68+
filePattern: process.cwd() + '/tests/fixtures/index.html'
69+
},
70+
}
3871
},
39-
indexPath: process.cwd() + '/tests/fixtures/index.html'
72+
distFiles: [process.cwd() + '/tests/fixtures/index.html']
4073
};
4174

42-
var result = plugin.didBuild.call(plugin, context);
43-
44-
assert.equal(result.tag, 'ae1569f72495012cd5e8588e0f2f5d49');
75+
return assert.isFulfilled(plugin.didBuild.call(plugin, context))
76+
.then(function(result) {
77+
assert.equal(result.tag, 'ae1569f72495012cd5e8588e0f2f5d49');
78+
});
4579
});
4680
});
4781
});

tests/unit/lib/tags/index-hash-nodetest.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ describe('the index-hash tag', function() {
1212
describe('#generate', function() {
1313
it ('generates a hash of the supplied index file', function() {
1414
var subject = new Tag({
15-
indexPath: process.cwd() + '/tests/fixtures/index.html'
15+
context: {
16+
distFiles: [process.cwd() + '/tests/fixtures/index.html'],
17+
},
18+
config: {
19+
filePattern: process.cwd() + '/tests/fixtures/index.html'
20+
}
1621
});
1722

1823
var hash = subject.generate();
@@ -22,7 +27,12 @@ describe('the index-hash tag', function() {
2227

2328
it('returns an empty string when the file doesn\'t exist', function() {
2429
var subject = new Tag({
25-
indexPath: 'non-existent-file.xml'
30+
context: {
31+
distFiles: [process.cwd() + '/tests/fixtures/index.html'],
32+
},
33+
config: {
34+
filePattern: 'some-file-that-does-not-exist'
35+
}
2636
});
2737

2838
var hash = subject.generate();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
var assert = require('ember-cli/tests/helpers/assert');
2+
3+
describe('validate-config', function() {
4+
var subject;
5+
var config;
6+
var mockUi;
7+
8+
before(function() {
9+
subject = require('../../../../lib/utilities/validate-config');
10+
});
11+
12+
beforeEach(function() {
13+
config = {
14+
type: 'aaaa',
15+
filePattern: 'eeee'
16+
};
17+
18+
mockUi = {
19+
messages: [],
20+
write: function() { },
21+
writeLine: function(message) {
22+
this.messages.push(message);
23+
}
24+
};
25+
});
26+
27+
it('warns about missing optional config', function() {
28+
delete config.type;
29+
delete config.filePattern;
30+
31+
return assert.isFulfilled(subject(mockUi, config))
32+
.then(function() {
33+
var messages = mockUi.messages.reduce(function(previous, current) {
34+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
35+
previous.push(current);
36+
}
37+
38+
return previous;
39+
}, []);
40+
41+
assert.equal(messages.length, 2);
42+
});
43+
});
44+
45+
it('adds default config to the config object', function() {
46+
delete config.type;
47+
delete config.filePattern;
48+
49+
assert.isUndefined(config.type);
50+
assert.isUndefined(config.filePattern);
51+
52+
return assert.isFulfilled(subject(mockUi, config))
53+
.then(function() {
54+
assert.isDefined(config.type);
55+
assert.isDefined(config.filePattern);
56+
});
57+
});
58+
59+
it('resolves if config is ok', function() {
60+
return assert.isFulfilled(subject(mockUi, config));
61+
})
62+
});

0 commit comments

Comments
 (0)