-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush.js
More file actions
67 lines (57 loc) · 1.78 KB
/
push.js
File metadata and controls
67 lines (57 loc) · 1.78 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
const fs = require('fs');
const AdmZip = require('adm-zip');
const AWS = require('aws-sdk');
const docFileName = process.env.DOC_NAME || "SimonStaszkiewiczResume.docx";
const htmlFileName = process.env.HTML_NAME || "SimonStaszkiewiczResume.html";
const zipName = process.env.ZIP_NAME || "SimonStaszkiewiczResume.zip";
const region = process.env.BUCKET_REGION || "us-west-2";
const bucket = process.env.BUCKET_NAME || "simon-staszkiewicz-public";
const gitCommit = process.env.GIT_COMMIT;
const s3 = new AWS.S3({region});
async function push() {
const zip = new AdmZip();
zip.addLocalFile(docFileName);
zip.addLocalFile(htmlFileName);
zip.toBuffer(async zipBuffer => {
try {
const pushAll = (zipLocation, docLocation, htmlLocation) => {
return [
pushToS3(zipLocation, zipBuffer),
pushToS3(docLocation, fs.readFileSync(docFileName)),
pushToS3(htmlLocation, fs.readFileSync(htmlFileName)),
]
};
const promises = [
...pushAll(zipName, docFileName, htmlFileName),
...pushAll(
`resume/latest/${zipName}`,
`resume/latest/${docFileName}`,
`resume/latest/${htmlFileName}`
),
];
if (gitCommit) {
promises.push(
...pushAll(
`resume/${gitCommit}/${zipName}`,
`resume/${gitCommit}/${docFileName}`,
`resume/${gitCommit}/${htmlFileName}`
),
)
}
await Promise.all(promises);
console.log('Successfully pushed all documents to S3.');
} catch(e) {
console.error(e);
process.exit(1);
}
});
}
function pushToS3(key, body) {
return s3.putObject({
Bucket: bucket,
ACL: 'public-read',
Key: key,
Body: body,
}).promise();
}
push();