-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
184 lines (152 loc) · 6.31 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
184
/* jshint node: true */
'use strict';
var DeployPluginBase = require('ember-cli-deploy-plugin');
var azure = require('azure-storage');
var Promise = require('rsvp').Promise;
var walk = require('walk');
var fs = require('fs');
var path = require('path');
var mime = require('mime');
module.exports = {
name: 'ember-cli-deploy-azure-blob',
createDeployPlugin: function(options) {
var DeployPlugin = DeployPluginBase.extend({
name: options.name,
defaultConfig: {
containerName: 'emberdeploy',
folderPrefix: '',
cacheControl: {
extensions: [
{ extension: 'png', policy: 'max-age=604800' },
{ extension: 'jpg', policy: 'max-age=604800' },
{ extension: 'gif', policy: 'max-age=604800' },
{ extension: 'jpeg', policy: 'max-age=604800' },
{ extension: 'css', policy: 'max-age=86400' },
{ extension: 'js', policy: 'max-age=86400' }
]
}
},
_createClient: function() {
var connectionString = this.readConfig("connectionString");
var storageAccount = this.readConfig("storageAccount");
var storageAccessKey = this.readConfig("storageAccessKey");
if(connectionString) {
return azure.createBlobService(connectionString);
} else if(storageAccount && storageAccessKey) {
return azure.createBlobService(storageAccount, storageAccessKey);
} else {
throw new Error("Missing connection string or storage account / access key combination.");
}
},
configure: function(context) {
this._super.configure.apply(this, context);
if(!this.pluginConfig.connectionString) {
['storageAccount', 'storageAccessKey'].forEach(this.ensureConfigPropertySet.bind(this));
}
},
upload: function(context) {
var client = this._createClient();
var _this = this;
var containerName = this.readConfig("containerName");
var distDir = context.distDir;
this.log("uploading files from " + distDir + "...", { verbose: true });
var gzippedFiles = context.gzippedFiles || [];
var correctedGzippedFiles = gzippedFiles.map(function(gzippedFile) {
return path.normalize(gzippedFile);
});
return new Promise(function(resolve, reject) {
// create container
client.createContainerIfNotExists(containerName, {publicAccessLevel : 'blob'}, function(error, result, response){
if(!error){
// set CORS
var serviceProperties = {
Cors: {
CorsRule: [{
AllowedOrigins: ['*'],
AllowedMethods: ['GET'],
AllowedHeaders: [],
ExposedHeaders: [],
MaxAgeInSeconds: 60
}]
}
};
client.setServiceProperties(serviceProperties, function(error, result, response) {
if(!error) {
// walk the directory to be uploaded
var walker = walk.walk(distDir, { followLinks: false });
walker.on("file", function (root, fileStats, next) {
_this._uploadFile(root, fileStats, next, context.distDir, client, correctedGzippedFiles);
});
walker.on("errors", function(root, nodeStatsArray, next) {
nodeStatsArray.forEach(function (n) {
this.log("[ERROR] " + n.name, {color: 'red', verbose: true});
this.log(n.error.message || (n.error.code + ": " + n.error.path), {color: 'red'});
});
reject();
});
walker.on("end", function() {
_this.log("upload succeeded");
resolve();
});
} else {
reject(error);
}
});
} else {
reject(error);
}
});
});
},
_uploadFile: function(root, fileStat, next, distDir, client, gzippedFiles) {
var _this = this;
var containerName = this.readConfig("containerName");
var folderPrefix = this.readConfig("folderPrefix");
var resolvedFile = path.resolve(root, fileStat.name);
var normalizedRoot = path.normalize(root);
var targetDirectory = normalizedRoot === distDir ? undefined : normalizedRoot.replace(distDir + path.sep, "");
var targetFile = targetDirectory ? targetDirectory + path.sep + fileStat.name : fileStat.name;
if(folderPrefix) {
targetFile = folderPrefix.replace(/\/$/, "") + path.sep + targetFile;
}
var options = {}
if (gzippedFiles.indexOf(targetFile) != -1) {
options["contentEncoding"] = "gzip";
}
// Set the cache control policy.
options['cacheControl'] = this._cacheControlPolicy(fileStat);
client.doesBlobExist(containerName, targetFile, function(error, blobExists, response) {
if(blobExists === true) {
next();
} else {
client.createBlockBlobFromLocalFile(containerName, targetFile, resolvedFile, options, function(error, result, response){
if(!error){
// file uploaded
} else {
_this.log("Error uploading " + targetFile, { color: 'red'});
_this.log(error, { color: 'red', verbose: true});
}
next();
});
}
});
},
_cacheControlPolicy: function(fileStat) {
var cacheControl = this.readConfig('cacheControl');
// Default cache policy.
var policy = 'no-cache, must-revalidate';
// Check for cache control extensions matches.
if (typeof cacheControl.extensions !== 'undefined') {
var validExtension = cacheControl.extensions.find(function(option) {
return option.extension === fileStat.name.split('.').pop(); // Get only the extension of the file.
});
if (typeof validExtension !== 'undefined') {
policy = validExtension.policy;
}
}
return policy;
}
});
return new DeployPlugin();
}
};