Skip to content

Commit 397e4f3

Browse files
committed
Now filePattern is relative to the dist dir which is stored in
`context.distDir`
1 parent 11ee781 commit 397e4f3

File tree

6 files changed

+63
-33
lines changed

6 files changed

+63
-33
lines changed

index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var Promise = require('ember-cli/lib/ext/promise');
55

66
var chalk = require('chalk');
77
var blue = chalk.blue;
8+
var red = chalk.red;
89

910
var validateConfig = require('./lib/utilities/validate-config');
1011

@@ -15,19 +16,26 @@ module.exports = {
1516
var tags = require('./lib/tags');
1617

1718
function _beginMessage(ui, type) {
18-
ui.write(blue('| '));
19+
ui.write(blue('| '));
1920
ui.writeLine(blue('- creating tag using `' + type + '`'));
2021

2122
return Promise.resolve();
2223
}
2324

2425
function _successMessage(ui, tag) {
25-
ui.write(blue('| '));
26+
ui.write(blue('| '));
2627
ui.writeLine(blue('- generated tag: `' + tag + '`'));
2728

2829
return Promise.resolve(tag);
2930
}
3031

32+
function _errorMessage(ui, error) {
33+
ui.write(blue('| '));
34+
ui.write(red('- ' + error + '`\n'));
35+
36+
return Promise.reject(error);
37+
}
38+
3139
return {
3240
name: options.name,
3341

@@ -56,13 +64,12 @@ module.exports = {
5664
});
5765

5866
return _beginMessage(ui, type)
59-
.then(function() {
60-
return Promise.resolve(tag.generate());
61-
})
67+
.then(tag.generate.bind(tag))
6268
.then(_successMessage.bind(this, ui))
6369
.then(function(value) {
6470
return { tag: value };
65-
});
71+
})
72+
.catch(_errorMessage.bind(this, ui));
6673
}
6774
};
6875
}

lib/tags/index-hash.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
var CoreObject = require('core-object');
22
var fs = require('fs');
3+
var path = require('path');
4+
var minimatch = require('minimatch');
35
var Fingerprint = require('broccoli-asset-rev/lib/fingerprint');
46

7+
var denodeify = require('rsvp').denodeify;
8+
var readFile = denodeify(fs.readFile);
9+
510
module.exports = CoreObject.extend({
611
init: function(options) {
712
this._context = options.context || {};
@@ -10,25 +15,22 @@ module.exports = CoreObject.extend({
1015

1116
generate: function() {
1217
var filePattern = this._config.filePattern;
18+
var distDir = this._context.distDir;
1319
var distFiles = this._context.distFiles;
14-
var index = distFiles.indexOf(filePattern);
15-
var indexPath;
16-
var contents;
1720
var fingerprint;
1821

19-
if (index === -1) {
20-
return '';
21-
}
22-
23-
indexPath = distFiles[index];
22+
var filePaths = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
2423

25-
if (!fs.existsSync(indexPath)) {
26-
return '';
24+
if (!filePaths.length) {
25+
return Promise.reject('`' + filePattern + '` does not exist in distDir');
2726
}
2827

29-
contents = fs.readFileSync(indexPath);
30-
fingerprint = new Fingerprint();
28+
var filePath = path.join(distDir, filePaths[0]);
3129

32-
return fingerprint.hashFn(contents);
30+
return readFile(filePath)
31+
.then(function(contents) {
32+
fingerprint = new Fingerprint();
33+
return fingerprint.hashFn(contents.toString());
34+
})
3335
}
3436
});

lib/utilities/validate-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module.exports = function(ui, config) {
1010

1111
var defaultConfig = {
1212
type: 'index-hash',
13-
filePattern: 'dist/index.html'
13+
filePattern: 'index.html'
1414
};
1515

1616
['type', 'filePattern'].forEach(function(prop) {

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
"dependencies": {
4545
"chalk": "^1.0.0",
4646
"core-object": "^1.1.0",
47-
"ember-cli-babel": "^5.0.0"
47+
"ember-cli-babel": "^5.0.0",
48+
"minimatch": "^2.0.4",
49+
"rsvp": "^3.0.18"
4850
},
4951
"ember-addon": {
5052
"configPath": "tests/dummy/config",

tests/unit/index-nodetest.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ describe('the index', function() {
5757
});
5858

5959
var context = {
60+
distDir: process.cwd() + '/tests/fixtures',
61+
distFiles: ['index.html'],
6062
deployment: {
6163
ui: {
6264
write: function() {},
@@ -65,11 +67,10 @@ describe('the index', function() {
6567
config: {
6668
tag: {
6769
type: 'index-hash',
68-
filePattern: process.cwd() + '/tests/fixtures/index.html'
70+
filePattern: 'index.html'
6971
},
7072
}
71-
},
72-
distFiles: [process.cwd() + '/tests/fixtures/index.html']
73+
}
7374
};
7475

7576
return assert.isFulfilled(plugin.didBuild.call(plugin, context))

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

+27-9
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,49 @@ describe('the index-hash tag', function() {
1313
it ('generates a hash of the supplied index file', function() {
1414
var subject = new Tag({
1515
context: {
16-
distFiles: [process.cwd() + '/tests/fixtures/index.html'],
16+
distDir: process.cwd() + '/tests/fixtures',
17+
distFiles: ['index.html'],
1718
},
1819
config: {
19-
filePattern: process.cwd() + '/tests/fixtures/index.html'
20+
filePattern: 'index.html'
2021
}
2122
});
2223

23-
var hash = subject.generate();
24-
25-
assert.equal(hash, 'ae1569f72495012cd5e8588e0f2f5d49');
24+
return assert.isFulfilled(subject.generate())
25+
.then(function(hash) {
26+
assert.equal(hash, 'ae1569f72495012cd5e8588e0f2f5d49');
27+
});
2628
});
2729

28-
it('returns an empty string when the file doesn\'t exist', function() {
30+
it('rejects when the filePattern doesn\'t exist in distFiles', function() {
2931
var subject = new Tag({
3032
context: {
31-
distFiles: [process.cwd() + '/tests/fixtures/index.html'],
33+
distDir: process.cwd() + '/tests/fixtures',
34+
distFiles: ['index.html']
3235
},
3336
config: {
3437
filePattern: 'some-file-that-does-not-exist'
3538
}
3639
});
3740

38-
var hash = subject.generate();
41+
return assert.isRejected(subject.generate())
42+
.then(function(error) {
43+
assert.equal(error, '`some-file-that-does-not-exist` does not exist in distDir');
44+
});
45+
});
46+
47+
it('rejects when the file doesn\'t exist', function() {
48+
var subject = new Tag({
49+
context: {
50+
distDir: process.cwd() + '/tests/fixtures',
51+
distFiles: ['index.xxx']
52+
},
53+
config: {
54+
filePattern: 'index.xxx'
55+
}
56+
});
3957

40-
assert.equal(hash, '');
58+
return assert.isRejected(subject.generate());
4159
});
4260
});
4361
});

0 commit comments

Comments
 (0)