Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

Commit

Permalink
Add --empty parameter to clear bucket before upload files (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
asponda authored and stojanovic committed May 12, 2018
1 parent a40ebf5 commit 1d23fd4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -58,6 +58,7 @@ beam-me-up {options}
- _--urlonly_ or _-o_ - Only output the resulting URL, CDN or S3 according to options (default: false)
- _--expire_ or _-e_ - delete objects on bucket older than n days (default: no expiration)
- _--profile or _-a_ - AWS profile to be used (default: 'default')
- _--empty or _-y_ - Empty the bucket (Delete all objects before upload files) (default: false)

### Examples

Expand Down
8 changes: 5 additions & 3 deletions bin/scotty.js
Expand Up @@ -60,6 +60,7 @@ function showHelp() {
${colors.magenta('--urlonly')} ${colors.cyan('or')} ${colors.magenta('-o')} Only output the resulting URL, CDN or S3 according to options ${colors.cyan('| default: false')}
${colors.magenta('--expire')} ${colors.cyan('or')} ${colors.magenta('-e')} Delete objects on bucket older than n days ${colors.cyan('| default: no expiration')}
${colors.magenta('--profile')} ${colors.cyan('or')} ${colors.magenta('-a')} AWS profile to be used ${colors.cyan('| default: default')}
${colors.magenta('--empty')} ${colors.cyan('or')} ${colors.magenta('-y')} Empty the bucket (Delete all objects before upload files) ${colors.cyan('| default: false')}
✤ ✤ ✤
Expand Down Expand Up @@ -88,10 +89,11 @@ function readArgs() {
c: 'nocdn',
o: 'urlonly',
e: 'expire',
a: 'profile'
a: 'profile',
y: 'empty'
},
string: ['source', 'bucket', 'prefix', 'region', 'profile'],
boolean: ['quiet', 'website', 'spa', 'force', 'update', 'delete'],
boolean: ['quiet', 'website', 'spa', 'force', 'update', 'delete', 'empty'],
default: {
source: process.cwd(),
bucket: path.parse(process.cwd()).name,
Expand Down Expand Up @@ -202,7 +204,7 @@ function beamUp (args, region, console) {
const s3 = new AWS.S3({
region: region
})
const promise = scotty(args.source, args.bucket, args.prefix, region, args.website, args.spa, args.update, args.delete, args.nocdn, args.urlonly, args.expire, args.force, args.quiet, !args.noclipboard, console, s3)
const promise = scotty(args.source, args.bucket, args.prefix, region, args.website, args.spa, args.update, args.delete, args.nocdn, args.urlonly, args.expire, args.force, args.empty, args.quiet, !args.noclipboard, console, s3)

if (!args.noclipboard) {
promise.then(endpoint => clipboardy.write(endpoint))
Expand Down
13 changes: 12 additions & 1 deletion lib/scotty.js
Expand Up @@ -5,6 +5,7 @@ const getFolderSize = require('get-folder-size')

const createBucket = require('./create-bucket')
const deleteBucket = require('./delete-bucket')
const emptyBucket = require('./empty-bucket')
const reuseBucket = require('./reuse-bucket')
const upload = require('./upload')
const getFormattedSize = require('./get-formatted-size')
Expand Down Expand Up @@ -68,7 +69,7 @@ function destroyBucket(bucket, quiet, logger, s3lib) {
})
}

function scotty(source, bucket, prefix, region, website, spa, update, destroy, nocdn, urlonly, expire, force, quiet, clipboard, logger, s3client) {
function scotty(source, bucket, prefix, region, website, spa, update, destroy, nocdn, urlonly, expire, force, empty, quiet, clipboard, logger, s3client) {

if (destroy)
return destroyBucket(bucket, quiet, logger, s3client)
Expand All @@ -87,6 +88,16 @@ function scotty(source, bucket, prefix, region, website, spa, update, destroy, n
logger.log(' bucket'.magenta, '✤', colors.cyan(result))

time = new Date().getTime()

if (!destroy && empty) {
if (!quiet)
logger.log(' empty'.magenta, '✤', colors.cyan(`${bucket} bucket`))

return emptyBucket(bucket).then(() => {
return upload(source, bucket, prefix, s3client)
})
}

return upload(source, bucket, prefix, s3client)
})
.then(() => {
Expand Down

0 comments on commit 1d23fd4

Please sign in to comment.