Skip to content

Commit ed472da

Browse files
author
Aaron Chambers
committed
Merge pull request #12 from lukemelia/plugin-base-class-restructure
Plugin base class restructure
2 parents 8411ae1 + a0cfa62 commit ed472da

File tree

5 files changed

+112
-200
lines changed

5 files changed

+112
-200
lines changed

index.js

+29-55
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,39 @@
22
'use strict';
33

44
var Promise = require('ember-cli/lib/ext/promise');
5-
65
var glob = require('glob');
7-
var chalk = require('chalk');
8-
var blue = chalk.blue;
9-
var validateConfig = require('./lib/utilities/validate-config');
6+
var DeployPluginBase = require('ember-cli-deploy-plugin');
107

118
module.exports = {
129
name: 'ember-cli-deploy-build',
1310

1411
createDeployPlugin: function(options) {
15-
function _beginMessage(ui, buildEnv, outputPath) {
16-
ui.write(blue('| '));
17-
ui.writeLine(blue('- building app to `' + outputPath + '` using buildEnv `' + buildEnv + '`...'));
18-
19-
return Promise.resolve();
20-
}
21-
22-
function _successMessage(ui, outputPath) {
23-
var files = glob.sync('**/**/*', { nonull: false, nodir: true, cwd: outputPath });
24-
25-
if (files && files.length) {
26-
files.forEach(function(path) {
27-
ui.write(blue('| '));
28-
ui.writeLine(blue('- ✔ ' + path));
29-
});
30-
}
31-
32-
ui.write(blue('| '));
33-
ui.writeLine(blue('- build ok'));
34-
35-
return Promise.resolve(files);
36-
}
37-
38-
return {
12+
var DeployPlugin = DeployPluginBase.extend({
3913
name: options.name,
40-
41-
configure: function(context) {
42-
var deployment = context.deployment;
43-
var ui = deployment.ui;
44-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
45-
46-
return validateConfig(ui, config)
47-
.then(function() {
48-
ui.write(blue('| '));
49-
ui.writeLine(blue('- config ok'));
50-
});
14+
defaultConfig: {
15+
environment: 'production',
16+
outputPath: 'tmp/deploy-dist'
5117
},
5218

5319
build: function(context) {
54-
var deployment = context.deployment;
55-
var ui = deployment.ui;
56-
var project = deployment.project;
57-
var config = deployment.config[this.name] || {};
58-
59-
var outputPath = config.outputPath;
60-
var buildEnv = config.buildEnv || 'production';
20+
var self = this;
21+
var outputPath = this.readConfig('outputPath');
22+
var buildEnv = this.readConfig('environment');
6123

6224
var Builder = require('ember-cli/lib/models/builder');
6325
var builder = new Builder({
64-
ui: ui,
26+
ui: this.ui,
6527
outputPath: outputPath,
6628
environment: buildEnv,
67-
project: project
29+
project: this.project
6830
});
6931

70-
return _beginMessage(ui, buildEnv, outputPath)
71-
.then(builder.build.bind(builder))
32+
this.log('building app to `' + outputPath + '` using buildEnv `' + buildEnv + '`...');
33+
return builder.build()
7234
.finally(function() {
7335
return builder.cleanup();
7436
})
75-
.then(_successMessage.bind(this, ui, outputPath))
37+
.then(this._logSuccess.bind(this, outputPath))
7638
.then(function(files) {
7739
files = files || [];
7840

@@ -82,12 +44,24 @@ module.exports = {
8244
};
8345
})
8446
.catch(function(error) {
85-
ui.write(blue('| '));
86-
ui.writeLine(chalk.red('build failed'));
87-
47+
self.log('build failed', { color: 'red' });
8848
return Promise.reject(error);
8949
});
50+
},
51+
_logSuccess: function(outputPath) {
52+
var self = this;
53+
var files = glob.sync('**/**/*', { nonull: false, nodir: true, cwd: outputPath });
54+
55+
if (files && files.length) {
56+
files.forEach(function(path) {
57+
self.log('✔ ' + path);
58+
});
59+
}
60+
self.log('build ok');
61+
62+
return Promise.resolve(files);
9063
}
91-
}
64+
});
65+
return new DeployPlugin();
9266
}
9367
};

lib/utilities/validate-config.js

-29
This file was deleted.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
],
4444
"dependencies": {
4545
"chalk": "^1.0.0",
46+
"ember-cli-deploy-plugin": "0.1.1",
4647
"ember-cli-babel": "^5.0.0",
4748
"glob": "^5.0.5",
4849
"rsvp": "^3.0.18"

tests/unit/index-nodetest.js

+82-35
Original file line numberDiff line numberDiff line change
@@ -6,78 +6,125 @@ var RSVP = require('ember-cli/lib/ext/promise');
66
var assert = require('ember-cli/tests/helpers/assert');
77

88
describe('build plugin', function() {
9-
var subject;
9+
var subject, mockUi, config;
1010

11-
before(function() {
11+
beforeEach(function() {
1212
subject = require('../../index');
13+
mockUi = {
14+
messages: [],
15+
write: function() { },
16+
writeLine: function(message) {
17+
this.messages.push(message);
18+
}
19+
};
1320
});
1421

1522
it('has a name', function() {
16-
var result = subject.createDeployPlugin({
23+
var plugin = subject.createDeployPlugin({
1724
name: 'test-plugin'
1825
});
1926

20-
assert.equal(result.name, 'test-plugin');
27+
assert.equal(plugin.name, 'test-plugin');
2128
});
2229

2330
it('implements the correct hooks', function() {
24-
var result = subject.createDeployPlugin({
31+
var plugin = subject.createDeployPlugin({
2532
name: 'test-plugin'
2633
});
2734

28-
assert.equal(typeof result.configure, 'function');
29-
assert.equal(typeof result.build, 'function');
35+
assert.equal(typeof plugin.configure, 'function');
36+
assert.equal(typeof plugin.build, 'function');
3037
});
3138

3239
describe('configure hook', function() {
33-
it('resolves if config is ok', function() {
34-
var plugin = subject.createDeployPlugin({
35-
name: 'build'
40+
var plugin, context;
41+
describe('without providing config', function () {
42+
beforeEach(function() {
43+
config = { };
44+
plugin = subject.createDeployPlugin({
45+
name: 'build'
46+
});
47+
context = {
48+
ui: mockUi,
49+
config: config
50+
};
51+
plugin.beforeHook(context);
3652
});
53+
it('warns about missing optional config', function() {
54+
plugin.configure(context);
55+
var messages = mockUi.messages.reduce(function(previous, current) {
56+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
57+
previous.push(current);
58+
}
59+
60+
return previous;
61+
}, []);
62+
63+
assert.equal(messages.length, 2);
64+
});
65+
66+
it('adds default config to the config object', function() {
67+
plugin.configure(context);
68+
assert.isDefined(config.build.environment);
69+
assert.isDefined(config.build.outputPath);
70+
});
71+
});
3772

38-
var context = {
39-
deployment: {
40-
ui: { write: function() {}, writeLine: function() {} },
41-
config: {
73+
describe('with a build environment and outputPath provided', function () {
74+
beforeEach(function() {
75+
config = {
76+
build: {
77+
environment: 'development',
78+
outputPath: 'tmp/dist-deploy'
4279
}
43-
}
44-
};
45-
return assert.isFulfilled(plugin.configure.call(plugin, context));
80+
};
81+
plugin = subject.createDeployPlugin({
82+
name: 'build'
83+
});
84+
context = {
85+
ui: mockUi,
86+
config: config
87+
};
88+
plugin.beforeHook(context);
89+
});
90+
it('does not warn about missing optional config', function() {
91+
plugin.configure(context);
92+
var messages = mockUi.messages.reduce(function(previous, current) {
93+
if (/- Missing config:\s.*, using default:\s/.test(current)) {
94+
previous.push(current);
95+
}
96+
97+
return previous;
98+
}, []);
99+
assert.equal(messages.length, 0);
100+
});
46101
});
47102
});
48103

49104
describe('build hook', function() {
50-
var plugin;
51-
var context;
105+
var plugin, context;
52106

53107
beforeEach(function() {
54108
plugin = subject.createDeployPlugin({
55109
name: 'build'
56110
});
57111

58112
context = {
59-
redisClient: {
60-
upload: function() {
61-
return RSVP.resolve('redis-key');
62-
}
63-
},
64-
tag: 'some-tag',
65-
deployment: {
66-
ui: { write: function() {}, writeLine: function() {} },
67-
project: { name: function() { return 'test-project'; }, addons: [], root: 'tests/dummy' },
68-
config: {
69-
build: {
70-
buildEnv: 'development',
71-
outputPath: 'tmp/dist-deploy',
72-
}
113+
ui: mockUi,
114+
project: { name: function() { return 'test-project'; }, addons: [], root: 'tests/dummy' },
115+
config: {
116+
build: {
117+
buildEnv: 'development',
118+
outputPath: 'tmp/dist-deploy',
73119
}
74120
}
75121
};
122+
plugin.beforeHook(context);
76123
});
77124

78-
it('builds the app and returns distDir and distFiles', function(done) {
125+
it('builds the app and resolves with distDir and distFiles', function(done) {
79126
this.timeout(50000);
80-
return assert.isFulfilled(plugin.build.call(plugin, context))
127+
return assert.isFulfilled(plugin.build(context))
81128
.then(function(result) {
82129
assert.deepEqual(result, {
83130
distDir: 'tmp/dist-deploy',

0 commit comments

Comments
 (0)