-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
131 lines (118 loc) · 4.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*eslint-env node*/
'use strict';
var RSVP = require('rsvp');
var minimatch = require('minimatch');
var DeployPluginBase = require('ember-cli-deploy-plugin');
var S3 = require('./lib/s3');
var EXPIRE_IN_2030 = new Date('2030');
var TWO_YEAR_CACHE_PERIOD_IN_SEC = 60 * 60 * 24 * 365 * 2;
module.exports = {
name: 'ember-cli-deploy-s3',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: '**/*.{js,css,png,gif,ico,jpg,webp,map,xml,txt,svg,swf,eot,ttf,woff,woff2,otf,wasm,json}',
fileIgnorePattern: null,
prefix: '',
profile: '',
acl: 'public-read',
cacheControl: 'max-age='+TWO_YEAR_CACHE_PERIOD_IN_SEC+', public, immutable',
expires: EXPIRE_IN_2030,
dotFolders: false,
batchSize: 0,
defaultMimeType: 'application/octet-stream',
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
},
brotliCompressedFiles: function(context) {
return context.brotliCompressedFiles || []; // 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: ['bucket', 'region'],
upload: function() {
var self = this;
var filePattern = this.readConfig('filePattern');
var fileIgnorePattern = this.readConfig('fileIgnorePattern');
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles');
var gzippedFiles = this.readConfig('gzippedFiles');
var brotliCompressedFiles = this.readConfig('brotliCompressedFiles');
var bucket = this.readConfig('bucket');
var acl = this.readConfig('acl');
var prefix = this.readConfig('prefix');
var manifestPath = this.readConfig('manifestPath');
var cacheControl = this.readConfig('cacheControl');
var expires = this.readConfig('expires');
var dotFolders = this.readConfig('dotFolders');
var serverSideEncryption = this.readConfig('serverSideEncryption');
var batchSize = this.readConfig('batchSize');
var defaultMimeType = this.readConfig('defaultMimeType');
var filesToUpload = distFiles.filter(minimatch.filter(filePattern, { matchBase: true, dot: dotFolders }));
if (fileIgnorePattern) {
filesToUpload = filesToUpload.filter(function(path) {
return !minimatch(path, fileIgnorePattern, { matchBase: true });
});
gzippedFiles = gzippedFiles.filter(function(path) {
return !minimatch(path, fileIgnorePattern, { matchBase: true });
});
brotliCompressedFiles = brotliCompressedFiles.filter(function(path) {
return !minimatch(path, fileIgnorePattern, { matchBase: true });
});
}
var s3 = this.readConfig('uploadClient') || new S3({
plugin: this
});
var options = {
cwd: distDir,
filePaths: filesToUpload,
gzippedFilePaths: gzippedFiles,
brotliCompressedFilePaths: brotliCompressedFiles,
prefix: prefix,
bucket: bucket,
manifestPath: manifestPath,
cacheControl: cacheControl,
expires: expires,
batchSize: batchSize,
defaultMimeType: defaultMimeType
};
if (acl) {
options.acl = acl;
}
if (serverSideEncryption) {
options.serverSideEncryption = serverSideEncryption;
}
this.log('preparing to upload to S3 bucket `' + bucket + '`', { verbose: true });
return s3.upload(options)
.then(function(filesUploaded){
self.log('uploaded ' + filesUploaded.length + ' files ok', { verbose: true });
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 RSVP.reject(error);
}
});
return new DeployPlugin();
}
};