badger-am/lib/util.js

103 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-03-24 22:05:51 +01:00
// requirements
2017-03-24 22:11:22 +01:00
const path = require('path');
const async = require('async');
const fse = require('fs-extra');
2017-03-24 22:05:51 +01:00
const progress = require('progress');
// print logo
exports.printLogo = function printLogo() {
console.log(' _ _ _ __ __ ');
console.log(' | |__ __ _ __| |__ _ ___ _ _ /_\\ | \\/ |');
console.log(' | \'_ \\\/ _` \/ _` \/ _` / -_) \'_\/ _ \\| |\\/| |');
console.log(' |_.__\/\\__,_\\__,_\\__, \\___|_|\/_\/ \\_\\_| |_|');
console.log(' |___/ ');
};
// create path for target file
exports.pathFromMetadata = function pathFromMetadata(file, output, metadata, callback) {
// define directory
let filePath = path.normalize(output);
if (metadata.albumartist && metadata.albumartist.length > 0) {
let tmp;
const artistCount = metadata.albumartist.length;
for (let counter = 0; counter < artistCount; counter++) {
if (counter > 0) {
tmp += ' - ' + metadata.albumartist[counter];
} else {
tmp = metadata.albumartist[counter];
}
}
filePath = path.join(filePath, tmp);
} else {
filePath = path.join(filePath, metadata.artist[0]);
}
if (metadata.album) {
filePath = path.join(filePath, metadata.album);
}
// define filename
let fileName = '';
if (metadata.disk.no) {
fileName += frontFill(metadata.disk.no, '0', 2);
}
if (metadata.track.no) {
if (fileName) {
fileName += '-' + frontFill(metadata.track.no, '0', 2) + ' ';
} else {
fileName += frontFill(metadata.track.no, '0', 2) + ' ';
}
}
if (metadata.artist) {
fileName += metadata.artist[0] + ' - ';
}
if (metadata.title) {
fileName += metadata.title;
}
// append extension
fileName += path.extname(file);
// join directory and name
callback(null, path.join(filePath, fileName));
};
// create target directory and move the file
exports.moveFile = 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();
});
2017-03-24 22:11:22 +01:00
};
2017-03-24 22:05:51 +01:00
// fill a string beginning from the front
exports.frontfill = function frontFill(string, fill, length) {
while (string.toString().length < length) {
string = fill + string;
}
return string;
};
// create a ascii progressbar
exports.createProgressBar = function createProgressBar(total) {
return new progress(':bar | progress: :current/:total (:percent) | elapsed: :elapseds | eta: :etas', {
total: total,
width: 32
});
};
// shutdown
exports.exit = 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);
};