From 63b93d82aa21a6bea6bc247dc83729f0ca8d7118 Mon Sep 17 00:00:00 2001 From: Mient-jan Stelling Date: Mon, 3 Dec 2018 18:05:34 +0100 Subject: [PATCH 1/2] added destructuring of options field in some methods. applied prettier to code. @todo find out why prettier is not firing --- src/lib/Uploader.ts | 54 +++++++++++++++++++-------------------------- src/lib/batch.ts | 4 ++-- src/lib/cli.ts | 32 ++++++++++++++++++--------- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/lib/Uploader.ts b/src/lib/Uploader.ts index 07fc445..a73c5fd 100644 --- a/src/lib/Uploader.ts +++ b/src/lib/Uploader.ts @@ -10,7 +10,6 @@ const chalk = require('chalk'); const fs = require('fs'); const mime = require('mime'); - export type Options = { bucket: string; localPath: string; @@ -32,21 +31,18 @@ export default class Uploader { private options: Options; private bar: any; - constructor(options: Options) - { + constructor(options: Options) { this.options = { ...defaultOptions, ...options, }; - if (this.options.config) - { + if (this.options.config) { AWS.config.loadFromPath(this.options.config); } // TODO: more checks on other options? - if (!this.options.bucket) - { + if (!this.options.bucket) { throw new Error('No bucket defined!'); } @@ -57,8 +53,9 @@ export default class Uploader { return this.run(); } - private async run():Promise { + private async run(): Promise { const files = await this.getFiles(); + const { concurrency, localPath, remotePath } = this.options; // a nice progress bar to show during upload this.bar = new ProgressBar('[:bar] :percent | :etas | :current / :total | :rate/fps ', { @@ -71,13 +68,10 @@ export default class Uploader { // do the work! await streamBatch({ files, - concurrency: this.options.concurrency, - processItem: (file:string):Promise => { - const key = path.join(this.options.remotePath, file); - return this.uploadFile( - path.resolve(this.options.localPath, file), - key, - ); + concurrency, + processItem: (file: string): Promise => { + const key = path.join(remotePath, file); + return this.uploadFile(path.resolve(localPath, file), key); }, onProgress: () => this.bar.tick(), }); @@ -87,22 +81,20 @@ export default class Uploader { } private getFiles(): Promise> { - const gatheringSpinner = ora( - `Gathering files from ${chalk.blue(this.options.localPath)} (please wait) ...`, - ); + const { localPath, glob: globPath } = this.options; + const gatheringSpinner = ora(`Gathering files from ${chalk.blue(localPath)} (please wait) ...`); + gatheringSpinner.start(); return new Promise((resolve, reject) => { - glob(`**/${this.options.glob}`, { cwd: path.resolve(this.options.localPath) }, (err, files) => { + glob(`**/${globPath}`, { cwd: path.resolve(localPath) }, (err, files) => { if (err) { gatheringSpinner.fail(err); reject(err); } gatheringSpinner.succeed( - `Found ${chalk.green(files.length)} files at ${chalk.blue( - this.options.localPath, - )}, starting upload:`, + `Found ${chalk.green(files.length)} files at ${chalk.blue(localPath)}, starting upload:`, ); resolve(files); @@ -110,17 +102,20 @@ export default class Uploader { }); } - private uploadFile(localFilePath:string, remotePath:string): Promise { - return new Promise((resolve) => { + private uploadFile(localFilePath: string, remotePath: string): Promise { + const { bucket, dryRun } = this.options; + const s3 = this.s3; + + return new Promise(resolve => { const body = fs.createReadStream(localFilePath); const params = { - Bucket: this.options.bucket, + Bucket: bucket, Key: remotePath, Body: body, ContentType: mime.getType(localFilePath), }; - if (!this.options.dryRun) { - this.s3.upload(params, err => { + if (!dryRun) { + s3.upload(params, err => { // tslint:disable-next-line no-console if (err) console.error('err:', err); resolve(); @@ -128,9 +123,6 @@ export default class Uploader { } else { resolve(); } - }) + }); } } - - - diff --git a/src/lib/batch.ts b/src/lib/batch.ts index 2c9e81e..37d58e1 100644 --- a/src/lib/batch.ts +++ b/src/lib/batch.ts @@ -10,7 +10,7 @@ export default function streamBatch({ files, processItem, onProgress, -}: Options):Promise { +}: Options): Promise { return new Promise(resolve => { let count = 0; const total = files.length; @@ -25,7 +25,7 @@ export default function streamBatch({ setTimeout(() => { onProgress(); resolve(); - }, 50) + }, 50); } else { onProgress(); diff --git a/src/lib/cli.ts b/src/lib/cli.ts index 57a5b13..8331447 100644 --- a/src/lib/cli.ts +++ b/src/lib/cli.ts @@ -5,16 +5,24 @@ import Uploader from './Uploader'; yargs .usage('Usage: $0 [options]') - .command(['$0'], 'Upload files to s3', () => {}, (argv) => { - new Uploader(argv).upload(); - }) - .example('$0 -b bucket-name -p ./files -r /data', 'Upload files from a local folder to a s3 bucket path') + .command( + ['$0'], + 'Upload files to s3', + () => {}, + argv => { + new Uploader(argv).upload(); + }, + ) + .example( + '$0 -b bucket-name -p ./files -r /data', + 'Upload files from a local folder to a s3 bucket path', + ) .example('$0 -d ...', 'Dry run upload') .option('d', { alias: 'dry-run', default: false, - describe: 'Do a dry run, don\'t do any upload.', - type: 'boolean' + describe: "Do a dry run, don't do any upload.", + type: 'boolean', }) .option('b', { alias: 'bucket', @@ -59,11 +67,15 @@ yargs type: 'string', nargs: 1, }) - .demandOption(['bucket', 'local-path', 'remote-path'], 'Please provide at least the required arguments to upload.') + .demandOption( + ['bucket', 'local-path', 'remote-path'], + 'Please provide at least the required arguments to upload.', + ) .group(['bucket', 'local-path', 'remote-path'], 'Required:') .help('h') .alias('h', 'help') - .epilogue('for more information about AWS authentication, please visit https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html') + .epilogue( + 'for more information about AWS authentication, please visit https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html', + ) .version(false) - .wrap(Math.min(120, yargs.terminalWidth())) - .argv; + .wrap(Math.min(120, yargs.terminalWidth())).argv; From 8c1cae3e46da934e46dae4be35f5985565f5263d Mon Sep 17 00:00:00 2001 From: Mient-jan Stelling Date: Tue, 4 Dec 2018 10:52:19 +0100 Subject: [PATCH 2/2] updating from master, doing some clean-up --- src/lib/Uploader.ts | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/lib/Uploader.ts b/src/lib/Uploader.ts index e28a386..fa69375 100644 --- a/src/lib/Uploader.ts +++ b/src/lib/Uploader.ts @@ -105,11 +105,12 @@ export default class Uploader { }); } - uploadFile(localFilePath: string, remotePath: string): Promise { + public uploadFile(localFilePath: string, remotePath: string): Promise { const body = fs.createReadStream(localFilePath); + const { dryRun, bucket: Bucket } = this.options; const params = { - Bucket: this.options.bucket, + Bucket, Key: remotePath.replace(/\\/, '/'), Body: body, ContentType: mime.getType(localFilePath), @@ -117,7 +118,7 @@ export default class Uploader { }; return new Promise(resolve => { - if (!this.options.dryRun) { + if (!dryRun) { this.s3.upload(params, err => { // tslint:disable-next-line no-console if (err) console.error('err:', err); @@ -129,16 +130,17 @@ export default class Uploader { }); } - getCacheControlValue(file: string): string { - if (this.options.cacheControl) { + public getCacheControlValue(file: string): string { + const { cacheControl } = this.options; + if (cacheControl) { // return single option for all files - if (typeof this.options.cacheControl === 'string') { - return this.options.cacheControl; + if (typeof cacheControl === 'string') { + return cacheControl; } // find match in glob patterns - const match = Object.keys(this.options.cacheControl).find(key => minimatch(file, key)); - return (match && this.options.cacheControl[match]) || ''; + const match = Object.keys(cacheControl).find(key => minimatch(file, key)); + return (match && cacheControl[match]) || ''; } // return default value