-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
140 lines (126 loc) · 5.63 KB
/
index.js
File metadata and controls
140 lines (126 loc) · 5.63 KB
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
'use strict';
const RSVP = require('rsvp');
const minimatch = require('minimatch');
const crypto = require('crypto');
const DeployPluginBase = require('ember-cli-deploy-plugin');
const Cloudinary = require('./lib/cloudinary');
function md5Hash(buf) {
let md5 = crypto.createHash('md5');
md5.update(buf);
return md5.digest('hex');
}
module.exports = {
name: require('./package').name,
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
filePattern: '**/*.{js,css,png,gif,ico,jpg,map,xml,txt,svg,swf,eot,ttf,woff,woff2,otf,wasm}',
fileIgnorePattern: null,
folder: '',
timestampSubfolder: {
enabled: true,
type: "timestamp" // default, otherwise ("hash") md5 hash from tymestamp
},
secure: true,
invalidate: true,
dotFolders: false,
uploadLarge: false, // files that are larger than 100 MB
useFilename: true,
uniqueFilename: true,
overwrite: true,
resourceType: 'raw',
type: 'upload',
accessControl: {access_type: "anonymous"},
accessMode: 'public',
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
}
},
requiredConfig: ['cloudName', 'apiKey', 'apiSecret'],
willBuild: function(context) {
if (!context.config.build.fingerprint) {
context.config.build.fingerprint = {}
}
if (context.config.cloudinary.timestampSubfolder.enabled && context.config.build.fingerprint.prepend) {
if(context.config.cloudinary.timestampSubfolder.type === "md5") {
var timestampSubfolder = context.config.timestampSubfolder = md5Hash(new Date().toISOString());
} else {
var timestampSubfolder = context.config.timestampSubfolder = new Date().toISOString();
}
context.cdnFingerprintPrepend = process.env.CDN_FINGERPRINT_PREPEND = context.config.build.fingerprint.prepend + timestampSubfolder + "/";
context.config.cloudinary.folder = context.config.cloudinary.folder === "" ? timestampSubfolder : ("/" + timestampSubfolder);
} else if (context.config.build.fingerprint.prepend) {
context.cdnFingerprintPrepend = process.env.CDN_FINGERPRINT_PREPEND = context.config.build.fingerprint.prepend
} else {
context.cdnFingerprintPrepend = process.env.CDN_FINGERPRINT_PREPEND = ""
}
} ,
upload: function(context) {
let filePattern = this.readConfig('filePattern');
let fileIgnorePattern = this.readConfig('fileIgnorePattern');
let distFiles = this.readConfig('distFiles');
let cloudName = this.readConfig('cloudName');
let dotFolders = this.readConfig('dotFolders');
let 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 });
// });
}
let uploadOptions = {
filePaths: filesToUpload,
// gzippedFilePaths: gzippedFiles,
// brotliCompressedFilePaths: brotliCompressedFiles,
distDir: this.readConfig('distDir'),
invalidate: this.readConfig('invalidate'),
uploadLarge: this.readConfig('uploadLarge'),
useFilename: this.readConfig('useFilename'),
uniqueFilename: this.readConfig('uniqueFilename'),
overwrite: this.readConfig('overwrite'),
resourceType: this.readConfig('resourceType'),
type: this.readConfig('type'),
accessControl: this.readConfig('accessControl'),
accessMode: this.readConfig('accessMode'),
manifestPath: this.readConfig('manifestPath'),
secure: this.readConfig('secure'),
folder: this.readConfig('folder')
};
this.log('Preparing to upload to Cloudinary `' + cloudName + '`', { verbose: true });
let cloudinary = new Cloudinary(this, uploadOptions)
return cloudinary.upload()
.then(filesUploaded => {
this.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();
}
};