From 48662e75b8e4db2d6d40183fcce4036c05e22781 Mon Sep 17 00:00:00 2001 From: velvettear Date: Mon, 27 Mar 2017 22:29:44 +0200 Subject: [PATCH] refactored and extended command line functionality --- lib/cli.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/util.js | 43 ++++++++++++------------------------- package.json | 2 +- 3 files changed, 74 insertions(+), 31 deletions(-) create mode 100644 lib/cli.js diff --git a/lib/cli.js b/lib/cli.js new file mode 100644 index 0000000..8098dbf --- /dev/null +++ b/lib/cli.js @@ -0,0 +1,60 @@ +// requirements +const progress = require('progress'); +const readline = require('readline'); + +// print logo +function printLogo() { + console.log(' _ _ _ __ __ '); + console.log(' | |__ __ _ __| |__ _ ___ _ _ /_\\ | \\/ |'); + console.log(' | \'_ \\\/ _` \/ _` \/ _` / -_) \'_\/ _ \\| |\\/| |'); + console.log(' |_.__\/\\__,_\\__,_\\__, \\___|_|\/_\/ \\_\\_| |_|'); + console.log(' |___/ '); +} + +// display a confirmation prompt +function askForConfirmation(message, confirms, callback) { + const line = readline.createInterface({ + input: process.stdin, + output: process.stdout + }); + message += ' ['; + for (let counter = 0, length = confirms.length; counter < length; counter++) { + if (counter > 0) { + message += '/' + confirms[counter]; + } else { + message += confirms[counter]; + } + } + message += ']'; + line.question(message + '\n', function (answer) { + if (confirms.indexOf(answer) === -1) { + return callback('wrong input: \'' + answer + '\''); + } + callback(); + }); +} + +// create a ascii progressbar +function createProgressBar(total) { + return new progress(':bar | progress: :current/:total (:percent) | elapsed: :elapseds | eta: :etas', { + total: total, + width: 32 + }); +} + +// shutdown +function exit(err, start) { + if (err) { + console.error(err); + process.exit(1); + } + const diff = process.hrtime(start); + console.log('exiting after ' + ((diff[0] + (diff[1] / 1000000)) / 1000).toFixed(2) + ' seconds'); + process.exit(0); +} + +// api +exports.printLogo = printLogo; +exports.askForConfirmation = askForConfirmation; +exports.createProgressBar = createProgressBar; +exports.exit = exit; diff --git a/lib/util.js b/lib/util.js index a64e897..b8cb23e 100644 --- a/lib/util.js +++ b/lib/util.js @@ -2,18 +2,11 @@ const path = require('path'); const async = require('async'); const fse = require('fs-extra'); +const recursive = require('recursive-readdir'); const progress = require('progress'); -function printLogo() { - console.log(' _ _ _ __ __ '); - console.log(' | |__ __ _ __| |__ _ ___ _ _ /_\\ | \\/ |'); - console.log(' | \'_ \\\/ _` \/ _` \/ _` / -_) \'_\/ _ \\| |\\/| |'); - console.log(' |_.__\/\\__,_\\__,_\\__, \\___|_|\/_\/ \\_\\_| |_|'); - console.log(' |___/ '); -} - // create path for target file -function pathFromMetadata(file, output, metadata, callback) { +function getPathByMetadata(source, output, metadata, callback) { // define directory let filePath = path.normalize(output); if (metadata.albumartist && metadata.albumartist.length > 0) { @@ -52,7 +45,7 @@ function pathFromMetadata(file, output, metadata, callback) { fileName += metadata.title; } // append extension - fileName += path.extname(file); + fileName += path.extname(source); // join directory and name callback(null, path.join(filePath, fileName)); } @@ -82,29 +75,19 @@ function frontFill(string, fill, length) { return string; } -// create a ascii progressbar -function createProgressBar(total) { - return new progress(':bar | progress: :current/:total (:percent) | elapsed: :elapseds | eta: :etas', { - total: total, - width: 32 - }); -} - -// shutdown -function exit(err, start) { - if (err) { - console.error(err); - process.exit(1); +// list files in directory +function readDirRecursive(where, extension, callback) { + if (extension.indexOf('.') !== 0) { + extension = '.' + extension; + } + recursive(where, [ignoreFilter], callback); + function ignoreFilter(file, stats) { + return !stats.isDirectory() && extension.indexOf(path.extname(file)) === -1; } - const diff = process.hrtime(start); - console.log('exiting after ' + ((diff[0] + (diff[1] / 1000000)) / 1000).toFixed(2) + ' seconds'); - process.exit(0); } // api -exports.printLogo = printLogo; -exports.pathFromMetadata = pathFromMetadata; +exports.getPathByMetadata = getPathByMetadata; exports.moveFile = moveFile; exports.frontFill = frontFill; -exports.createProgressBar = createProgressBar; -exports.exit = exit; \ No newline at end of file +exports.readDirRecursive = readDirRecursive; \ No newline at end of file diff --git a/package.json b/package.json index eed97ac..0e96770 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "badger-am", - "version": "0.4.1", + "version": "0.5.0", "license": "MIT", "description": "audio manager", "author": "Daniel Sommer ",