-
Notifications
You must be signed in to change notification settings - Fork 12
/
upload.js
74 lines (67 loc) · 2.09 KB
/
upload.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
/**
* run nuxt generate + upload result to s3
* as of jan 2020, this is not used
*/
console.log(`AWS_PROFILE ${process.env.AWS_PROFILE} BUCKET_NAME ${process.env.BUCKET_NAME}`)
const BUCKET_NAME = process.env.BUCKET_NAME
const fs = require('fs')
const aws = require('aws-sdk')
const path = require('path')
var credentials = new aws.SharedIniFileCredentials({ profile: process.env.AWS_PROFILE })
aws.config.credentials = credentials
const s3 = new aws.S3({
// accessKeyId: process.env.AWS_ACCESS_KEY,
// secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
})
const getLangFromFileName = fileName => {
if (fileName.includes('/fr/')) {
return 'fr-CA'
} else {
return 'en-CA'
}
}
const getContentTypeFromFileName = fileName => {
if (fileName.endsWith('.js')) {
return 'text/javascript'
} else if (fileName.endsWith('.html')) {
return 'text/html'
} else if (fileName.endsWith('.css')) {
return 'text/css'
} else if (fileName.endsWith('.png')) {
return 'image/png'
} else if (fileName.endsWith('.ico')) {
return 'image/x-icon'
} else {
return 'text/plain'
}
}
const dist = 'dist/'
function uploadFolder(folder) {
const files = fs.readdirSync(folder).map(f => path.join(folder, f))
files
.filter(f => !fs.lstatSync(f).isDirectory())
.forEach(fileName => {
fs.readFile(fileName, (err, data) => {
if (err) throw err
// tell s3 to delete file after 30 days (should be regenerated well before)
const expiry = new Date()
expiry.setDate(expiry.getDate() + 30)
s3.upload(
{
Bucket: BUCKET_NAME,
Key: fileName.replace(dist, ''),
Body: data,
Expires: expiry,
ContentType: getContentTypeFromFileName(fileName),
ContentLanguage: getLangFromFileName(fileName),
},
function(s3Err, data) {
if (s3Err) throw s3Err
console.log(`File uploaded successfully at ${data.Location}`)
}
)
})
})
files.filter(f => fs.lstatSync(f).isDirectory()).forEach(f => uploadFolder(f))
}
uploadFolder(dist)