diff --git a/badger-am.js b/badger-am.js index e94a93e..d7cd7a7 100755 --- a/badger-am.js +++ b/badger-am.js @@ -71,37 +71,9 @@ function convert(input, output, options) { // sort files function sort(input, output, options) { - async.waterfall([ - function (asyncCallback) { - recursive(input, [ignoreFilter], asyncCallback); - }, - function (files, asyncCallback) { - console.log(files.length + ' \'' + options.format + '\' files found'); - // display progressbar - const bar = util.createProgressBar(files.length); - // handle each file - async.eachLimit(files, commander.concurrency, function (file, eachCallback) { - processFileSort(output, file, options, function (err) { - bar.tick(); - if (err) { - return eachCallback(err); - } - eachCallback(); - }); - }, function (err) { - if (err) { - return asyncCallback(err); - } - asyncCallback(); - }); - } - ], function (err, result) { - util.exit(err, start); - }); - - function ignoreFilter(file, stats) { - return !stats.isDirectory() && path.extname(file).indexOf(options.format) == -1; - } + util.batchSort(getConfig(input, output, options, commander), function (err) { + cli.exit(err); + }) } // move file to location defined by its metadata diff --git a/lib/util.js b/lib/util.js index b8cb23e..edf4316 100644 --- a/lib/util.js +++ b/lib/util.js @@ -4,6 +4,66 @@ const async = require('async'); const fse = require('fs-extra'); const recursive = require('recursive-readdir'); const progress = require('progress'); +const cli = require('./cli'); +const audio = require('./audio'); + +// move all files from input to output directory +function batchSort(config, callback) { + async.waterfall([ + // get files + function (waterfallCallback) { + readDirRecursive(config.input, config.format, waterfallCallback); + }, + // display info, prompt user and create progressbar + function (files, waterfallCallback) { + cli.askForConfirmation(files.length + ' files found.\nstart sorting now?', ['yes', 'y'], function (err) { + if (err) { + return waterfallCallback(err); + } + waterfallCallback(null, files, cli.createProgressBar(files.length)); + }); + }, + // process each file + function (files, bar, waterfallCallback) { + async.eachLimit(files, config.concurrency, function (file, eachCallback) { + moveFile(file, config, function (err) { + bar.tick(); + if (err) { + return eachCallback(err); + } + eachCallback(); + }) + }, waterfallCallback); + } + ], callback); +} + +// create target directory and move the file +function moveFile(source, config, callback) { + async.waterfall([ + // get metadata + function (waterfallCallback) { + audio.extractMetadata(source, waterfallCallback); + }, + // create path from metadata + function (metadata, waterfallCallback) { + getPathByMetadata(source, config.output, metadata, waterfallCallback); + }, + // create target directory + function (target, waterfallCallback) { + fse.mkdirs(path.dirname(target), function (err) { + if (err) { + return waterfallCallback(err); + } + waterfallCallback(null, target); + }); + }, + // move file + function (target, waterfallCallback) { + fse.move(source, target, waterfallCallback); + } + ], callback); +} // create path for target file function getPathByMetadata(source, output, metadata, callback) { @@ -50,23 +110,6 @@ function getPathByMetadata(source, output, metadata, callback) { callback(null, path.join(filePath, fileName)); } -// create target directory and move the file -function moveFile(source, target, callback) { - async.series([ - function (asyncCallback) { - fse.mkdirs(path.dirname(target), asyncCallback); - }, - function (asyncCallback) { - fse.move(source, target, asyncCallback); - } - ], function (err, results) { - if (err) { - return callback(err); - } - callback(); - }); -} - // fill a string beginning from the front function frontFill(string, fill, length) { while (string.toString().length < length) { @@ -87,7 +130,8 @@ function readDirRecursive(where, extension, callback) { } // api -exports.getPathByMetadata = getPathByMetadata; +exports.batchSort = batchSort; exports.moveFile = moveFile; +exports.getPathByMetadata = getPathByMetadata; exports.frontFill = frontFill; exports.readDirRecursive = readDirRecursive; \ No newline at end of file diff --git a/package.json b/package.json index 0e96770..954129d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "badger-am", - "version": "0.5.0", + "version": "0.5.1", "license": "MIT", "description": "audio manager", "author": "Daniel Sommer ",