Skip to content

Commit 6ce7f9b

Browse files
author
Aaron Chambers
committed
Merge pull request #7 from lukemelia/plugin-base-class-restructure
Restructure to make more things pull from config, and use ember-cli-deploy-plugin base class
2 parents 7fa0316 + 5bfd06a commit 6ce7f9b

File tree

7 files changed

+233
-311
lines changed

7 files changed

+233
-311
lines changed

index.js

Lines changed: 57 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,85 +3,84 @@
33

44
var Promise = require('ember-cli/lib/ext/promise');
55
var minimatch = require('minimatch');
6-
7-
var chalk = require('chalk');
8-
var blue = chalk.blue;
9-
var red = chalk.red;
10-
11-
var validateConfig = require('./lib/utilities/validate-config');
6+
var DeployPluginBase = require('ember-cli-deploy-plugin');
127
var S3 = require('./lib/s3');
138

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

1712
createDeployPlugin: function(options) {
18-
function _beginMessage(ui, filesToUpload, bucket) {
19-
ui.write(blue('| '));
20-
ui.writeLine(blue('- preparing to upload to S3 bucket `' + bucket + '`'));
21-
22-
return Promise.resolve();
23-
}
24-
25-
function _successMessage(ui, filesUploaded) {
26-
ui.write(blue('| '));
27-
ui.writeLine(blue('- uploaded ' + filesUploaded.length + ' files ok'));
28-
29-
return Promise.resolve();
30-
}
31-
32-
function _errorMessage(ui, error) {
33-
ui.write(blue('| '));
34-
ui.writeLine(red('- ' + error));
35-
36-
return Promise.reject(error);
37-
}
38-
39-
return {
13+
var DeployPlugin = DeployPluginBase.extend({
4014
name: options.name,
41-
42-
willDeploy: function(context) {
43-
var deployment = context.deployment;
44-
var ui = deployment.ui;
45-
var config = deployment.config[this.name] = deployment.config[this.name] || {};
46-
47-
return validateConfig(ui, config)
48-
.then(function() {
49-
ui.write(blue('| '));
50-
ui.writeLine(blue('- config ok'));
51-
});
15+
defaultConfig: {
16+
region: 'us-east-1',
17+
filePattern: '**/*.{js,css,png,gif,jpg,map,xml,txt,svg,eot,ttf,woff,woff2}',
18+
prefix: '',
19+
distDir: function(context) {
20+
return context.distDir;
21+
},
22+
distFiles: function(context) {
23+
return context.distFiles || [];
24+
},
25+
gzippedFiles: function(context) {
26+
return context.gzippedFiles || []; // e.g. from ember-cli-deploy-gzip
27+
},
28+
manifestPath: function(context) {
29+
return context.manifestPath; // e.g. from ember-cli-deploy-manifest
30+
},
31+
uploadClient: function(context) {
32+
return context.uploadClient; // if you want to provide your own upload client to be used instead of one from this plugin
33+
},
34+
s3Client: function(context) {
35+
return context.s3Client; // if you want to provide your own S3 client to be used instead of one from aws-sdk
36+
}
5237
},
38+
requiredConfig: ['accessKeyId', 'secretAccessKey', 'bucket'],
5339

5440
upload: function(context) {
55-
var deployment = context.deployment;
56-
var ui = deployment.ui;
57-
var config = deployment.config[this.name];
41+
debugger;
42+
var self = this;
43+
44+
var filePattern = this.readConfig('filePattern');
45+
var distDir = this.readConfig('distDir');
46+
var distFiles = this.readConfig('distFiles');
47+
var gzippedFiles = this.readConfig('gzippedFiles');
48+
var bucket = this.readConfig('bucket');
49+
var prefix = this.readConfig('prefix');
50+
var manifestPath = this.readConfig('manifestPath');
5851

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

65-
var s3 = context.s3Client || new S3({
66-
ui: ui,
67-
config: config,
68-
client: context.client
54+
var s3 = this.readConfig('uploadClient') || new S3({
55+
plugin: this
6956
});
7057

7158
var options = {
7259
cwd: distDir,
7360
filePaths: filesToUpload,
7461
gzippedFilePaths: gzippedFiles,
75-
prefix: config.prefix,
76-
bucket: config.bucket,
77-
manifestPath: context.manifestPath
62+
prefix: prefix,
63+
bucket: bucket,
64+
manifestPath: manifestPath
7865
};
7966

80-
return _beginMessage(ui, filesToUpload, config.bucket)
81-
.then(s3.upload.bind(s3, options))
82-
.then(_successMessage.bind(this, ui))
83-
.catch(_errorMessage.bind(this, ui));
67+
this.log('preparing to upload to S3 bucket `' + bucket + '`');
68+
69+
return s3.upload(options)
70+
.then(function(filesUploaded){
71+
self.log('uploaded ' + filesUploaded.length + ' files ok');
72+
return { filesUploaded: filesUploaded };
73+
})
74+
.catch(this._errorMessage.bind(this));
75+
},
76+
_errorMessage: function(error) {
77+
this.log(error, { color: 'red' });
78+
if (error) {
79+
this.log(error.stack, { color: 'red' });
80+
}
81+
return Promise.reject(error);
8482
}
85-
};
83+
});
84+
return new DeployPlugin();
8685
}
8786
};

lib/s3.js

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,18 @@ var mime = require('mime');
77
var Promise = require('ember-cli/lib/ext/promise');
88

99
var _ = require('lodash');
10-
var chalk = require('chalk');
11-
var blue = chalk.blue;
1210
var EXPIRE_IN_2030 = new Date('2030');
1311
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;
1412

1513
module.exports = CoreObject.extend({
1614
init: function(options) {
17-
var config = options.config;
18-
15+
this._plugin = options.plugin;
1916
var AWS = require('aws-sdk');
20-
this._client = options.client || new AWS.S3({
21-
accessKeyId: config.accessKeyId,
22-
secretAccessKey: config.secretAccessKey,
23-
region: config.region
17+
this._client = this._plugin.readConfig('s3Client') || new AWS.S3({
18+
accessKeyId: this._plugin.readConfig('accessKeyId'),
19+
secretAccessKey: this._plugin.readConfig('secretAccessKey'),
20+
region: this._plugin.readConfig('region')
2421
});
25-
26-
this._ui = options.ui;
2722
},
2823

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

3631
_determineFilePaths: function(options) {
37-
var ui = this._ui;
32+
var plugin = this._plugin;
3833
var filePaths = options.filePaths || [];
3934
if (typeof filePaths === 'string') {
4035
filePaths = [filePaths];
@@ -43,8 +38,7 @@ module.exports = CoreObject.extend({
4338
var manifestPath = options.manifestPath;
4439
if (manifestPath) {
4540
var key = path.join(prefix, manifestPath);
46-
ui.write(blue('| '));
47-
ui.writeLine(blue("- Downloading manifest for differential deploy from `" + key + "`..."));
41+
plugin.log('Downloading manifest for differential deploy from `' + key + '`...');
4842
return new Promise(function(resolve, reject){
4943
var params = { Bucket: options.bucket, Key: key};
5044
this._client.getObject(params, function(error, data) {
@@ -55,13 +49,10 @@ module.exports = CoreObject.extend({
5549
}
5650
}.bind(this));
5751
}.bind(this)).then(function(manifestEntries){
58-
ui.write(blue('| '));
59-
ui.writeLine(blue("- Manifest found. Differential deploy will be applied."));
52+
plugin.log("Manifest found. Differential deploy will be applied.");
6053
return _.difference(filePaths, manifestEntries);
6154
}).catch(function(reason){
62-
console.log(reason);
63-
ui.write(blue('| '));
64-
ui.writeLine(blue("- Manifest not found. Disabling differential deploy."));
55+
plugin.log("Manifest not found. Disabling differential deploy.", { color: 'yellow' });
6556
return Promise.resolve(filePaths);
6657
});
6758
} else {
@@ -70,7 +61,7 @@ module.exports = CoreObject.extend({
7061
},
7162

7263
_putObjects: function(filePaths, options) {
73-
var ui = this._ui;
64+
var plugin = this._plugin;
7465
var cwd = options.cwd;
7566
var bucket = options.bucket;
7667
var prefix = options.prefix;
@@ -110,8 +101,7 @@ module.exports = CoreObject.extend({
110101
if (error) {
111102
reject(error);
112103
} else {
113-
ui.write(blue('| '));
114-
ui.writeLine(blue('- ✔ ' + key));
104+
plugin.log('✔ ' + key);
115105
resolve(filePath);
116106
}
117107
});

lib/utilities/validate-config.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"aws-sdk": "^2.1.25",
4646
"chalk": "^1.0.0",
4747
"core-object": "^1.1.0",
48+
"ember-cli-deploy-plugin": "0.1.1",
4849
"ember-cli-babel": "^5.0.0",
4950
"lodash": "^3.9.3",
5051
"mime": "^1.3.4",

0 commit comments

Comments
 (0)