Skip to content

Commit 21bdb49

Browse files
author
Aaron Chambers
committed
Merge pull request #8 from strange-studios/plugin-base-class-refactor
Restructure to use ember-cli-deploy-plugin
2 parents d18ff5b + bf4975a commit 21bdb49

File tree

5 files changed

+72
-219
lines changed

5 files changed

+72
-219
lines changed

index.js

Lines changed: 41 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,71 @@
33

44
var path = require('path');
55
var fs = require('fs');
6-
var chalk = require('chalk');
76
var RSVP = require('rsvp');
87
var Promise = RSVP.Promise;
98
var denodeify = RSVP.denodeify;
109

1110
var readFile = denodeify(fs.readFile);
1211
var writeFile = denodeify(fs.writeFile);
1312

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');
13+
var extractConfigFromHtmlAsJson = require('./lib/utilities/extract-index-config');
14+
var DeployPluginBase = require('ember-cli-deploy-plugin');
1915

2016
module.exports = {
2117
name: 'ember-cli-deploy-json-config',
2218

2319
createDeployPlugin: function(options) {
24-
function _beginMessage(ui, inputPath, outputPath) {
25-
ui.write(blue('| '));
26-
ui.writeLine(blue('- generating `' + outputPath + '` from `' + inputPath + '`'));
27-
28-
return Promise.resolve();
29-
}
30-
31-
function _successMessage(ui, outputPath) {
32-
ui.write(blue('| '));
33-
ui.writeLine(blue('- generated: `' + outputPath + '`'));
34-
35-
return Promise.resolve();
36-
}
37-
38-
function _errorMessage(ui, error) {
39-
ui.write(blue('| '));
40-
ui.write(red('- ' + error + '`\n'));
41-
42-
return Promise.reject(error);
43-
}
44-
45-
return {
20+
var DeployPlugin = DeployPluginBase.extend({
4621
name: options.name,
4722

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] || {};
52-
53-
return validateConfig(ui, config)
54-
.then(function() {
55-
ui.write(blue('| '));
56-
ui.writeLine(blue('- config ok'));
57-
});
23+
defaultConfig: {
24+
fileInputPattern: 'index.html',
25+
fileOutputPattern: 'index.json',
26+
projectRoot: function(context) {
27+
return context.project.root;
28+
},
29+
distDir: function(context) {
30+
return context.distDir;
31+
}
5832
},
5933

6034
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-
66-
var root = project.root;
67-
var distDir = context.distDir;
68-
var fileInputPattern = config.fileInputPattern;
69-
var fileOutputPattern = config.fileOutputPattern;
35+
var root = this.readConfig('projectRoot');
36+
var distDir = this.readConfig('distDir');
37+
var fileInputPattern = this.readConfig('fileInputPattern');
38+
var fileOutputPattern = this.readConfig('fileOutputPattern');
7039
var inputPath = path.join(distDir, fileInputPattern);
7140
var outputPath = path.join(distDir, fileOutputPattern);
72-
var absoluteInputPath = path.join(root, inputPath);
73-
var absoluteOutputPath = path.join(root, outputPath);
41+
var absoluteInputPath = path.join(root, inputPath);
42+
var absoluteOutputPath = path.join(root, outputPath);
43+
44+
this.log('generating `' + outputPath + '` from `' + inputPath + '`');
7445

75-
return _beginMessage(ui, inputPath, outputPath)
76-
.then(readFile.bind(readFile, absoluteInputPath))
77-
.then(extractConfig.bind(this))
46+
return readFile(absoluteInputPath)
47+
.then(extractConfigFromHtmlAsJson.bind(this))
7848
.then(writeFile.bind(writeFile, absoluteOutputPath))
79-
.then(_successMessage.bind(this, ui, outputPath))
49+
.then(this._successMessage.bind(this, outputPath, fileOutputPattern))
8050
.then(function() {
81-
ui.write(blue('| '));
82-
ui.writeLine(blue('- added `' + fileOutputPattern + '` to `context.distFiles`'));
83-
8451
return { distFiles: [fileOutputPattern] };
8552
})
86-
.catch(_errorMessage.bind(this, ui));
53+
.catch(this._errorMessage.bind(this));
54+
},
55+
56+
_successMessage: function(outputPath, fileOutputPattern) {
57+
this.log('generated: `' + outputPath + '`');
58+
this.log('added `' + fileOutputPattern + '` to `context.distFiles`');
59+
return Promise.resolve();
60+
},
61+
62+
_errorMessage: function(error) {
63+
this.log(error, { color: 'red' });
64+
if (error) {
65+
this.log(error.stack, { color: 'red' });
66+
}
67+
return Promise.reject(error);
8768
}
88-
}
69+
});
70+
71+
return new DeployPlugin();
8972
}
9073
};

