diff --git a/modules/configure-commander.js b/modules/configure-commander.js new file mode 100644 index 0000000..b3add02 --- /dev/null +++ b/modules/configure-commander.js @@ -0,0 +1,26 @@ +exports.setup = function(args){ + + // Setup Command Line Interface + var program = require('commander'); + program + .version('1.21.0') + .option('-p, --port ', 'Select Port', /^\d+$/i, 3000) + .option('-t, --tunnel', 'Use nat-pmp to configure port fowarding') + .option('-g, --gateip ', 'Manually set gateway IP for the tunnel option') + .option('-l, --login', 'Require users to login') + .option('-u, --user ', 'Set Username') + .option('-x, --password ', 'Set Password') + .option('-G, --guest ', 'Set Guest Username') + .option('-X, --guestpassword ', 'Set Guest Password') + // .option('-k, --key ', 'Add SSL Key') + // .option('-c, --cert ', 'Add SSL Certificate') + .option('-d, --database ', 'Specify Database Filepath', 'mstreamdb.lite') + .option('-b, --beetspath ', 'Specify Folder where Beets DB should import music from. This also overides the normal DB functions with functions that integrate with beets DB') + .option('-b, --databaseplugin ', '', /^(default|beets)$/i, 'default') + .option('-i, --userinterface ', 'Specify folder name that will be served as the UI', 'public') + .option('-f, --filepath ', 'Set the path of your music directory', process.cwd()) + .option('-s, --secret ', 'Set the login secret key') + .parse(args); + + return program; +} diff --git a/modules/configure-json-file.js b/modules/configure-json-file.js new file mode 100644 index 0000000..ac58676 --- /dev/null +++ b/modules/configure-json-file.js @@ -0,0 +1,30 @@ +const fs = require('graceful-fs'); // File System + + +exports.setup = function(args){ + // Open File + try{ + var loadJson = JSON.parse(fs.readFileSync(args[args.length-1], 'utf8')); + }catch(err){ + console.log('Failed to parse JSON file'); + return false; + } + + // Check for validity + if(!loadJson.filepath){ + loadJson.filepath = process.cwd(); + } + + if(!loadJson.databaseplugin){ + loadJson.databaseplugin = 'default'; + }else{ + var re = new RegExp("/^(default|beets)$/i"); + if(!re.test(loadJson.databaseplugin)){ + console.log('Incorrect database plugin. Please update and try again'); + return false; + } + } + + // Export JSON + return loadJson; +} diff --git a/mstream.js b/mstream.js index e566e0c..b1f4799 100755 --- a/mstream.js +++ b/mstream.js @@ -1,29 +1,6 @@ #!/usr/bin/env node "use strict"; -// Setup Command Line Interface -var program = require('commander'); -program - .version('1.21.0') - .option('-p, --port ', 'Select Port', /^\d+$/i, 3000) - .option('-t, --tunnel', 'Use nat-pmp to configure port fowarding') - .option('-g, --gateip ', 'Manually set gateway IP for the tunnel option') - .option('-l, --login', 'Require users to login') - .option('-u, --user ', 'Set Username') - .option('-x, --password ', 'Set Password') - .option('-G, --guest ', 'Set Guest Username') - .option('-X, --guestpassword ', 'Set Guest Password') - // .option('-k, --key ', 'Add SSL Key') - // .option('-c, --cert ', 'Add SSL Certificate') - .option('-d, --database ', 'Specify Database Filepath', 'mstreamdb.lite') - .option('-b, --beetspath ', 'Specify Folder where Beets DB should import music from. This also overides the normal DB functions with functions that integrate with beets DB') - .option('-b, --databaseplugin ', '', /^(default|beets)$/i, 'default') - .option('-i, --userinterface ', 'Specify folder name that will be served as the UI', 'public') - .option('-f, --filepath ', 'Set the path of your music directory', process.cwd()) - .option('-s, --secret ', 'Set the login secret key') - .parse(process.argv); - - const express = require('express'); const mstream = express(); const fs = require('graceful-fs'); // File System @@ -34,6 +11,23 @@ const os = require('os'); const crypto = require('crypto'); const slash = require('slash'); const sqlite3 = require('sqlite3').verbose(); + + +var startup = 'configure-commander'; +// If the user gives a json file then try pulling the config from that +try{ + if(fe.extname(process.argv[process.argv.length-1]) == '.json' && fs.statSync(process.argv[process.argv.length-1]).isFile()){ + startup = 'configure-json-file'; + } +}catch(error){ + console.log('JSON file does not appear to exist'); + process.exit(); +} + +const program = require('./modules/' + startup + '.js').setup(process.argv); +if(program == false){ + process.exit(); +} const db = new sqlite3.Database(program.database); @@ -129,7 +123,7 @@ if(program.login){ if(secretIsFile === true){ - secret = fs.readFileSync(program.secret, 'utf8') + secret = fs.readFileSync(program.secret, 'utf8'); }else if(program.secret){ secret = String(program.secret); }else{