From c245a87e7eeede97c6b8428cae554df574ed178e Mon Sep 17 00:00:00 2001 From: tushar mathur Date: Wed, 29 Jun 2016 13:39:15 +0530 Subject: [PATCH] refactor(cli): remove lodash dependency --- package.json | 1 - src/Main.js | 1 - src/bin/NewDownload.js | 13 ---------- src/bin/ResumeDownload.js | 15 ------------ src/bin/mtd.js | 50 +++++++-------------------------------- 5 files changed, 8 insertions(+), 72 deletions(-) delete mode 100644 src/bin/NewDownload.js delete mode 100644 src/bin/ResumeDownload.js diff --git a/package.json b/package.json index e929b55..ea68ae5 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,6 @@ "dependencies": { "graceful-fs": "^4.1.3", "humanize-plus": "^1.8.1", - "lodash": "^4.0.0", "meow": "^3.7.0", "muxer": "^1.0.1", "progress": "^1.1.8", diff --git a/src/Main.js b/src/Main.js index a439691..77703ce 100644 --- a/src/Main.js +++ b/src/Main.js @@ -12,7 +12,6 @@ import {mux, demux} from 'muxer' export const UTILS = U export const createDownload = (_options) => { - const HTTP = T.HTTP(request) const FILE = T.FILE(fs) const options = U.MergeDefaultOptions(_options) diff --git a/src/bin/NewDownload.js b/src/bin/NewDownload.js deleted file mode 100644 index dd7c633..0000000 --- a/src/bin/NewDownload.js +++ /dev/null @@ -1,13 +0,0 @@ -import Rx from 'rx' -import VALID_URL from 'valid-url' -import _ from 'lodash' -import {ResolvePath} from '../Utils' - -export default (createDownload, pFlags) => pFlags - .filter((x) => VALID_URL.isUri(x.url)) - .flatMap((x) => { - const defaultParams = {path: ResolvePath(x.url)} - _.defaults(x, defaultParams) - const source = createDownload(x) - return Rx.Observable.merge(source.start(), source.stats) - }) diff --git a/src/bin/ResumeDownload.js b/src/bin/ResumeDownload.js deleted file mode 100644 index f90ac57..0000000 --- a/src/bin/ResumeDownload.js +++ /dev/null @@ -1,15 +0,0 @@ -import {NormalizePath} from '../Utils' -import _ from 'lodash' -import Rx from 'rx' - -export default (createDownload, pFlags) => pFlags - .skipWhile((x) => x.url) - .flatMap((x) => { - const defaultParams = { - mtdPath: NormalizePath(x.file), - path: NormalizePath(x.file.replace('.mtd', '')) - } - _.defaults(x, defaultParams) - const source = createDownload(x) - return Rx.Observable.merge(source.download(), source.stats) - }) diff --git a/src/bin/mtd.js b/src/bin/mtd.js index 8facb5c..66a59c7 100644 --- a/src/bin/mtd.js +++ b/src/bin/mtd.js @@ -4,17 +4,10 @@ */ 'use strict' - -import Rx from 'rx' -import _ from 'lodash' +import * as U from '../Utils' import meow from 'meow' -import humanize from 'humanize-plus' -import newDownload from './NewDownload' -import resumeDownload from './ResumeDownload' -import {createDownload} from '../Main' -import ProgressBar from 'progress' - -const flags = meow(` +import R from 'ramda' +const HELP_TEXT = ` Usage mtd @@ -25,39 +18,12 @@ const flags = meow(` Examples mtd --url http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4 mtd --file big_buck_bunny_720p_1mb.mp4.mtd - `).flags + ` +const flags = meow(HELP_TEXT).flags -if (!_.some([flags.url, flags.file], (x) => x)) { +if (!R.any(R.identity)([flags.url, flags.file])) { + console.log(HELP_TEXT) process.exit(0) } -const pFlags = Rx.Observable.just(flags) - -// TODO: Add unit tests -const downloads = Rx.Observable.merge( - newDownload(createDownload, pFlags), - resumeDownload(createDownload, pFlags) -).share() - -const progress = downloads - .pluck('message', 'totalBytes') - .filter((x) => x > 0) - .first() - .map((total) => new ProgressBar(':bar :percent', {total, complete: '█', incomplete: '░'})) - .tap((x) => console.log(`SIZE: ${humanize.fileSize(x.total)}`)).share() - -downloads - .filter((x) => x) - .filter((x) => x.event === 'DATA') - .pluck('message') - - .map((x) => _.sum(_.map(x.offsets, (o, i) => o - x.threads[i][0])) / x.totalBytes) - .withLatestFrom(progress, (bytes, progress) => ({bytes, progress})) - .subscribe((x) => x.progress.update(x.bytes)) - -downloads.last() - .withLatestFrom(progress, (a, b) => b) - .subscribe((x) => { - x.update(x.total) - console.log('Download Completed!') - }) +U.CLI(flags)