forked from ember-cli-deploy/ember-cli-deploy-s3-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
143 lines (123 loc) · 4.75 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
132
133
134
135
136
137
138
139
140
141
142
143
/* jshint node: true */
'use strict';
var DeployPluginBase = require('ember-cli-deploy-plugin');
var S3 = require('./lib/s3');
var joinUriSegments = require('./lib/util/join-uri-segments');
module.exports = {
name: 'ember-cli-deploy-s3-index',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
S3: options.S3 || S3,
defaultConfig: {
filePattern: 'index.html',
prefix: '',
acl: 'public-read',
cacheControl: 'max-age=0, no-cache',
urlEncodeSourceObject: true,
distDir: function(context) {
return context.distDir;
},
revisionKey: function(context) {
var revisionKey = context.revisionData && context.revisionData.revisionKey;
return context.commandOptions.revision || revisionKey;
},
s3Client: function(context) {
return context.s3Client; // if you want to provide your own S3 client to be used instead of one from aws-sdk
},
gzippedFiles: function(context) {
return context.gzippedFiles || [];
},
brotliCompressedFiles: function(context) {
return context.brotliCompressedFiles || [];
},
allowOverwrite: false
},
requiredConfig: ['bucket', 'region'],
upload: function(/* context */) {
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
var acl = this.readConfig('acl');
var cacheControl = this.readConfig('cacheControl');
var revisionKey = this.readConfig('revisionKey');
var distDir = this.readConfig('distDir');
var filePattern = this.readConfig('filePattern');
var gzippedFiles = this.readConfig('gzippedFiles');
var brotliCompressedFiles = this.readConfig('brotliCompressedFiles');
var allowOverwrite = this.readConfig('allowOverwrite');
var serverSideEncryption = this.readConfig('serverSideEncryption');
var urlEncodeSourceObject = this.readConfig('urlEncodeSourceObject');
var filePath = joinUriSegments(distDir, filePattern);
var options = {
bucket: bucket,
prefix: prefix,
acl: acl,
cacheControl: cacheControl,
filePattern: filePattern,
filePath: filePath,
revisionKey: revisionKey,
gzippedFilePaths: gzippedFiles,
brotliCompressedFilePaths: brotliCompressedFiles,
allowOverwrite: allowOverwrite,
urlEncodeSourceObject: urlEncodeSourceObject
};
if (serverSideEncryption) {
options.serverSideEncryption = serverSideEncryption;
}
this.log('preparing to upload revision to S3 bucket `' + bucket + '`', { verbose: true });
var s3 = new this.S3({ plugin: this });
return s3.upload(options);
},
activate: function(/* context */) {
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
var acl = this.readConfig('acl');
var revisionKey = this.readConfig('revisionKey');
var filePattern = this.readConfig('filePattern');
var serverSideEncryption = this.readConfig('serverSideEncryption');
var options = {
bucket: bucket,
prefix: prefix,
acl: acl,
filePattern: filePattern,
revisionKey: revisionKey,
};
if (serverSideEncryption) {
options.serverSideEncryption = serverSideEncryption;
}
this.log('preparing to activate `' + revisionKey + '`', { verbose: true });
var s3 = new this.S3({ plugin: this });
return s3.activate(options);
},
fetchRevisions: function(context) {
return this._list(context)
.then(function(revisions) {
return {
revisions: revisions
};
});
},
fetchInitialRevisions: function(context) {
return this._list(context)
.then(function(revisions) {
return {
initialRevisions: revisions
};
});
},
_list: function(/* context */) {
var bucket = this.readConfig('bucket');
var prefix = this.readConfig('prefix');
var filePattern = this.readConfig('filePattern');
var options = {
bucket: bucket,
prefix: prefix,
filePattern: filePattern,
};
var s3 = new this.S3({ plugin: this });
return s3.fetchRevisions(options);
}
});
return new DeployPlugin();
}
};