forked from brave/https-everywhere-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadDataFiles.js
More file actions
59 lines (53 loc) · 1.92 KB
/
uploadDataFiles.js
File metadata and controls
59 lines (53 loc) · 1.92 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
const fs = require('fs')
const s3 = require('s3')
const commander = require('commander')
const splitVersion = process.env.npm_package_version.split('.')
splitVersion.splice(2)
const dataFileVersion = splitVersion.join('.')
const client = s3.createClient({
maxAsyncS3: 20,
s3RetryCount: 3,
s3RetryDelay: 1000,
multipartUploadThreshold: 20971520,
multipartUploadSize: 15728640,
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html#constructor-property
s3Options: {}
})
const uploadFile = (key, filePath, filename) => {
return new Promise((resolve, reject) => {
var params = {
localFile: filePath,
// See: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property
s3Params: {
Bucket: 'https-everywhere-data',
Key: `${key}/${filename}`,
ACL: 'public-read'
}
}
var uploader = client.uploadFile(params)
process.stdout.write(`Started uploading to: ${params.s3Params.Key}... `)
uploader.on('error', function (err) {
console.error('Unable to upload:', err.stack, 'Do you have ~/.aws/credentials filled out?')
reject(new Error('Unable to upload'))
})
uploader.on('end', function (params) {
console.log('completed')
resolve()
})
})
}
commander
.option('-p, --prod', 'whether the upload is for prod, if not specified uploads to the test location')
.parse(process.argv)
// Queue up all the uploads one at a time to easily spot errors
let p = Promise.resolve()
const date = new Date().toISOString().split('.')[0]
const dataFilenames = fs.readdirSync('out')
dataFilenames.forEach((filename) => {
if (commander.prod) {
p = p.then(uploadFile.bind(null, dataFileVersion, `out/${filename}`, filename))
p = p.then(uploadFile.bind(null, `backups/${date}`, `out/${filename}`, filename))
} else {
p = p.then(uploadFile.bind(null, `test/${dataFileVersion}`, `out/${filename}`, filename))
}
})