|
3 | 3 |
|
4 | 4 | var Promise = require('ember-cli/lib/ext/promise');
|
5 | 5 | 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'); |
12 | 7 | var S3 = require('./lib/s3');
|
13 | 8 |
|
14 | 9 | module.exports = {
|
15 | 10 | name: 'ember-cli-deploy-s3',
|
16 | 11 |
|
17 | 12 | 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({ |
40 | 14 | 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 | + } |
52 | 37 | },
|
| 38 | + requiredConfig: ['accessKeyId', 'secretAccessKey', 'bucket'], |
53 | 39 |
|
54 | 40 | 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'); |
58 | 51 |
|
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 |
63 | 52 | var filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true }));
|
64 | 53 |
|
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 |
69 | 56 | });
|
70 | 57 |
|
71 | 58 | var options = {
|
72 | 59 | cwd: distDir,
|
73 | 60 | filePaths: filesToUpload,
|
74 | 61 | gzippedFilePaths: gzippedFiles,
|
75 |
| - prefix: config.prefix, |
76 |
| - bucket: config.bucket, |
77 |
| - manifestPath: context.manifestPath |
| 62 | + prefix: prefix, |
| 63 | + bucket: bucket, |
| 64 | + manifestPath: manifestPath |
78 | 65 | };
|
79 | 66 |
|
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); |
84 | 82 | }
|
85 |
| - }; |
| 83 | + }); |
| 84 | + return new DeployPlugin(); |
86 | 85 | }
|
87 | 86 | };
|
0 commit comments