Skip to content
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

Upload to s3 #1

Merged
merged 1 commit into from
Apr 28, 2015
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
81 changes: 80 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,85 @@
/* jshint node: true */
'use strict';

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

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

var validateConfig = require('./lib/utilities/validate-config');
var S3 = require('./lib/s3');

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

createDeployPlugin: function(options) {
function _beginMessage(ui, filesToUpload, bucket) {
ui.write(blue('| '));
ui.writeLine(blue('- uploading ' + filesToUpload.length + ' files to `' + bucket + '`'));

return Promise.resolve();
}

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

return Promise.resolve();
}

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

return Promise.reject(error);
}

return {
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'));
});
},

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

var filePattern = config.filePattern;
var distDir = context.distDir;
var distFiles = context.distFiles || [];
var filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));

var s3 = context.s3Client || new S3({
ui: ui,
config: config,
client: context.client
});

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

return _beginMessage(ui, filesToUpload, config.bucket)
.then(s3.upload.bind(s3, options))
.then(_successMessage.bind(this, ui, filesToUpload))
.catch(_errorMessage.bind(this, ui));
}
};
}
};
78 changes: 78 additions & 0 deletions lib/s3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var CoreObject = require('core-object');
var fs = require('fs');
var path = require('path');
var mime = require('mime');

var Promise = require('ember-cli/lib/ext/promise');
var denodeify = Promise.denodeify;
var readFile = denodeify(fs.readFile);

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;

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

this._ui = options.ui;
},

upload: function(options) {
options = options || {};

var ui = this._ui;
var cwd = options.cwd;
var bucket = options.bucket;
var prefix = options.prefix;
var acl = options.acl || 'public-read';
var filePaths = options.filePaths || [];
var cacheControl = 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public';
var expires = EXPIRE_IN_2030;

if (typeof filePaths === 'string') {
filePaths = [filePaths];
}

var promises = filePaths.map(function(filePath) {
var basePath = path.join(cwd, filePath);
var data = fs.readFileSync(basePath);
var contentType = mime.lookup(basePath);
var key = path.join(prefix, filePath);

var params = {
Bucket: bucket,
ACL: acl,
Body: data,
ContentType: contentType,
Key: key,
CacheControl: cacheControl,
Expires: expires
};

return new Promise(function(resolve, reject) {
this._client.putObject(params, function(error, data) {
if (error) {
reject(error);
} else {
ui.write(blue('| '));
ui.writeLine(blue('- uploaded: ' + filePath));

resolve();
}
});
}.bind(this));
}.bind(this));

return Promise.all(promises);
}
});
39 changes: 39 additions & 0 deletions lib/utilities/validate-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
var Promise = require('ember-cli/lib/ext/promise');

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

module.exports = function(ui, config) {
ui.write(blue('| '));
ui.write(blue('- validating config\n'));

var defaultConfig = {
region: 'us-east-1',
filePattern: '**/*.{js,css,png,gif,jpg,map,xml,txt}',
prefix: ''
};

['region', 'filePattern', 'prefix'].forEach(function(prop) {
if (!config[prop]) {
var value = defaultConfig[prop];
config[prop] = value;
ui.write(blue('| '));
ui.writeLine(yellow('- Missing config: ' + prop + ', using default: `' + value + '`'));
}
});

var promise = Promise.resolve();

['accessKeyId', 'secretAccessKey', 'bucket'].forEach(function(prop) {
if (!config[prop]) {
ui.write(blue('| '));
ui.writeLine(red('- Missing config: `' + prop + '`'));

promise = Promise.reject();
}
});

return promise;
}
20 changes: 15 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"scripts": {
"start": "ember server",
"build": "ember build",
"test": "ember try:testall"
"test": "node tests/runner.js"
},
"repository": "https://github.com/zapnito/ember-cli-deploy-s3",
"engines": {
Expand All @@ -19,6 +19,8 @@
"license": "MIT",
"devDependencies": {
"broccoli-asset-rev": "^2.0.2",
"chai": "^2.2.0",
"chai-as-promised": "^5.0.0",
"ember-cli": "0.2.3",
"ember-cli-app-version": "0.3.3",
"ember-cli-content-security-policy": "0.4.0",
Expand All @@ -29,15 +31,23 @@
"ember-cli-qunit": "0.3.10",
"ember-cli-uglify": "1.0.1",
"ember-data": "1.0.0-beta.16.1",
"ember-export-application-global": "^1.0.2",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-try": "0.0.4"
"ember-export-application-global": "^1.0.2",
"ember-try": "0.0.4",
"glob": "^5.0.5",
"mocha": "^2.2.4"
},
"keywords": [
"ember-addon"
"ember-addon",
"ember-cli-deploy-plugin"
],
"dependencies": {
"ember-cli-babel": "^5.0.0"
"aws-sdk": "^2.1.25",
"chalk": "^1.0.0",
"core-object": "^1.1.0",
"ember-cli-babel": "^5.0.0",
"mime": "^1.3.4",
"minimatch": "^2.0.4"
},
"ember-addon": {
"configPath": "tests/dummy/config"
Expand Down
Empty file added tests/fixtures/dist/app.css
Empty file.
1 change: 1 addition & 0 deletions tests/fixtures/dist/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some content here
27 changes: 27 additions & 0 deletions tests/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

var glob = require('glob');
var Mocha = require('mocha');

var mocha = new Mocha({
reporter: 'spec'
});

var arg = process.argv[2];
var root = 'tests/';

function addFiles(mocha, files) {
glob.sync(root + files).forEach(mocha.addFile.bind(mocha));
}

addFiles(mocha, '/**/*-nodetest.js');

if (arg === 'all') {
addFiles(mocha, '/**/*-nodetest-slow.js');
}

mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
Loading