This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
s3.js
290 lines (255 loc) · 8.59 KB
/
s3.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
|--------------------------------------------------------------------------
| S3 Bucket Deploy
|--------------------------------------------------------------------------
|
| npm run upload:s3 will build your plugin and upload it to your bucket.
| Read the plugin readme for full details.
|
*/
const AWS = require("aws-sdk");
const fs = require("fs");
const colors = require("colors/safe");
const zlib = require("zlib");
let manifest = require("./manifest.json");
var args = {};
process.argv.forEach(arg => {
const parts = arg.split("=");
if (parts[0] && parts[1] && parts.indexOf(parts[0]) > -1) {
args[parts[0]] = parts[1];
}
});
console.log("\n");
console.log(
`\t ${colors.bgWhite(colors.black(" --------------------------- "))}`
);
console.log(
`\t ${colors.bgWhite(colors.black(" Plugin S3 plugin uploader "))}`
);
console.log(
`\t ${colors.bgWhite(colors.black(" --------------------------- "))}`
);
console.log("\n");
if (!args.accessKeyId) {
console.log("💥 Failed to upload plugin, missing arg accessKeyId");
return false;
} else if (!args.secretAccessKey) {
console.log("💥 Failed to upload plugin, missing arg secretAccessKey");
return false;
} else if (!args.bucket) {
console.log("💥 Failed to upload plugin, missing arg bucket");
return false;
}
const s3Bucket = new AWS.S3({
accessKeyId: args.accessKeyId,
secretAccessKey: args.secretAccessKey
});
const bundlePath = manifest.bundle.replace(/\./g, "-").toLowerCase();
const versionPath = manifest.version.replace(/\./g, "-").toLowerCase();
const baseKey = `${bundlePath}/${versionPath}`;
fs.readFile("./icon.png", (err, pluginIconData) => {
const shouldUploadPluginIcon = !err && pluginIconData;
uploadIndexJS()
.then(location => {
console.log("\n> uploaded index > " + location + "\n");
uploadStyleCSS()
.then(location => console.log("> uploaded style > " + location + "\n"))
.catch(err => console.log("💥 " + err));
const uploadManifest = () => {
uploadManifestJSON()
.then(location => {
console.log("> uploaded manifest > " + location + "\n");
console.log(
"🎉 Successfully uploaded " +
manifest.name +
" to " +
location.replace("/manifest.json", "")
);
})
.catch(err => console.log("💥 " + err));
};
/**
* Check if icon.png is exist.
* Upload icon.png
*/
uploadIcon(shouldUploadPluginIcon, pluginIconData).then(iconLocation => {
if (iconLocation) {
console.log("> uploaded plugin icon > " + iconLocation + "\n");
manifest.graphic_url = iconLocation;
}
/**
* Check if thumbnail.png is exist.
* Upload thumbnail.png
*/
uploadThumbnail().then(thumbnailLocation => {
if (thumbnailLocation) {
console.log(
"> uploaded plugin thumbnail > " + thumbnailLocation + "\n"
);
manifest.thumbnail_url = thumbnailLocation;
}
/**
* Check if markdown.md is exist.
* Upload markdown.md
*/
uploadMarkdown().then(markDownLocation => {
if (markDownLocation) {
console.log(
"> uploaded plugin markdown > " + markDownLocation + "\n"
);
manifest.markdown_url = markDownLocation;
}
/**
* Set timestamp as a build version to avoid browser cache.
* buildVersion won't be stored with plugin data.
*/
manifest.buildVersion = Date.now();
/**
* Update the manifest file with new urls
* Upload manifest.json
*/
updateManifestFile().then(() => uploadManifest());
});
});
});
})
.catch(err => console.log("💥 " + err));
});
function updateManifestFile() {
return new Promise(resolve => {
fs.writeFile(
"./manifest.json",
JSON.stringify(manifest, null, 4),
"utf8",
err => {
if (!err) {
resolve();
} else {
console.log(
"> failed to set manifest properties (will continue to upload manifest) \n"
);
resolve();
}
}
);
});
}
function uploadIcon(shouldUploadPluginIcon, pluginIconData) {
return new Promise(resolve => {
if (shouldUploadPluginIcon) {
uploadPluginIcon(pluginIconData)
.then(iconLocation => resolve(iconLocation))
.catch(err => {
console.log("💥 " + err);
resolve(null);
});
} else {
resolve(null);
}
});
}
function uploadThumbnail() {
return new Promise(resolve => {
fs.readFile("./thumbnail.png", (err, pluginThumbnail) => {
if (!err && pluginThumbnail) {
upload({
resolve: resolve,
reject: err => {
console.log("💥 " + err);
resolve(null);
},
key: "thumbnail.png",
data: pluginThumbnail,
contentType: "image/png"
});
} else {
resolve(null);
}
});
});
}
function uploadIndexJS() {
return new Promise((resolve, reject) => {
zlib.gzip(fs.readFileSync("dist/index.js"), (error, result) => {
if (error) throw error;
upload({
resolve: resolve,
reject: reject,
key: "index.js",
data: result,
contentType: "text/javascript",
contentEncoding: "gzip"
});
});
});
}
function uploadStyleCSS() {
return new Promise((resolve, reject) => {
zlib.gzip(fs.readFileSync("dist/style.css"), (error, result) => {
if (error) throw error;
upload({
resolve: resolve,
reject: reject,
key: "style.css",
data: result,
contentType: "text/css",
contentEncoding: "gzip"
});
});
});
}
function uploadPluginIcon(data) {
return new Promise((resolve, reject) => {
upload({
resolve: resolve,
reject: reject,
key: "icon.png",
data: data,
contentType: "image/png"
});
});
}
function uploadMarkdown() {
return new Promise(resolve => {
fs.readFile("./markdown.md", (err, pluginMarkDown) => {
if (!err && pluginMarkDown) {
upload({
resolve: resolve,
reject: err => {
console.log("💥 " + err);
resolve(null);
},
key: "markdown.md",
data: pluginMarkDown,
contentType: "text/markdown"
});
} else {
resolve(null);
}
});
});
}
function uploadManifestJSON() {
return new Promise((resolve, reject) => {
upload({
resolve: resolve,
reject: reject,
key: "manifest.json",
data: JSON.stringify(manifest, null, 4),
contentType: "application/json"
});
});
}
function upload(params) {
s3Bucket.upload(
{
Bucket: args.bucket,
Key: baseKey + "/" + params.key,
Body: params.data,
ContentType: params.contentType,
ContentEncoding: params.contentEncoding || "UTF-8",
ACL: "public-read"
},
(err, data) => (err ? params.reject(err) : params.resolve(data.Location))
);
}