|
|
@@ -57,11 +57,68 @@ String.prototype.toTitleCase = function() {
|
|
|
return str;
|
|
|
};
|
|
|
|
|
|
+// get the db from the path
|
|
|
+const getDbForDir = (dir, opts) => {
|
|
|
+ let options = opts || Object.assign({}, defaults, paths[dir]); // baseline the options
|
|
|
+ let adapter = new FileSync(options.database); // init the db adapter
|
|
|
+ let db = low(adapter); // connect the db
|
|
|
+ return db;
|
|
|
+};
|
|
|
+
|
|
|
+// find a file in the db
|
|
|
+const findFile = (db, file) => {
|
|
|
+ return db.get('files').find({ input:file }).value(); // does it already exist?
|
|
|
+};
|
|
|
+
|
|
|
+// set a file in the db
|
|
|
+const setFile = (db, file, payload) => {
|
|
|
+ if (!payload && typeof file === 'object')
|
|
|
+ return db.get('files').push(file).write();
|
|
|
+ return db.get('files').find({ input: file }).assign(payload).write();
|
|
|
+};
|
|
|
+
|
|
|
+// remove a file from the db
|
|
|
+const removeFile = (db, file) => {
|
|
|
+ return db.get('files').remove({ input: file }).write(); // remove file from database
|
|
|
+};
|
|
|
+
|
|
|
+// write process output
|
|
|
+const processOutput = (str) => {
|
|
|
+ process.stdout.clearLine();
|
|
|
+ process.stdout.cursorTo(0);
|
|
|
+ process.stdout.write(str);
|
|
|
+};
|
|
|
+
|
|
|
+// spawn a handbrake
|
|
|
+const processWithHandbrake = (input, output, preset) => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const filename = path.basename(input);
|
|
|
+ hbjs.spawn({
|
|
|
+ input: input,
|
|
|
+ output: output,
|
|
|
+ preset: preset
|
|
|
+ }).on('start', (err) => {
|
|
|
+ processOutput(` --> processing "${filename}" to "${output}" with "${preset}"`);
|
|
|
+ }).on('error', (err) => {
|
|
|
+ processOutput(` --> processing ${output} error: ${err.message || err}.\n`);
|
|
|
+ reject(err);
|
|
|
+ }).on('progress', (progress) => {
|
|
|
+ processOutput(` --> processing ${output} - ${progress.percentComplete}, ETA: ${progress.eta}`);
|
|
|
+ }).on('cancelled', () => {
|
|
|
+ processOutput(` --> processing ${output} cancelled\n`);
|
|
|
+ reject(new Error(`Processing ${output} cancelled`));
|
|
|
+ }).on('complete', () => {
|
|
|
+ processOutput(` --> processing ${output} complete\n`);
|
|
|
+ resolve(true);
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
if (args.version) return version();
|
|
|
if (args.help || !args.config) return help();
|
|
|
|
|
|
-const ignoreInitial = (args.hasOwnProperty('ignoreInitial')) ? args.ignoreInitial : false;
|
|
|
-const test = (args.hasOwnProperty('test')) ? args.test : false;
|
|
|
+const ignoreInitial = (args.hasOwnProperty('ignoreInitial')) ? args.ignoreInitial : false; // ignore initial files
|
|
|
+const test = (args.hasOwnProperty('test')) ? args.test : false; // do a dry run ... just log
|
|
|
|
|
|
// get our paths
|
|
|
const paths = require(args.config);
|
|
|
@@ -99,11 +156,8 @@ let watches = []; // array of things to watch
|
|
|
for (let d in dirs) { // loop the dirs
|
|
|
let dir = dirs[d]; // pointer
|
|
|
let options = Object.assign({}, defaults, paths[dir]); // baseline the options
|
|
|
- let adapter = new FileSync(options.database); // init our db adapter
|
|
|
- let db = low(adapter); // create a db connection
|
|
|
- db.defaults({
|
|
|
- files: []
|
|
|
- }).write(); // init the database
|
|
|
+ let db = getDbForDir(dir);
|
|
|
+ db.defaults({ files: [] }).write(); // init the database
|
|
|
for (let e in options.exts) { // loop the exts to watch
|
|
|
let ext = options.exts[e]; // alias the ext
|
|
|
watches.push(`${dir}/**/*.${ext}`); // push the watch
|
|
|
@@ -114,7 +168,7 @@ const watcher = chokidar.watch(watches, opts); // init our watcher
|
|
|
console.log('Watching', watches);
|
|
|
|
|
|
// when a new file is added
|
|
|
-watcher.on('add', (file) => {
|
|
|
+watcher.on('add', async (file) => {
|
|
|
|
|
|
let adapter,
|
|
|
db,
|
|
|
@@ -136,18 +190,17 @@ watcher.on('add', (file) => {
|
|
|
let dir = dirs[i]; // pointer to the dir
|
|
|
if (file && dir && file.indexOf(dir) > -1) { // is this in this path?
|
|
|
options = Object.assign({}, defaults, paths[dir]); // baseline the options
|
|
|
- adapter = new FileSync(options.database); // init the db adapter
|
|
|
- db = low(adapter); // connect the db
|
|
|
- let found = db.get('files').find({ input:file }).value(); // does it already exist?
|
|
|
+ db = getDbForDir(dir, options); // init the db connection
|
|
|
+ let found = findFile(db, file); // does it already exist?
|
|
|
if (found && found.status && found.status === 'success') { // was it already processed?
|
|
|
//console.log(`File ${file} has already been successfully processed.`);
|
|
|
return; // break this loop
|
|
|
} else if (!found) { // was it found?
|
|
|
- console.log(`File ${file} has been added for processing.`);
|
|
|
- db.get('files').push({ input:file, output:'', status:'', date:new Date() }).write(); // push onto the list an entry
|
|
|
+ processOutput(`-> ${path.basename(file)} [processing]`);
|
|
|
+ setFile(db, { input:file, output:'', status:'', date:new Date() }); // push onto the list an entry
|
|
|
} else {
|
|
|
- console.log(`File ${file} has been added for re-processing.`);
|
|
|
- db.get('files').find({ input: file }).assign({ status:'', date:new Date() }).write(); // set the status to blank
|
|
|
+ processOutput(`-> ${path.basename(file)} [re-processing]`);
|
|
|
+ setFile(db, file, { status:'', date:new Date() }); // set the status to blank
|
|
|
}
|
|
|
preset = options.preset; // apply the specifics ...
|
|
|
clean = options.clean; // ...
|
|
|
@@ -182,64 +235,44 @@ watcher.on('add', (file) => {
|
|
|
let target = destination + ((folder) ? '/' + folder : ''); // setup the target location
|
|
|
output = target + '/' + output + '.' + ext; // update the new name
|
|
|
|
|
|
- db.get('files').find({
|
|
|
- input: file
|
|
|
- }).assign({
|
|
|
- output: output
|
|
|
- }).write(); // update database with output
|
|
|
+ if (fs.existsSync(output)) {
|
|
|
+ processOutput(`-> ${path.basename(file)} [skipping ... already processed]\n`);
|
|
|
+ setFile(db, file, { status: 'success', date:new Date() }); // update database with status
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ processOutput('\n');
|
|
|
+
|
|
|
+ setFile(db, file, { output: output }); // update database with output
|
|
|
|
|
|
if (target && !fs.existsSync(target)) {
|
|
|
- console.log('Creating target directory:', target);
|
|
|
+ console.log(' --> creating parent directory:', target);
|
|
|
fs.mkdirSync(target, {
|
|
|
recursive: true
|
|
|
});
|
|
|
}
|
|
|
- console.log(`Processing "${input}" to "${output}" with "${preset}"`);
|
|
|
+
|
|
|
+ //console.log(` --> processing "${path.basename(input)}" to "${output}" with "${preset}"`);
|
|
|
// spawn handbrake
|
|
|
if (!test) {
|
|
|
- hbjs.spawn({
|
|
|
- input: input,
|
|
|
- output: output,
|
|
|
- preset: preset
|
|
|
- }).on('start', (err) => {
|
|
|
- process.stdout.clearLine();
|
|
|
- process.stdout.cursorTo(0);
|
|
|
- process.stdout.write(`Processing "${input}" to "${output}" with "${preset}"`);
|
|
|
- }).on('error', (err) => {
|
|
|
- process.stdout.clearLine();
|
|
|
- process.stdout.cursorTo(0);
|
|
|
- process.stdout.write(`Processing ${output} error: ${err.message || err}.\n`);
|
|
|
- db.get('files').find({ input: file }).assign({ status: 'failure', date:new Date() }).write(); // update database with status
|
|
|
- }).on('progress', (progress) => {
|
|
|
- process.stdout.clearLine();
|
|
|
- process.stdout.cursorTo(0);
|
|
|
- process.stdout.write(`Processing ${output} - Percent complete: ${progress.percentComplete}, ETA: ${progress.eta}`);
|
|
|
- }).on('cancelled', () => {
|
|
|
- process.stdout.clearLine();
|
|
|
- process.stdout.cursorTo(0);
|
|
|
- process.stdout.write(`Processing ${output} cancelled.\n`);
|
|
|
- db.get('files').find({ input: file }).assign({ status: 'failure', date:new Date() }).write(); // update database with status
|
|
|
- }).on('complete', () => {
|
|
|
- process.stdout.clearLine();
|
|
|
- process.stdout.cursorTo(0);
|
|
|
- process.stdout.write(`Processing ${output} complete.\n`);
|
|
|
- db.get('files').find({ input: file }).assign({ status: 'success', date:new Date() }).write(); // update database with status
|
|
|
- });
|
|
|
+ try {
|
|
|
+ await processWithHandbrake(input, output, preset);
|
|
|
+ setFile(db, file, { status: 'success', date:new Date() }); // update database with status
|
|
|
+ } catch (err) {
|
|
|
+ setFile(db, file, { status: 'failure', date:new Date() }); // update database with status
|
|
|
+ }
|
|
|
}
|
|
|
}).on('change', (file) => {
|
|
|
- console.log(`File ${file} has been changed.`);
|
|
|
-}).on('unlink', (file) => {
|
|
|
- console.log(`File ${file} has been removed.`);
|
|
|
+ console.log(` -> ${file} has been changed`);
|
|
|
+}).on('unlink', async (file) => {
|
|
|
+ console.log(` -> ${file} has been removed`);
|
|
|
let adapter, db, options;
|
|
|
for (let i = 0, l = dirs.length; i < l; i++) {
|
|
|
let dir = dirs[i]; // pointer to the dir
|
|
|
if (file && dir && file.indexOf(dir) > -1) { // is this in this path?
|
|
|
- options = Object.assign({}, defaults, paths[dir]); // baseline the options
|
|
|
- adapter = new FileSync(options.database); // init the db adapter
|
|
|
- db = low(adapter); // connect the db
|
|
|
- db.get('files').remove({ input: file }).write(); // remove file form database
|
|
|
+ db = getDbForDir(dir); // init the db connection
|
|
|
+ removeFile(db, file); // remove file form database
|
|
|
}
|
|
|
}
|
|
|
}).on('error', (error) => {
|
|
|
- console.error(`Error: ${error}.`);
|
|
|
+ console.error(` -> Error: ${error.message || error}`);
|
|
|
});
|