-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (66 loc) · 2.33 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
/*eslint-env node*/
'use strict';
var BasePlugin = require('ember-cli-deploy-plugin');
var Minio = require('minio');
var RSVP = require('rsvp');
var mime = require('mime-types');
var path = require('path');
var fs = require('fs');
module.exports = {
name: 'ember-cli-deploy-minio',
createDeployPlugin: function(options) {
var DeployPlugin = BasePlugin.extend({
name: options.name,
requiredConfig: ['accessKey', 'secretKey', 'bucket', 'endpoint'],
runAfter: 'gzip',
defaultConfig: {
distDir: function(context) {
return context.distDir;
},
secure: true
},
upload: function(/*context*/) {
var plugin = this,
source = this.readConfig('distDir');
var promises = this.walk(path.join(source)).map(file => {
var relative = path.relative(source, file),
bucket = plugin.readConfig('bucket'),
meta = { 'Content-Type': mime.lookup(file) };
plugin.log(`preparing to upload to: "${bucket}"`, { verbose: true });
return new RSVP.Promise(function(resolve, reject) {
plugin.uploader().fPutObject(bucket, relative, file, meta, err => {
if (err) {
plugin.log('✘ ' + relative, { color: 'black' });
plugin.log(err, { color: 'black' });
reject(err);
} else {
plugin.log('✔ ' + relative, { verbose: true });
resolve(relative);
}
})
});
});
plugin.log(`uploaded ${promises.length} files ok`, { verbose: true });
return RSVP.all(promises);
},
walk: function(dir) {
var plugin = this;
return fs.readdirSync(dir).reduce((list, file) => {
var name = path.resolve(dir, file);
var isDir = fs.statSync(name).isDirectory();
return list.concat(isDir ? plugin.walk(name) : [name]);
}, []);
},
uploader: function() {
return new Minio.Client({
accessKey: this.readConfig('accessKey'),
secretKey: this.readConfig('secretKey'),
endPoint: this.readConfig('endpoint'),
useSSL: this.readConfig('secure'),
port: this.readConfig('port')
})
}
});
return new DeployPlugin();
}
};