Skip to content

Plugin base class restructure #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 29 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,77 +2,39 @@
'use strict';

var Promise = require('ember-cli/lib/ext/promise');

var glob = require('glob');
var chalk = require('chalk');
var blue = chalk.blue;
var validateConfig = require('./lib/utilities/validate-config');
var DeployPluginBase = require('ember-cli-deploy-plugin');

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

createDeployPlugin: function(options) {
function _beginMessage(ui, buildEnv, outputPath) {
ui.write(blue('| '));
ui.writeLine(blue('- building app to `' + outputPath + '` using buildEnv `' + buildEnv + '`...'));

return Promise.resolve();
}

function _successMessage(ui, outputPath) {
var files = glob.sync('**/**/*', { nonull: false, nodir: true, cwd: outputPath });

if (files && files.length) {
files.forEach(function(path) {
ui.write(blue('| '));
ui.writeLine(blue('- ✔ ' + path));
});
}

ui.write(blue('| '));
ui.writeLine(blue('- build ok'));

return Promise.resolve(files);
}

return {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,

configure: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name] = deployment.config[this.name] || {};

return validateConfig(ui, config)
.then(function() {
ui.write(blue('| '));
ui.writeLine(blue('- config ok'));
});
defaultConfig: {
environment: 'production',
outputPath: 'tmp/deploy-dist'
},

build: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var project = deployment.project;
var config = deployment.config[this.name] || {};

var outputPath = config.outputPath;
var buildEnv = config.buildEnv || 'production';
var self = this;
var outputPath = this.readConfig('outputPath');
var buildEnv = this.readConfig('environment');

var Builder = require('ember-cli/lib/models/builder');
var builder = new Builder({
ui: ui,
ui: this.ui,
outputPath: outputPath,
environment: buildEnv,
project: project
project: this.project
});

return _beginMessage(ui, buildEnv, outputPath)
.then(builder.build.bind(builder))
this.log('building app to `' + outputPath + '` using buildEnv `' + buildEnv + '`...');
return builder.build()
.finally(function() {
return builder.cleanup();
})
.then(_successMessage.bind(this, ui, outputPath))
.then(this._logSuccess.bind(this, outputPath))
.then(function(files) {
files = files || [];

Expand All @@ -82,12 +44,24 @@ module.exports = {
};
})
.catch(function(error) {
ui.write(blue('| '));
ui.writeLine(chalk.red('build failed'));

self.log('build failed', { color: 'red' });
return Promise.reject(error);
});
},
_logSuccess: function(outputPath) {
var self = this;
var files = glob.sync('**/**/*', { nonull: false, nodir: true, cwd: outputPath });

if (files && files.length) {
files.forEach(function(path) {
self.log('✔ ' + path);
});
}
self.log('build ok');

return Promise.resolve(files);
}
}
});
return new DeployPlugin();
}
};
29 changes: 0 additions & 29 deletions lib/utilities/validate-config.js

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
],
"dependencies": {
"chalk": "^1.0.0",
"ember-cli-deploy-plugin": "0.1.1",
"ember-cli-babel": "^5.0.0",
"glob": "^5.0.5",
"rsvp": "^3.0.18"
Expand Down
117 changes: 82 additions & 35 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,125 @@ var RSVP = require('ember-cli/lib/ext/promise');
var assert = require('ember-cli/tests/helpers/assert');

describe('build plugin', function() {
var subject;
var subject, mockUi, config;

before(function() {
beforeEach(function() {
subject = require('../../index');
mockUi = {
messages: [],
write: function() { },
writeLine: function(message) {
this.messages.push(message);
}
};
});

it('has a name', function() {
var result = subject.createDeployPlugin({
var plugin = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(result.name, 'test-plugin');
assert.equal(plugin.name, 'test-plugin');
});

it('implements the correct hooks', function() {
var result = subject.createDeployPlugin({
var plugin = subject.createDeployPlugin({
name: 'test-plugin'
});

assert.equal(typeof result.configure, 'function');
assert.equal(typeof result.build, 'function');
assert.equal(typeof plugin.configure, 'function');
assert.equal(typeof plugin.build, 'function');
});

describe('configure hook', function() {
it('resolves if config is ok', function() {
var plugin = subject.createDeployPlugin({
name: 'build'
var plugin, context;
describe('without providing config', function () {
beforeEach(function() {
config = { };
plugin = subject.createDeployPlugin({
name: 'build'
});
context = {
ui: mockUi,
config: config
};
plugin.beforeHook(context);
});
it('warns about missing optional config', function() {
plugin.configure(context);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}

return previous;
}, []);

assert.equal(messages.length, 2);
});

it('adds default config to the config object', function() {
plugin.configure(context);
assert.isDefined(config.build.environment);
assert.isDefined(config.build.outputPath);
});
});

var context = {
deployment: {
ui: { write: function() {}, writeLine: function() {} },
config: {
describe('with a build environment and outputPath provided', function () {
beforeEach(function() {
config = {
build: {
environment: 'development',
outputPath: 'tmp/dist-deploy'
}
}
};
return assert.isFulfilled(plugin.configure.call(plugin, context));
};
plugin = subject.createDeployPlugin({
name: 'build'
});
context = {
ui: mockUi,
config: config
};
plugin.beforeHook(context);
});
it('does not warn about missing optional config', function() {
plugin.configure(context);
var messages = mockUi.messages.reduce(function(previous, current) {
if (/- Missing config:\s.*, using default:\s/.test(current)) {
previous.push(current);
}

return previous;
}, []);
assert.equal(messages.length, 0);
});
});
});

describe('build hook', function() {
var plugin;
var context;
var plugin, context;

beforeEach(function() {
plugin = subject.createDeployPlugin({
name: 'build'
});

context = {
redisClient: {
upload: function() {
return RSVP.resolve('redis-key');
}
},
tag: 'some-tag',
deployment: {
ui: { write: function() {}, writeLine: function() {} },
project: { name: function() { return 'test-project'; }, addons: [], root: 'tests/dummy' },
config: {
build: {
buildEnv: 'development',
outputPath: 'tmp/dist-deploy',
}
ui: mockUi,
project: { name: function() { return 'test-project'; }, addons: [], root: 'tests/dummy' },
config: {
build: {
buildEnv: 'development',
outputPath: 'tmp/dist-deploy',
}
}
};
plugin.beforeHook(context);
});

it('builds the app and returns distDir and distFiles', function(done) {
it('builds the app and resolves with distDir and distFiles', function(done) {
this.timeout(50000);
return assert.isFulfilled(plugin.build.call(plugin, context))
return assert.isFulfilled(plugin.build(context))
.then(function(result) {
assert.deepEqual(result, {
distDir: 'tmp/dist-deploy',
Expand Down
Loading