diff --git a/README.md b/README.md index 3c3299f..7478ddf 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,7 @@ beam-me-up {options} - _--source_ or _-s_ - Source of the folder that will be uploaded (default: current folder) - _--bucket_ or _-b_ - Name of the S3 bucket (default: name of the current folder) - _--region_ or _-r_ - AWS region where the files will be uploaded, default: saved region if exists or a list to choose one if it is not saved yet -- _--force_ or _-f_ - Update the bucket and pick "us-east-1" region without asking (default: false) +- _--force_ or _-f_ - Update the bucket without asking (default: false, forced region can be overridden with _-r_) - _--update_ or _-u_ - Update existing bucket (default: false) ### Examples diff --git a/bin/scotty.js b/bin/scotty.js index 46ac219..2242d02 100755 --- a/bin/scotty.js +++ b/bin/scotty.js @@ -7,6 +7,7 @@ const AWS = require('aws-sdk') const scotty = require('../index') const inquirer = require('inquirer') const colors = require('colors') +const clipboardy = require('clipboardy') // Supported regions from http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region const AWS_REGIONS = [ @@ -48,7 +49,7 @@ function showHelp() { ${colors.magenta('--source')} ${colors.cyan('or')} ${colors.magenta('-s')} Source of the folder that will be uploaded ${colors.cyan('| default: current folder')} ${colors.magenta('--bucket')} ${colors.cyan('or')} ${colors.magenta('-b')} Name of the S3 bucket ${colors.cyan('| default: name of the current folder')} ${colors.magenta('--region')} ${colors.cyan('or')} ${colors.magenta('-r')} AWS region where the files will be uploaded ${colors.cyan('| default: saved region if exists or a list to choose one if it is not saved yet')} - ${colors.magenta('--force')} ${colors.cyan('or')} ${colors.magenta('-f')} Update the bucket and pick "us-east-1" region without asking ${colors.cyan('| default: false')} + ${colors.magenta('--force')} ${colors.cyan('or')} ${colors.magenta('-f')} Update the bucket without asking, region can be overridden with ${colors.magenta('-r')} ${colors.cyan('| default: false')} ${colors.magenta('--update')} ${colors.cyan('or')} ${colors.magenta('-u')} Update existing bucket ${colors.cyan('| default: false')} ✤ ✤ ✤ @@ -134,11 +135,11 @@ function cmd(console) { if (!AWS.config.credentials) return console.log(`Set AWS credentials first. Guide is available here: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html`) - if (!AWS.config.region) + if (!args.region) { return getDefaultRegion() .catch(() => { if (args.force) - return saveDefaultRegion('us-east-1') + return 'us-east-1' return inquirer.prompt([{ type: 'list', @@ -150,11 +151,16 @@ function cmd(console) { .then(result => result.region) .then(saveDefaultRegion) }) - .then(region => scotty(args.source, args.bucket, region, args.website, args.spa, args.update, args.force, args.quiet, console)) - .then(() => process.exit(1)) - .catch(() => process.exit(1)) + .then(region => beamUp(args, region, console)) + } - return scotty(args.source, args.bucket, AWS.config.region, args.website, args.spa, args.update, args.force, args.quiet, console) + return saveDefaultRegion(args.region) + .then(() => beamUp(args, args.region, console)) +} + +function beamUp (args, region, console) { + return scotty(args.source, args.bucket, region, args.website, args.spa, args.update, args.force, args.quiet, console) + .then(endpoint => clipboardy.write(endpoint)) .then(() => process.exit(1)) .catch(() => process.exit(1)) } diff --git a/lib/scotty.js b/lib/scotty.js index 53bd05f..06b9673 100644 --- a/lib/scotty.js +++ b/lib/scotty.js @@ -2,7 +2,6 @@ const colors = require('colors') const getFolderSize = require('get-folder-size') -const clipboardy = require('clipboardy') const createBucket = require('./create-bucket') const reuseBucket = require('./reuse-bucket') @@ -35,6 +34,8 @@ function createOrUpdateBucket(bucket, region, update, force, quiet, logger) { if (err.code === 'BucketAlreadyExists') err.message += '\n\nTry running the command with a new bucket name: \n scotty --bucket some-new-unique-name\n\nor change the name of your folder and re-run the same command.' + if (err.code === 'InvalidLocationConstraint') + err.message += '\n\nBucket already exists in a different region. Please change to: \n a) the correct region (using `-r region-name`) \n OR \n b) a different bucket name (using `-b bucket-name`)' throw err }) } @@ -91,14 +92,13 @@ function scotty(source, bucket, region, website, spa, update, force, quiet, logg .then(response => { const cdnUrl = response && response.cdn ? response.url : null const endpoint = website || spa ? `http://${bucket}.s3-website.${region}.amazonaws.com/` : `http://${bucket}.s3.amazonaws.com/` - clipboardy.writeSync(endpoint) if (!quiet) { logger.log('\nSuccessfully beamed up!'.magenta, colors.cyan(endpoint), '\nThis link should be copied to your clipboard now.'.magenta) logger.log('\nCDN URL:'.magenta, colors.cyan(cdnUrl), '\nCloudFront is super slow, this link should be valid in next 10 minutes or so.'.magenta) } - return true + return endpoint }) .catch(err => { if (!quiet)