refactored and extended command line functionality

This commit is contained in:
Daniel Sommer 2017-03-27 22:29:44 +02:00
parent e8f7c33a46
commit 48662e75b8
3 changed files with 74 additions and 31 deletions

60
lib/cli.js Normal file
View file

@ -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;

View file

@ -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
});
// list files in directory
function readDirRecursive(where, extension, callback) {
if (extension.indexOf('.') !== 0) {
extension = '.' + extension;
}
// shutdown
function exit(err, start) {
if (err) {
console.error(err);
process.exit(1);
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;
exports.readDirRecursive = readDirRecursive;

View file

@ -1,6 +1,6 @@
{
"name": "badger-am",
"version": "0.4.1",
"version": "0.5.0",
"license": "MIT",
"description": "audio manager",
"author": "Daniel Sommer <daniel.sommer@velvettear.de>",