Skip to content

Commit e6534de

Browse files
author
Aaron Chambers
committed
Merge pull request #6 from strange-studios/use-config-before-context-options
Now we use user defined config before context values
2 parents 04e4c03 + 3821b38 commit e6534de

File tree

7 files changed

+257
-57
lines changed

7 files changed

+257
-57
lines changed

index.js

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,84 @@
11
/* jshint node: true */
22
'use strict';
33

4-
var cheerio = require('cheerio');
54
var path = require('path');
65
var fs = require('fs');
6+
var chalk = require('chalk');
77
var RSVP = require('rsvp');
8+
var Promise = RSVP.Promise;
89
var denodeify = RSVP.denodeify;
910

1011
var readFile = denodeify(fs.readFile);
1112
var writeFile = denodeify(fs.writeFile);
1213

14+
var blue = chalk.blue;
15+
var red = chalk.red;
16+
17+
var validateConfig = require('./lib/utilities/validate-config');
18+
var extractConfig = require('./lib/utilities/extract-index-config');
19+
1320
module.exports = {
1421
name: 'ember-cli-deploy-json-config',
1522

1623
createDeployPlugin: function(options) {
17-
return {
18-
name: options.name,
19-
20-
didBuild: function(context) {
21-
var deployment = context.deployment;
22-
var project = deployment.project;
23-
var root = project.root;
24-
var indexPath = path.join(root, (context.indexPath || 'dist/index.html'));
25-
var outputPath = path.join(path.dirname(indexPath), 'index.json');
24+
function _beginMessage(ui, inputPattern, outputPattern) {
25+
ui.write(blue('| '));
26+
ui.writeLine(blue('- generating `' + outputPattern + '` from `' + inputPattern + '`'));
2627

27-
return readFile(indexPath)
28-
.then(this._extractConfig.bind(this), this._handleMissingFile)
29-
.then(writeFile.bind(this, outputPath))
30-
.then(function() {
31-
return { indexPath: outputPath };
32-
});
33-
}.bind(this)
28+
return Promise.resolve();
3429
}
35-
},
3630

37-
_extractConfig: function(data) {
38-
var $ = cheerio.load(data.toString());
39-
var json = {
40-
base: this._get($, 'base', ['href']),
41-
meta: this._get($, 'meta[name*="/config/environment"]', ['name', 'content']),
42-
link: this._get($, 'link', ['rel', 'href']),
43-
script: this._get($, 'script', ['src'])
44-
};
31+
function _successMessage(ui, outputPattern) {
32+
ui.write(blue('| '));
33+
ui.writeLine(blue('- generated: `' + outputPattern + '`'));
34+
35+
return Promise.resolve(outputPattern);
36+
}
4537

46-
return RSVP.resolve(JSON.stringify(json));
47-
},
38+
function _errorMessage(ui, error) {
39+
ui.write(blue('| '));
40+
ui.write(red('- ' + error + '`\n'));
4841

49-
_handleMissingFile: function() {
50-
return RSVP.resolve();
51-
},
42+
return Promise.reject(error);
43+
}
5244

53-
_get: function($, selector, attributes) {
54-
attributes = attributes || [];
55-
var config = [];
56-
var $tags = $(selector);
45+
return {
46+
name: options.name,
5747

58-
$tags.each(function() {
59-
var $tag = $(this);
48+
willDeploy: function(context) {
49+
var deployment = context.deployment;
50+
var ui = deployment.ui;
51+
var config = deployment.config[this.name] = deployment.config[this.name] || {};
6052

61-
var data = attributes.reduce(function(data, attribute) {
62-
data[attribute] = $tag.attr(attribute);
53+
return validateConfig(ui, config)
54+
.then(function() {
55+
ui.write(blue('| '));
56+
ui.writeLine(blue('- config ok'));
57+
});
58+
},
6359

64-
return data;
65-
}, {});
60+
didBuild: function(context) {
61+
var deployment = context.deployment;
62+
var ui = deployment.ui;
63+
var config = deployment.config[this.name];
64+
var project = deployment.project;
65+
var root = project.root;
6666

67-
config.push(data);
68-
});
67+
var fileInputPattern = config.fileInputPattern;
68+
var fileOutputPattern = config.fileOutputPattern;
69+
var inputPath = path.join(root, fileInputPattern);
70+
var outputPath = path.join(root, fileOutputPattern);
6971

70-
return config;
72+
return _beginMessage(ui, fileInputPattern, fileOutputPattern)
73+
.then(readFile.bind(readFile, inputPath))
74+
.then(extractConfig.bind(this))
75+
.then(writeFile.bind(writeFile, outputPath))
76+
.then(_successMessage.bind(this, ui, fileOutputPattern))
77+
.then(function(outputPattern) {
78+
return { distFiles: [outputPattern] };
79+
})
80+
.catch(_errorMessage.bind(this, ui));
81+
}
82+
}
7183
}
7284
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var cheerio = require('cheerio');
2+
var RSVP = require('rsvp');
3+
4+
function _get($, selector, attributes) {
5+
attributes = attributes || [];
6+
var config = [];
7+
var $tags = $(selector);
8+
9+
$tags.each(function() {
10+
var $tag = $(this);
11+
12+
var data = attributes.reduce(function(data, attribute) {
13+
data[attribute] = $tag.attr(attribute);
14+
15+
return data;
16+
}, {});
17+
18+
config.push(data);
19+
});
20+
21+
return config;
22+
}
23+
24+
module.exports = function(data) {
25+
var $ = cheerio.load(data.toString());
26+
var json = {
27+
base: _get($, 'base', ['href']),
28+
meta: _get($, 'meta[name*="/config/environment"]', ['name', 'content']),
29+
link: _get($, 'link', ['rel', 'href']),
30+
script: _get($, 'script', ['src'])
31+
};
32+
33+
return RSVP.resolve(JSON.stringify(json));
34+
};

lib/utilities/validate-config.js

Lines changed: 26 additions & 0 deletions
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+
fileInputPattern: 'dist/index.html',
13+
fileOutputPattern: 'dist/index.json'
14+
};
15+
16+
['fileInputPattern', 'fileOutputPattern'].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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@
4242
"ember-cli-deploy-plugin"
4343
],
4444
"dependencies": {
45-
"ember-cli-babel": "^5.0.0",
45+
"chalk": "^1.0.0",
4646
"cheerio": "^0.19.0",
47+
"ember-cli-babel": "^5.0.0",
4748
"rsvp": "^3.0.18"
4849
},
4950
"ember-addon": {

tests/unit/index-nodetest.js

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,53 @@ describe('the deploy plugin object', function() {
3333
name: 'test-plugin'
3434
});
3535

36+
assert.equal(typeof result.willDeploy, 'function');
3637
assert.equal(typeof result.didBuild, 'function');
3738
});
3839

