Skip to content

Commit

Permalink
Restructure to make more things pull from config, and use ember-cli-d…
Browse files Browse the repository at this point in the history
…eploy-plugin base class
  • Loading branch information
lukemelia committed Jul 13, 2015
1 parent 7fa0316 commit a34fac9
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 311 deletions.
115 changes: 57 additions & 58 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,84 @@

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

var chalk = require('chalk');
var blue = chalk.blue;
var red = chalk.red;

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

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

createDeployPlugin: function(options) {
function _beginMessage(ui, filesToUpload, bucket) {
ui.write(blue('| '));
ui.writeLine(blue('- preparing to upload to S3 bucket `' + bucket + '`'));

return Promise.resolve();
}

function _successMessage(ui, filesUploaded) {
ui.write(blue('| '));
ui.writeLine(blue('- uploaded ' + filesUploaded.length + ' files ok'));

return Promise.resolve();
}

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

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: {
region: 'us-east-1',
filePattern: '**/*.{js,css,png,gif,jpg,map,xml,txt,svg,eot,ttf,woff,woff2}',
prefix: '',
distDir: function(context) {
return context.distDir;
},
distFiles: function(context) {
return context.distFiles || [];
},
gzippedFiles: function(context) {
return context.gzippedFiles || []; // e.g. from ember-cli-deploy-gzip
},
manifestPath: function(context) {
return context.manifestPath; // e.g. from ember-cli-deploy-manifest
},
uploadClient: function(context) {
return context.uploadClient; // if you want to provide your own upload client to be used instead of one from this plugin
},
s3Client: function(context) {
return context.s3Client; // if you want to provide your own S3 client to be used instead of one from aws-sdk
}
},
requiredConfig: ['accessKeyId', 'secretAccessKey', 'bucket'],

upload: function(context) {
var deployment = context.deployment;
var ui = deployment.ui;
var config = deployment.config[this.name];
debugger;
var self = this;

var filePattern = this.readConfig('filePattern');
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles');
var gzippedFiles = this.readConfig('gzippedFiles');
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
var manifestPath = this.readConfig('manifestPath');

var filePattern = config.filePattern;
var distDir = context.distDir;
var distFiles = context.distFiles || [];
var gzippedFiles = context.gzippedFiles || []; // e.g. from ember-cli-deploy-gzip
var filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));

var s3 = context.s3Client || new S3({
ui: ui,
config: config,
client: context.client
var s3 = this.readConfig('uploadClient') || new S3({
plugin: this
});

var options = {
cwd: distDir,
filePaths: filesToUpload,
gzippedFilePaths: gzippedFiles,
prefix: config.prefix,
bucket: config.bucket,
manifestPath: context.manifestPath
prefix: prefix,
bucket: bucket,
manifestPath: manifestPath
};

return _beginMessage(ui, filesToUpload, config.bucket)
.then(s3.upload.bind(s3, options))
.then(_successMessage.bind(this, ui))
.catch(_errorMessage.bind(this, ui));
this.log('preparing to upload to S3 bucket `' + bucket + '`');

return s3.upload(options)
.then(function(filesUploaded){
self.log('uploaded ' + filesUploaded.length + ' files ok');
return { filesUploaded: filesUploaded };
})
.catch(this._errorMessage.bind(this));
},
_errorMessage: function(error) {
this.log(error, { color: 'red' });
if (error) {
this.log(error.stack, { color: 'red' });
}
return Promise.reject(error);
}
};
});
return new DeployPlugin();
}
};
32 changes: 11 additions & 21 deletions lib/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,18 @@ var mime = require('mime');
var Promise = require('ember-cli/lib/ext/promise');

var _ = require('lodash');
var chalk = require('chalk');
var blue = chalk.blue;
var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;

module.exports = CoreObject.extend({
init: function(options) {
var config = options.config;

this._plugin = options.plugin;
var AWS = require('aws-sdk');
this._client = options.client || new AWS.S3({
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
region: config.region
this._client = this._plugin.readConfig('s3Client') || new AWS.S3({
accessKeyId: this._plugin.readConfig('accessKeyId'),
secretAccessKey: this._plugin.readConfig('secretAccessKey'),
region: this._plugin.readConfig('region')
});

this._ui = options.ui;
},

upload: function(options) {
Expand All @@ -34,7 +29,7 @@ module.exports = CoreObject.extend({
},

_determineFilePaths: function(options) {
var ui = this._ui;
var plugin = this._plugin;
var filePaths = options.filePaths || [];
if (typeof filePaths === 'string') {
filePaths = [filePaths];
Expand All @@ -43,8 +38,7 @@ module.exports = CoreObject.extend({
var manifestPath = options.manifestPath;
if (manifestPath) {
var key = path.join(prefix, manifestPath);
ui.write(blue('| '));
ui.writeLine(blue("- Downloading manifest for differential deploy from `" + key + "`..."));
plugin.log('Downloading manifest for differential deploy from `' + key + '`...');
return new Promise(function(resolve, reject){
var params = { Bucket: options.bucket, Key: key};
this._client.getObject(params, function(error, data) {
Expand All @@ -55,13 +49,10 @@ module.exports = CoreObject.extend({
}
}.bind(this));
}.bind(this)).then(function(manifestEntries){
ui.write(blue('| '));
ui.writeLine(blue("- Manifest found. Differential deploy will be applied."));
plugin.log("Manifest found. Differential deploy will be applied.");
return _.difference(filePaths, manifestEntries);
}).catch(function(reason){
console.log(reason);
ui.write(blue('| '));
ui.writeLine(blue("- Manifest not found. Disabling differential deploy."));
plugin.log("Manifest not found. Disabling differential deploy.", { color: 'yellow' });
return Promise.resolve(filePaths);
});
} else {
Expand All @@ -70,7 +61,7 @@ module.exports = CoreObject.extend({
},

_putObjects: function(filePaths, options) {
var ui = this._ui;
var plugin = this._plugin;
var cwd = options.cwd;
var bucket = options.bucket;
var prefix = options.prefix;
Expand Down Expand Up @@ -110,8 +101,7 @@ module.exports = CoreObject.extend({
if (error) {
reject(error);
} else {
ui.write(blue('| '));
ui.writeLine(blue('- ✔ ' + key));
plugin.log('✔ ' + key);
resolve(filePath);
}
});
Expand Down
39 changes: 0 additions & 39 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 @@
"aws-sdk": "^2.1.25",
"chalk": "^1.0.0",
"core-object": "^1.1.0",
"ember-cli-deploy-plugin": "0.1.0",
"ember-cli-babel": "^5.0.0",
"lodash": "^3.9.3",
"mime": "^1.3.4",
Expand Down
Loading

0 comments on commit a34fac9

Please sign in to comment.