forked from k1LoW/serverless-s3-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (169 loc) · 5.3 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';
const BbPromise = require('bluebird');
const s3 = require('@auth0/s3');
const chalk = require('chalk');
const minimatch = require('minimatch');
const path = require('path');
const messagePrefix = 'S3 Sync: ';
class ServerlessS3Sync {
constructor(serverless, options) {
this.serverless = serverless;
this.options = options || {};
this.servicePath = this.serverless.service.serverless.config.servicePath;
this.commands = {
s3sync: {
usage: 'Sync directories and S3 prefixes',
lifecycleEvents: [
'sync'
]
}
};
this.hooks = {
'after:deploy:deploy': () => BbPromise.bind(this).then(this.sync),
'before:remove:remove': () => BbPromise.bind(this).then(this.clear),
's3sync:sync': () => BbPromise.bind(this).then(this.sync)
};
}
client() {
const provider = this.serverless.getProvider('aws');
const awsCredentials = provider.getCredentials();
const s3Client = new provider.sdk.S3({
region: awsCredentials.region,
credentials: awsCredentials.credentials,
});
return s3.createClient({ s3Client });
}
sync() {
const s3Sync = this.serverless.service.custom.s3Sync;
const cli = this.serverless.cli;
if (!Array.isArray(s3Sync)) {
cli.consoleLog(`${messagePrefix}${chalk.red('No configuration found')}`)
return Promise.resolve();
}
cli.consoleLog(`${messagePrefix}${chalk.yellow('Syncing directories and S3 prefixes...')}`);
const servicePath = this.servicePath;
const promises = s3Sync.map((s) => {
let bucketPrefix = '';
if (s.hasOwnProperty('bucketPrefix')) {
bucketPrefix = s.bucketPrefix;
}
let acl = 'private';
if (s.hasOwnProperty('acl')) {
acl = s.acl;
}
let followSymlinks = false;
if (s.hasOwnProperty('followSymlinks')) {
followSymlinks = s.followSymlinks;
}
let defaultContentType = undefined
if (s.hasOwnProperty('defaultContentType')) {
defaultContentType = s.defaultContentType;
}
if (!s.bucketName || !s.localDir) {
throw 'Invalid custom.s3Sync';
}
let deleteRemoved = true;
if (s.hasOwnProperty('deleteRemoved')) {
deleteRemoved = s.deleteRemoved;
}
return new Promise((resolve) => {
const localDir = [servicePath, s.localDir].join('/');
const params = {
maxAsyncS3: 5,
localDir,
deleteRemoved,
followSymlinks: followSymlinks,
getS3Params: (localFile, stat, cb) => {
const s3Params = {};
if(Array.isArray(s.params)) {
s.params.forEach((param) => {
const glob = Object.keys(param)[0];
if(minimatch(localFile, `${path.resolve(localDir)}/${glob}`)) {
Object.assign(s3Params, param[glob] || {});
}
});
}
cb(null, s3Params);
},
s3Params: {
Bucket: s.bucketName,
Prefix: bucketPrefix,
ACL: acl
}
};
if (typeof(defaultContentType) != 'undefined') {
Object.assign(params, {defaultContentType: defaultContentType})
}
const uploader = this.client().uploadDir(params);
uploader.on('error', (err) => {
throw err;
});
let percent = 0;
uploader.on('progress', () => {
if (uploader.progressTotal === 0) {
return;
}
const current = Math.round((uploader.progressAmount / uploader.progressTotal) * 10) * 10;
if (current > percent) {
percent = current;
cli.printDot();
}
});
uploader.on('end', () => {
resolve('done');
});
});
});
return Promise.all(promises)
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Synced.')}`);
});
}
clear() {
const s3Sync = this.serverless.service.custom.s3Sync;
if (!Array.isArray(s3Sync)) {
return Promise.resolve();
}
const cli = this.serverless.cli;
cli.consoleLog(`${messagePrefix}${chalk.yellow('Removing S3 objects...')}`);
const promises = s3Sync.map((s) => {
let bucketPrefix = '';
if (s.hasOwnProperty('bucketPrefix')) {
bucketPrefix = s.bucketPrefix;
}
return new Promise((resolve) => {
const params = {
Bucket: s.bucketName,
Prefix: bucketPrefix
};
const uploader = this.client().deleteDir(params);
uploader.on('error', (err) => {
throw err;
});
let percent = 0;
uploader.on('progress', () => {
if (uploader.progressTotal === 0) {
return;
}
const current = Math.round((uploader.progressAmount / uploader.progressTotal) * 10) * 10;
if (current > percent) {
percent = current;
cli.printDot();
}
});
uploader.on('end', () => {
resolve('done');
});
});
});
return Promise.all(promises)
.then(() => {
cli.printDot();
cli.consoleLog('');
cli.consoleLog(`${messagePrefix}${chalk.yellow('Removed.')}`);
});
}
}
module.exports = ServerlessS3Sync;