40+
describe('willDeploy hook', function() {
41+
it('resolves if config is ok', function() {
42+
var plugin = subject.createDeployPlugin({
43+
name: 'json-config'
44+
});
45+
46+
var context = {
47+
deployment: {
48+
ui: { write: function() {}, writeLine: function() {} },
49+
config: {
50+
'json-config': {
51+
fileInputPattern: 'dist/index.html',
52+
fileOutputPattern: 'dist/index.json'
53+
}
54+
}
55+
}
56+
};
57+
58+
return assert.isFulfilled(plugin.willDeploy.call(plugin, context))
59+
});
60+
});
61+
3962
describe('didBuild hook', function() {
4063
it('generates index.json from index.html', function() {
41-
var didBuild = subject.createDeployPlugin({
42-
name: 'test-plugin'
43-
}).didBuild;
64+
var plugin = subject.createDeployPlugin({
65+
name: 'json-config'
66+
});
4467

4568
var context = {
4669
deployment: {
47-
project: { root: fakeRoot }
70+
project: { root: fakeRoot },
71+
ui: {write: function() {}, writeLine: function() {}},
72+
config: {
73+
'json-config': {
74+
fileInputPattern: 'dist/index.html',
75+
fileOutputPattern: 'dist/index.json'
76+
}
77+
}
4878
},
4979
data: {}
5080
};
5181

52-
var promise = didBuild(context);
82+
var promise = plugin.didBuild.call(plugin, context);
5383
return assert.isFulfilled(promise)
5484
.then(function() {
5585
var json = require(fakeRoot + '/dist/index.json');
@@ -66,22 +96,28 @@ describe('the deploy plugin object', function() {
6696
});
6797

6898
it ('returns the index.json path', function() {
69-
var didBuild = subject.createDeployPlugin({
70-
name: 'test-plugin'
71-
}).didBuild;
99+
var plugin = subject.createDeployPlugin({
100+
name: 'json-config'
101+
});
72102

73103
var data = {};
74104
var context = {
75-
indexPath: 'dist/index.html',
76105
deployment: {
77-
project: { root: fakeRoot }
106+
project: { root: fakeRoot },
107+
ui: {write: function() {}, writeLine: function() {}},
108+
config: {
109+
'json-config': {
110+
fileInputPattern: 'dist/index.html',
111+
fileOutputPattern: 'dist/index.json'
112+
}
113+
}
78114
}
79115
};
80116

81-
var promise = didBuild(context);
117+
var promise = plugin.didBuild.call(plugin, context);
82118
return assert.isFulfilled(promise)
83119
.then(function(result) {
84-
assert.equal(result.indexPath, fakeRoot + '/dist/index.json');
120+
assert.deepEqual(result.distFiles, ['dist/index.json']);
85121
});
86122
});
87123
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var assert = require('ember-cli/tests/helpers/assert');
2+
3+
var fs = require('fs');
4+
5+
describe('extract-index-config', function() {
6+
var subject;
7+
8+
before(function() {
9+
subject = require('../../../../lib/utilities/extract-index-config');
10+
});
11+
12+
it('extracts the correct config', function() {
13+
var contents = fs.readFileSync(process.cwd() + '/tests/fixtures/dist/index.html');
14+
15+
return assert.isFulfilled(subject(contents))
16+
.then(function(config) {
17+
var json = JSON.parse(config);
18+
19+
assert.equal(Object.keys(json).length, 4);
20+
21+
assert.deepEqual(json.base[0], { href: '/' });
22+
assert.deepEqual(json.meta[0], { name: 'my-app/config/environment', content: 'some-config-values' });
23+
assert.deepEqual(json.link[0], { rel: 'stylesheet', href: 'assets/vendor.css' });
24+
assert.deepEqual(json.link[1], { rel: 'stylesheet', href: 'assets/app.css' });
25+
assert.deepEqual(json.script[0], { src: 'assets/vendor.js' });
26+
assert.deepEqual(json.script[1], { src: 'assets/app.js' });
27+
});
28+
});
29+
});
Lines changed: 62 additions & 0 deletions
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+
fileInputPattern: 'aaaa',
15+
fileOutputPattern:'bbbb'
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.fileInputPattern;
29+
delete config.fileOutputPattern;
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.fileInputPattern;
47+
delete config.fileOutputPattern;
48+
49+
assert.isUndefined(config.fileInputPattern);
50+
assert.isUndefined(config.fileOutputPattern);
51+
52+
return assert.isFulfilled(subject(mockUi, config))
53+
.then(function() {
54+
assert.isDefined(config.fileInputPattern);
55+
assert.isDefined(config.fileOutputPattern);
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)