From fd083edbfbdff7a4d8c575b494d37409228f67be Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 4 Jan 2019 17:53:22 +0100 Subject: [PATCH] main: move options in a separate module, disable user setting files if HOME is not defined (#453) --- src/server/options.js | 77 +++++++++++++++++++++++++++++++++ src/server/settings.js | 98 ++++++------------------------------------ 2 files changed, 90 insertions(+), 85 deletions(-) create mode 100644 src/server/options.js diff --git a/src/server/options.js b/src/server/options.js new file mode 100644 index 00000000..03a2db2a --- /dev/null +++ b/src/server/options.js @@ -0,0 +1,77 @@ +module.exports = { + 's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port or domain:port pairs)', + check: (s)=>{ + return s.some(item=>!item.match(/^[^:]*:[0-9]{4,5}$/)) ? + 'Targets must be ip:port domain:port pairs & port must be >= 1024' : true + } + }, + 'l':{alias:'load',type:'string',file:{name:'OSC Session (.json)', extensions: ['json', 'js']},describe:'session file to load'}, + 'state':{type:'string',file:{name:'OSC State (.state)', extensions: ['state']},describe:'state file to load'}, + 'b':{alias:'blank',type:'boolean',describe:'load a blank session and start the editor'}, + 'c':{alias:'custom-module',type:'array',file:{name:'OSC Custom module (.js)', extensions: ['js']},describe:'custom module file to load (custom options can be passed after the filename)'}, + 'p':{alias:'port',type:'number',describe:'http port of the server (default to 8080)', + check: (p)=>{ + return (!isNaN(p) && p > 1023 && parseInt(p)===p) ? + true : 'Port must be an integer >= 1024' + } + }, + 'o':{alias:'osc-port',type:'number',describe:'osc input port (default to --port)', + check: (o)=>{ + return (!isNaN(o) && o > 1023 && parseInt(o)===o) ? + true : 'Port must be an integer >= 1024' + } + }, + 'tcp-port':{type:'number', describe:'TCP server input port', + check: (t, argv)=>{ + var h = argv['port'] || 8080 + if (t===h) return 'TCP input port must different from --port' + return (!isNaN(t) && t > 1023 && parseInt(t)===t) ? + true : 'Port must be an integer >= 1024' + } + }, + 'tcp-targets':{type:'array',describe:'TCP servers to connect to (ip:port pairs), does not susbtitute for --send', + check: (s, argv)=>{ + if (!argv['tcp-port']) return '--tcp-port must be set' + return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? + 'Targets must be ip:port pairs & port must be >= 1024' : true + } + }, + 'm':{alias:'midi',type:'array',describe:'midi router settings'}, + 'd':{alias:'debug',type:'boolean',describe:'log received osc messages in the console'}, + 'n':{alias:'no-gui',type:'boolean',describe:'disable default gui', + check: (n,argv)=>{ + return (!n || !argv.g) ? + true : 'no-gui and gui-only can\'s be enabled simultaneously' + } + }, + 'g':{alias:'gui-only',type:'string',describe:'app server\'s url. If true, local port (--port) is used', + check: (g,argv)=>{ + return (!g || !argv.n) ? + true : 'no-gui and gui-only can\'s be enabled simultaneously' + } + }, + 't':{alias:'theme',type:'array',describe:'theme name or path (mutliple values allowed)'}, + 'e':{alias:'examples',type:'boolean',describe:'list examples instead of recent sessions', + check: (e,argv)=>{ + return (!e || !argv.l) ? + true : 'examples can\'t be listed if --load is set' + } + }, + 'url-options':{type:'array',describe:'url options (opt=value pairs)', + check: (u, argv)=>{ + return (!u || !argv.n) ? + true : 'url options can\'t be passed in no-gui mode' + } + }, + 'disable-vsync':{type:'boolean',describe:'disable gui\'s vertical synchronization', restart: true}, + 'disable-gpu':{type:'boolean',describe:'disable hardware acceleration', restart: true}, + 'read-only':{type:'boolean',describe:'disable session editing and session history changes', + check: (r, argv)=>{ + return (!r || !argv.b) ? + true : 'blank session can\'t be started in read-only mode' + } + }, + 'remote-saving':{type:'string',describe:'disable remote session saving for hosts that don\'t match the regular expresion'}, + 'instance-name':{type:'string',describe:'used to differenciate multiple instances in a zeroconf network'}, + 'fullscreen':{type:'boolean', describe:'launch in fullscreen mode (only affects the default gui, F11 to exit)'} +} diff --git a/src/server/settings.js b/src/server/settings.js index 0b98b14f..031def48 100644 --- a/src/server/settings.js +++ b/src/server/settings.js @@ -1,89 +1,6 @@ // This prevents argv parsing to be breaked when the app is packaged (executed without 'electron' prefix) if (process.argv[1]&&process.argv[1].indexOf('-')==0) process.argv.unshift('') -var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], - configPath = require('path').join(baseDir, '.open-stage-control'), - fs = require('fs'), - ifaces = require('os').networkInterfaces() - -var options = { - 's':{alias:'send',type:'array',describe:'default targets for all widgets (ip:port or domain:port pairs)', - check: (s)=>{ - return s.some(item=>!item.match(/^[^:]*:[0-9]{4,5}$/)) ? - 'Targets must be ip:port domain:port pairs & port must be >= 1024' : true - } - }, - 'l':{alias:'load',type:'string',file:{name:'OSC Session (.json)', extensions: ['json', 'js']},describe:'session file to load'}, - 'state':{type:'string',file:{name:'OSC State (.state)', extensions: ['state']},describe:'state file to load'}, - 'b':{alias:'blank',type:'boolean',describe:'load a blank session and start the editor'}, - 'c':{alias:'custom-module',type:'array',file:{name:'OSC Custom module (.js)', extensions: ['js']},describe:'custom module file to load (custom options can be passed after the filename)'}, - 'p':{alias:'port',type:'number',describe:'http port of the server (default to 8080)', - check: (p)=>{ - return (!isNaN(p) && p > 1023 && parseInt(p)===p) ? - true : 'Port must be an integer >= 1024' - } - }, - 'o':{alias:'osc-port',type:'number',describe:'osc input port (default to --port)', - check: (o)=>{ - return (!isNaN(o) && o > 1023 && parseInt(o)===o) ? - true : 'Port must be an integer >= 1024' - } - }, - 'tcp-port':{type:'number', describe:'TCP server input port', - check: (t, argv)=>{ - var h = argv['port'] || 8080 - if (t===h) return 'TCP input port must different from --port' - return (!isNaN(t) && t > 1023 && parseInt(t)===t) ? - true : 'Port must be an integer >= 1024' - } - }, - 'tcp-targets':{type:'array',describe:'TCP servers to connect to (ip:port pairs), does not susbtitute for --send', - check: (s, argv)=>{ - if (!argv['tcp-port']) return '--tcp-port must be set' - return s.some(item=>!item.match('^[^:\s\"\']*:[0-9]{4,5}[\s\"\']*$')) ? - 'Targets must be ip:port pairs & port must be >= 1024' : true - } - }, - 'm':{alias:'midi',type:'array',describe:'midi router settings'}, - 'd':{alias:'debug',type:'boolean',describe:'log received osc messages in the console'}, - 'n':{alias:'no-gui',type:'boolean',describe:'disable default gui', - check: (n,argv)=>{ - return (!n || !argv.g) ? - true : 'no-gui and gui-only can\'s be enabled simultaneously' - } - }, - 'g':{alias:'gui-only',type:'string',describe:'app server\'s url. If true, local port (--port) is used', - check: (g,argv)=>{ - return (!g || !argv.n) ? - true : 'no-gui and gui-only can\'s be enabled simultaneously' - } - }, - 't':{alias:'theme',type:'array',describe:'theme name or path (mutliple values allowed)'}, - 'e':{alias:'examples',type:'boolean',describe:'list examples instead of recent sessions', - check: (e,argv)=>{ - return (!e || !argv.l) ? - true : 'examples can\'t be listed if --load is set' - } - }, - 'url-options':{type:'array',describe:'url options (opt=value pairs)', - check: (u, argv)=>{ - return (!u || !argv.n) ? - true : 'url options can\'t be passed in no-gui mode' - } - }, - 'disable-vsync':{type:'boolean',describe:'disable gui\'s vertical synchronization', restart: true}, - 'disable-gpu':{type:'boolean',describe:'disable hardware acceleration', restart: true}, - 'read-only':{type:'boolean',describe:'disable session editing and session history changes', - check: (r, argv)=>{ - return (!r || !argv.b) ? - true : 'blank session can\'t be started in read-only mode' - } - }, - 'remote-saving':{type:'string',describe:'disable remote session saving for hosts that don\'t match the regular expresion'}, - 'instance-name':{type:'string',describe:'used to differenciate multiple instances in a zeroconf network'}, - 'fullscreen':{type:'boolean', describe:'launch in fullscreen mode (only affects the default gui, F11 to exit)'} -} - // hack process.cwd to ensure yargs finds package.json var _cwd = process.cwd process.cwd = () => { return __dirname } @@ -111,15 +28,26 @@ argv = argv.argv // restore process.cwd process.cwd = _cwd +// are we in a terminal ? var cli = false for (i in argv) { if (i != '_' && i != '$0' && (argv[i]!=undefined && argv[i]!==false)) cli = true } -var configFile = function(){try {return JSON.parse(fs.readFileSync(configPath,'utf-8'))} catch(err) {return {}}}(), +var fs = require('fs'), + ifaces = require('os').networkInterfaces() + + +var baseDir = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'], + configPath = baseDir ? require('path').join(baseDir, '.open-stage-control') : '', + configFile = configPath ? function(){try {return JSON.parse(fs.readFileSync(configPath,'utf-8'))} catch(err) {return {}}}() : {}, config = JSON.parse(JSON.stringify(configFile)), defaultConfig +if (!configPath) { + console.warn('Home directory not found, settings and session history will not be saved.') +} + var makeDefaultConfig = function(argv){ defaultConfig = { @@ -186,7 +114,7 @@ module.exports = { }, write:function(key,value,tmp) { config[key] = value - if (tmp) return + if (tmp || !configPath) return configFile[key] = value fs.writeFile(configPath,JSON.stringify(configFile,null,4), function(err, data) { if (err) throw err