lib/utilities/validate-config.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"chalk": "^1.0.0",
4646
"cheerio": "^0.19.0",
4747
"ember-cli-babel": "^5.0.0",
48+
"ember-cli-deploy-plugin": "^0.1.1",
4849
"rsvp": "^3.0.18"
4950
},
5051
"ember-addon": {

tests/unit/index-nodetest.js

Lines changed: 30 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,59 @@ var path = require('path');
55
var assert = require('ember-cli/tests/helpers/assert');
66

77
describe('the deploy plugin object', function() {
8-
var subject;
98
var fakeRoot;
9+
var plugin;
10+
var promise;
1011

1112
before(function() {
12-
subject = require('../../index');
1313
fakeRoot = process.cwd() + '/tests/fixtures';
1414
});
1515

1616
beforeEach(function() {
17+
var subject = require('../../index');
1718
var jsonPath = fakeRoot + '/dist/index.json';
1819
if (fs.existsSync(jsonPath)) {
1920
fs.unlinkSync(jsonPath);
2021
}
21-
});
2222

23-
it('has a name', function() {
24-
var result = subject.createDeployPlugin({
25-
name: 'test-plugin'
23+
var mockUi = {write: function() {}, writeLine: function() {}};
24+
25+
plugin = subject.createDeployPlugin({
26+
name: 'json-config'
2627
});
2728

28-
assert.equal('test-plugin', result.name);
29-
});
29+
var context = {
30+
ui: mockUi,
31+
config: {
32+
'json-config': {
33+
fileInputPattern: 'index.html',
34+
fileOutputPattern: 'index.json',
35+
distDir: function(context) {
36+
return 'dist';
37+
},
38+
projectRoot: function(context) {
39+
return fakeRoot;
40+
}
41+
}
42+
}
43+
};
3044

31-
it('implements the correct hooks', function() {
32-
var result = subject.createDeployPlugin({
33-
name: 'test-plugin'
34-
});
45+
plugin.beforeHook(context);
46+
plugin.configure(context);
3547

36-
assert.equal(typeof result.willDeploy, 'function');
37-
assert.equal(typeof result.didBuild, 'function');
48+
promise = plugin.didBuild.call(plugin, context);
3849
});
3950

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-
};
51+
it('has a name', function() {
52+
assert.equal('json-config', plugin.name);
53+
});
5754

58-
return assert.isFulfilled(plugin.willDeploy.call(plugin, context))
59-
});
55+
it('implements the correct hooks', function() {
56+
assert.equal(typeof plugin.didBuild, 'function');
6057
});
6158

6259
describe('didBuild hook', function() {
6360
it('generates index.json from index.html', function() {
64-
var plugin = subject.createDeployPlugin({
65-
name: 'json-config'
66-
});
67-
68-
var context = {
69-
distDir: 'dist',
70-
deployment: {
71-
project: { root: fakeRoot },
72-
ui: {write: function() {}, writeLine: function() {}},
73-
config: {
74-
'json-config': {
75-
fileInputPattern: 'index.html',
76-
fileOutputPattern: 'index.json'
77-
}
78-
}
79-
},
80-
data: {}
81-
};
82-
83-
var promise = plugin.didBuild.call(plugin, context);
8461
return assert.isFulfilled(promise)
8562
.then(function() {
8663
var json = require(fakeRoot + '/dist/index.json');
@@ -97,26 +74,6 @@ describe('the deploy plugin object', function() {
9774
});
9875

9976
it ('returns the index.json path', function() {
100-
var plugin = subject.createDeployPlugin({
101-
name: 'json-config'
102-
});
103-
104-
var data = {};
105-
var context = {
106-
distDir: 'dist',
107-
deployment: {
108-
project: { root: fakeRoot },
109-
ui: {write: function() {}, writeLine: function() {}},
110-
config: {
111-
'json-config': {
112-
fileInputPattern: 'index.html',
113-
fileOutputPattern: 'index.json'
114-
}
115-
}
116-
}
117-
};
118-
119-
var promise = plugin.didBuild.call(plugin, context);
12077
return assert.isFulfilled(promise)
12178
.then(function(result) {
12279
assert.deepEqual(result.distFiles, ['index.json']);

tests/unit/lib/utilities/validate-config-nodetest.js

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)