Skip to content

Commit 59c2496

Browse files
committed
Merge pull request #4 from ember-cli/fix-stuff
Fix stuff
2 parents 83aea13 + d00bebd commit 59c2496

3 files changed

Lines changed: 90 additions & 39 deletions

File tree

index.js

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

3-
var fs = require('fs-extra'),
4-
path = require('path'),
5-
Plugin = require('broccoli-plugin'),
6-
hashStrings = require('broccoli-kitchen-sink-helpers').hashStrings;
3+
var fs = require('fs-extra');
4+
var path = require('path');
5+
var Plugin = require('broccoli-plugin');
6+
var hashStrings = require('broccoli-kitchen-sink-helpers').hashStrings;
7+
var debug = require('debug')('broccoli-config-replace');
78

89
function ConfigReplace(inputNode, configNode, options) {
910
options = options || {};
@@ -26,9 +27,11 @@ ConfigReplace.prototype.build = function () {
2627
var config = this.getConfig();
2728

2829
this.options.files.forEach(function(file) {
29-
var key = this.deriveCacheKey(file),
30-
entry = this._cache[key.hash],
31-
contents, filePath;
30+
var key = this.deriveCacheKey(file);
31+
var entry = this._cache[key.hash];
32+
var contents, filePath;
33+
34+
debug('cache hit: %o, for: %s', !!entry, file);
3235

3336
if (entry) { return; }
3437

@@ -42,25 +45,30 @@ ConfigReplace.prototype.build = function () {
4245
};
4346

4447
ConfigReplace.prototype.deriveCacheKey = function(file) {
45-
var stats = fs.statSync(this.getConfigPath());
48+
var configStat = fs.statSync(this.getConfigPath());
49+
var fileStat = fs.statSync(this.inputPaths[0] + '/' + file);
4650

47-
if (stats.isDirectory()) {
51+
if (configStat.isDirectory()) {
4852
throw new Error('Must provide a path for the config file, you supplied a directory');
4953
}
5054

5155
var patterns = this.options.patterns.reduce(function(a, b) {
5256
return a + b.match;
5357
}, '');
5458

59+
5560
return {
5661
file: file,
5762
hash: hashStrings([
5863
file,
5964
this.configPath,
6065
patterns,
61-
stats.size,
62-
stats.mode,
63-
stats.mtime.getTime()
66+
configStat.size,
67+
configStat.mode,
68+
configStat.mtime.getTime(),
69+
fileStat.size,
70+
fileStat.mode,
71+
fileStat.mtime.getTime(),
6472
])
6573
};
6674
};

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Simple templating using a config.json and regex patterns",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha"
7+
"test": "mocha",
8+
"test:debug": "mocha debug"
89
},
910
"repository": {
1011
"type": "git",
@@ -28,10 +29,12 @@
2829
"dependencies": {
2930
"broccoli-kitchen-sink-helpers": "^0.3.1",
3031
"broccoli-plugin": "^1.2.0",
32+
"debug": "^2.2.0",
3133
"fs-extra": "^0.24.0"
3234
},
3335
"devDependencies": {
3436
"broccoli": "^0.16.8",
37+
"chai": "^3.4.1",
3538
"mocha": "^2.3.3",
3639
"tmp-sync": "^1.1.0"
3740
}

test/index.js

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
var assert = require('assert'),
2-
broccoli = require('broccoli'),
3-
ConfigReplace = require('..'),
4-
join = require('path').join,
5-
fs = require('fs'),
6-
tmp = require('tmp-sync');
1+
var assert = require('assert');
2+
var broccoli = require('broccoli');
3+
var ConfigReplace = require('..');
4+
var join = require('path').join;
5+
var fs = require('fs');
6+
var tmp = require('tmp-sync');
7+
var expect = require('chai').expect;
78

89
afterEach(function() {
910
if (this.builder) {
@@ -13,10 +14,16 @@ afterEach(function() {
1314

1415
function writeExample(options) {
1516
var root = tmp.in(join(process.cwd(), 'tmp'));
17+
1618
fs.writeFileSync(join(root, 'config.json'), options.config);
1719
fs.writeFileSync(join(root, 'index.html'), options.index);
20+
1821
return root;
19-
};
22+
}
23+
24+
function read(fullPath) {
25+
return fs.readFileSync(fullPath, 'UTF8');
26+
}
2027

2128
function makeConfigReplace(root, patterns) {
2229
return new ConfigReplace(
@@ -29,52 +36,53 @@ function makeConfigReplace(root, patterns) {
2936
patterns: patterns
3037
}
3138
);
32-
};
39+
}
3340

3441
function makeBuilder(root, patterns) {
3542
var configReplace = makeConfigReplace(root, patterns);
3643
return new broccoli.Builder(configReplace);
37-
};
44+
}
3845

39-
var expectEquals = function(expected) {
46+
function expectEquals(expected) {
4047
return function(results) {
4148
var resultsPath = join(results.directory, 'index.html'),
4249
contents = fs.readFileSync(resultsPath, { encoding: 'utf8' });
4350

4451
assert.equal(contents.trim(), expected);
52+
return results;
4553
};
46-
};
54+
}
4755

4856
describe('config-replace', function() {
49-
it('replaces with text from config.json', function(done) {
57+
it('replaces with text from config.json', function() {
5058
var root = writeExample({
5159
config: '{"color":"red"}',
5260
index: '{{color}}'
5361
});
5462

55-
makeBuilder(root, [{
63+
return makeBuilder(root, [{
5664
match: /\{\{color\}\}/g,
5765
replacement: function(config) { return config.color; }
5866
}]).build().then(
5967
expectEquals('red')
60-
).then(done).catch(console.log);
68+
);
6169
});
6270

63-
it('replaces with string passed in via options', function(done) {
71+
it('replaces with string passed in via options', function() {
6472
var root = writeExample({
6573
config: '{}',
6674
index: '{{name}}'
6775
});
6876

69-
makeBuilder(root, [{
77+
return makeBuilder(root, [{
7078
match: /\{\{name\}\}/g,
7179
replacement: 'hari'
7280
}]).build().then(
7381
expectEquals('hari')
74-
).then(done).catch(console.log);
82+
);
7583
});
7684

77-
it('rebuilds if the config file changes', function(done) {
85+
it('rebuilds if the config file changes', function() {
7886
var root = writeExample({
7987
config: '{"pokemon":"diglet"}',
8088
index: '{{pokemon}}'
@@ -85,17 +93,17 @@ describe('config-replace', function() {
8593
replacement: function(config) { return config.pokemon; }
8694
}]);
8795

88-
builder.build().then(
96+
return builder.build().then(
8997
expectEquals('diglet')
9098
).then(function() {
9199
fs.writeFileSync(join(root, 'config.json'), '{"pokemon":"jigglypuff"}');
92100
return builder.build();
93101
}).then(
94102
expectEquals('jigglypuff')
95-
).then(done).catch(console.log);
103+
);
96104
});
97105

98-
it('caches the result', function(done) {
106+
it('caches the result', function() {
99107
var root, configReplace, builder, key, entry;
100108

101109
root = writeExample({
@@ -109,14 +117,46 @@ describe('config-replace', function() {
109117
}]);
110118

111119
builder = new broccoli.Builder(configReplace);
112-
builder.build().then(
120+
121+
var indexStat;
122+
return builder.build().then(
113123
expectEquals('nyc')
114124
).then(function() {
115-
key = Object.keys(configReplace._cache)[0];
116-
entry = configReplace._cache[key];
125+
indexStat = fs.statSync(join(root, 'index.html'));
126+
117127
return builder.build();
118128
}).then(function() {
119-
assert.equal(entry, configReplace._cache[key]);
120-
}).then(done).catch(console.log);
129+
var nextStat = fs.statSync(join(root, 'index.html'));
130+
assert.deepEqual(indexStat, nextStat);
131+
});
132+
});
133+
134+
it('evicts after change', function() {
135+
var root, configReplace, builder, key, entry;
136+
137+
root = writeExample({
138+
config: '{"city":"nyc"}',
139+
index: '{{city}}'
140+
});
141+
142+
configReplace = makeConfigReplace(root, [{
143+
match: /\{\{city\}\}/g,
144+
replacement: function(config) { return config.city; }
145+
}]);
146+
147+
builder = new broccoli.Builder(configReplace);
148+
149+
var oldContent;
150+
151+
return builder.build().then(function(results) {
152+
oldContent = read(results.directory + '/index.html');
153+
154+
fs.writeFileSync(root + '/index.html', 'foo');
155+
return builder.build();
156+
}).then(function(results) {
157+
158+
var newContent = read(results.directory + '/index.html');
159+
expect(newContent).to.not.equal(oldContent);
160+
});
121161
});
122162
});

0 commit comments

Comments
 (0)