Skip to content

Restructure to use ember-cli-deploy-plugin #8

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
99 changes: 41 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,71 @@

var path = require('path');
var fs = require('fs');
var chalk = require('chalk');
var RSVP = require('rsvp');
var Promise = RSVP.Promise;
var denodeify = RSVP.denodeify;

var readFile = denodeify(fs.readFile);
var writeFile = denodeify(fs.writeFile);

var blue = chalk.blue;
var red = chalk.red;

var validateConfig = require('./lib/utilities/validate-config');
var extractConfig = require('./lib/utilities/extract-index-config');
var extractConfigFromHtmlAsJson = require('./lib/utilities/extract-index-config');
var DeployPluginBase = require('ember-cli-deploy-plugin');

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

createDeployPlugin: function(options) {
function _beginMessage(ui, inputPath, outputPath) {
ui.write(blue('| '));
ui.writeLine(blue('- generating `' + outputPath + '` from `' + inputPath + '`'));

return Promise.resolve();
}

function _successMessage(ui, outputPath) {
ui.write(blue('| '));
ui.writeLine(blue('- generated: `' + outputPath + '`'));

return Promise.resolve();
}

function _errorMessage(ui, error) {
ui.write(blue('| '));
ui.write(red('- ' + error + '`\n'));

return Promise.reject(error);
}

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

willDeploy: 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: {
fileInputPattern: 'index.html',
fileOutputPattern: 'index.json',
projectRoot: function(context) {
return context.project.root;
},
distDir: function(context) {
return context.distDir;
}
},

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

var root = project.root;
var distDir = context.distDir;
var fileInputPattern = config.fileInputPattern;
var fileOutputPattern = config.fileOutputPattern;
var root = this.readConfig('projectRoot');
var distDir = this.readConfig('distDir');
var fileInputPattern = this.readConfig('fileInputPattern');
var fileOutputPattern = this.readConfig('fileOutputPattern');
var inputPath = path.join(distDir, fileInputPattern);
var outputPath = path.join(distDir, fileOutputPattern);
var absoluteInputPath = path.join(root, inputPath);
var absoluteOutputPath = path.join(root, outputPath);
var absoluteInputPath = path.join(root, inputPath);
var absoluteOutputPath = path.join(root, outputPath);

this.log('generating `' + outputPath + '` from `' + inputPath + '`');

return _beginMessage(ui, inputPath, outputPath)
.then(readFile.bind(readFile, absoluteInputPath))
.then(extractConfig.bind(this))
return readFile(absoluteInputPath)
.then(extractConfigFromHtmlAsJson.bind(this))
.then(writeFile.bind(writeFile, absoluteOutputPath))
.then(_successMessage.bind(this, ui, outputPath))
.then(this._successMessage.bind(this, outputPath, fileOutputPattern))
.then(function() {
ui.write(blue('| '));
ui.writeLine(blue('- added `' + fileOutputPattern + '` to `context.distFiles`'));

return { distFiles: [fileOutputPattern] };
})
.catch(_errorMessage.bind(this, ui));
.catch(this._errorMessage.bind(this));
},

_successMessage: function(outputPath, fileOutputPattern) {
this.log('generated: `' + outputPath + '`');
this.log('added `' + fileOutputPattern + '` to `context.distFiles`');
return Promise.resolve();
},

_errorMessage: function(error) {
this.log(error, { color: 'red' });
if (error) {
this.log(error.stack, { color: 'red' });
}
return Promise.reject(error);
}
}
});

return new DeployPlugin();
}
};
26 changes: 0 additions & 26 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 @@ -45,6 +45,7 @@
"chalk": "^1.0.0",
"cheerio": "^0.19.0",
"ember-cli-babel": "^5.0.0",
"ember-cli-deploy-plugin": "^0.1.1",
"rsvp": "^3.0.18"
},
"ember-addon": {
Expand Down
103 changes: 30 additions & 73 deletions tests/unit/index-nodetest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,59 @@ var path = require('path');
var assert = require('ember-cli/tests/helpers/assert');

describe('the deploy plugin object', function() {
var subject;
var fakeRoot;
var plugin;
var promise;

before(function() {
subject = require('../../index');
fakeRoot = process.cwd() + '/tests/fixtures';
});

beforeEach(function() {
var subject = require('../../index');
var jsonPath = fakeRoot + '/dist/index.json';
if (fs.existsSync(jsonPath)) {
fs.unlinkSync(jsonPath);
}
});

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

plugin = subject.createDeployPlugin({
name: 'json-config'
});

assert.equal('test-plugin', result.name);
});
var context = {
ui: mockUi,
config: {
'json-config': {
fileInputPattern: 'index.html',
fileOutputPattern: 'index.json',
distDir: function(context) {
return 'dist';
},
projectRoot: function(context) {
return fakeRoot;
}
}
}
};

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

assert.equal(typeof result.willDeploy, 'function');
assert.equal(typeof result.didBuild, 'function');
promise = plugin.didBuild.call(plugin, context);
});

describe('willDeploy hook', function() {
it('resolves if config is ok', function() {
var plugin = subject.createDeployPlugin({
name: 'json-config'
});

var context = {
deployment: {
ui: { write: function() {}, writeLine: function() {} },
config: {
'json-config': {
fileInputPattern: 'dist/index.html',
fileOutputPattern: 'dist/index.json'
}
}
}
};
it('has a name', function() {
assert.equal('json-config', plugin.name);
});

return assert.isFulfilled(plugin.willDeploy.call(plugin, context))
});
it('implements the correct hooks', function() {
assert.equal(typeof plugin.didBuild, 'function');
});

describe('didBuild hook', function() {
it('generates index.json from index.html', function() {
var plugin = subject.createDeployPlugin({
name: 'json-config'
});

var context = {
distDir: 'dist',
deployment: {
project: { root: fakeRoot },
ui: {write: function() {}, writeLine: function() {}},
config: {
'json-config': {
fileInputPattern: 'index.html',
fileOutputPattern: 'index.json'
}
}
},
data: {}
};

var promise = plugin.didBuild.call(plugin, context);
return assert.isFulfilled(promise)
.then(function() {
var json = require(fakeRoot + '/dist/index.json');
Expand All @@ -97,26 +74,6 @@ describe('the deploy plugin object', function() {
});

it ('returns the index.json path', function() {
var plugin = subject.createDeployPlugin({
name: 'json-config'
});

var data = {};
var context = {
distDir: 'dist',
deployment: {
project: { root: fakeRoot },
ui: {write: function() {}, writeLine: function() {}},
config: {
'json-config': {
fileInputPattern: 'index.html',
fileOutputPattern: 'index.json'
}
}
}
};

var promise = plugin.didBuild.call(plugin, context);
return assert.isFulfilled(promise)
.then(function(result) {
assert.deepEqual(result.distFiles, ['index.json']);
Expand Down
62 changes: 0 additions & 62 deletions tests/unit/lib/utilities/validate-config-nodetest.js

This file was deleted.