Skip to content

Commit f353c86

Browse files
Ensure bucket exists before upload
1 parent 9bee1b1 commit f353c86

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

tools/deploy.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ const chalk = require('chalk');
88
const spinner = require('ora')();
99
const metadata = require('../package.json');
1010

11-
aws.config.update({
11+
const s3 = new aws.S3({
1212
s3ForcePathStyle: true,
1313
endpoint: 'https://s3.csh.rit.edu'
1414
});
15-
const s3 = new aws.S3();
1615

1716
const config = {
1817
root: path.resolve(__dirname, '..'),
@@ -22,10 +21,32 @@ const config = {
2221
'src/*'
2322
],
2423
bucket: metadata.name,
24+
region: '',
2525
dest: metadata.version,
2626
acl: 'public-read'
2727
};
2828

29+
async function ensureBucket() {
30+
const params = {
31+
Bucket: config.bucket
32+
};
33+
34+
return s3.headBucket(params).promise()
35+
.catch(err => {
36+
if (err.code == 'NoSuchBucket' || err.code == 'NotFound') {
37+
// Bucket does not exist, create it
38+
params.CreateBucketConfiguration = {
39+
LocationConstraint: config.region
40+
};
41+
42+
return s3.createBucket(params).promise();
43+
}
44+
45+
// Unhandled error
46+
throw err;
47+
});
48+
}
49+
2950
function upload(srcPath, destPath) {
3051
return fs.readFile(srcPath)
3152
.then(data => {
@@ -48,6 +69,10 @@ function upload(srcPath, destPath) {
4869
async function deploy() {
4970
const uploaders = [];
5071

72+
// Ensure the upload bucket exists
73+
await ensureBucket();
74+
75+
// Upload each artifact to the bucket
5176
for (let artifact of config.artifacts) {
5277
const filenames = await globby(path.resolve(config.root, artifact));
5378

@@ -71,7 +96,7 @@ function fail(error, message) {
7196
if (typeof error === 'string') {
7297
spinner.fail(chalk.bold.red(error));
7398
} else {
74-
spinner.fail(chalk.bold.red(`${message}\n${error}`))
99+
spinner.fail(chalk.bold.red(`${message}\n${error}${error.code ? ": " + error.code : ""}`))
75100
}
76101

77102
shell.exit(1);

0 commit comments

Comments
 (0)