From 554bdaa113ce17cdb8c88d80d0b9750ba488d734 Mon Sep 17 00:00:00 2001 From: bordun Date: Sun, 6 Sep 2020 11:12:13 +0200 Subject: [PATCH] custom module: rm support for extra args (avoid path issues) --- CHANGELOG.md | 3 +++ .../getting-started/server-configuration.md | 6 +++--- src/server/options.js | 20 ++++-------------- src/server/osc/index.js | 21 ++++++------------- 4 files changed, 16 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d592a66..851868df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## next +- custom module + - remove support for extra args (avoid path issues) + - widgets - knob: add `solid` & `line` designs - fader: add `knobSize` property diff --git a/resources/docs/docs/getting-started/server-configuration.md b/resources/docs/docs/getting-started/server-configuration.md index aa834a31..eec3dfe2 100644 --- a/resources/docs/docs/getting-started/server-configuration.md +++ b/resources/docs/docs/getting-started/server-configuration.md @@ -12,16 +12,16 @@ The **server** can be configured with many options, either from a terminal or wi === "load" - Path to a session file. All clients connecting the server will load it. + Path to a session file (.json). All clients connecting the server will load it. === "state" - Path to a state file. All clients connecting the server will load it. + Path to a state file (.state). All clients connecting the server will load it. === "custom-module" - Path to a custom module file. Additional options can be passed after the path, they'll be accessible in the custom module's context. + Path to a custom module file (.js). === "port" diff --git a/src/server/options.js b/src/server/options.js index 85cd5e63..c667ce4c 100644 --- a/src/server/options.js +++ b/src/server/options.js @@ -7,31 +7,19 @@ module.exports = { 'Targets must be ip:port or hostname:port pairs (port must be >= 1024) or midi:port_name pairs' : true } }, - 'l': {alias: 'load', type: 'string', file: {name: 'OSC Session (.json)', extensions: ['json', 'js']}, describe: 'session file to load', + 'l': {alias: 'load', type: 'string', file: {name: 'OSC Session (.json)', extensions: ['json']}, describe: 'session file to load (.json)', check: (arg)=>{ return fs.existsSync(arg) ? true : 'Session file not found: ' + arg } }, - 'state': {type: 'string', file: {name: 'OSC State (.state)', extensions: ['state']}, describe: 'state file to load', + 'state': {type: 'string', file: {name: 'OSC State (.state)', extensions: ['state']}, describe: 'state file to load (.state)', check: (arg)=>{ return fs.existsSync(arg) ? true : 'State file not found: ' + arg } }, - '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)', + 'c': {alias: 'custom-module', type: 'string', file: {name: 'OSC Custom module (.js)', extensions: ['js']}, describe: 'custom module file to load (.js)', check: (arg)=>{ - - var customModule = arg.slice() - - // consolidate path containing whitespaces - if (!customModule[0].includes('.js')) { - var index = customModule.findIndex(x => typeof x === 'string' && x.includes('.js')) - if (index !== undefined) { - var _path = customModule.slice(0, index + 1).join(' ') - customModule.splice(0, index + 1, _path) - } - } - - return fs.existsSync(customModule[0]) ? true : 'Custom module file not found: ' + customModule[0] + return fs.existsSync(arg) ? true : 'Custom module file not found: ' + arg }, }, 'p': {alias: 'port', type: 'number', describe: 'http port of the server (default to 8080)', diff --git a/src/server/osc/index.js b/src/server/osc/index.js index cd36d31d..88140a71 100644 --- a/src/server/osc/index.js +++ b/src/server/osc/index.js @@ -21,22 +21,14 @@ class OscServer { this.customModule = (()=>{ var customModule = settings.read('custom-module') - if (!customModule) return false - // consolidate path containing whitespaces - if (!customModule[0].includes('.js')) { - var index = customModule.findIndex(x => typeof x === 'string' && x.includes('.js')) - if (index !== undefined) { - var _path = customModule.slice(0, index + 1).join(' ') - customModule.splice(0, index + 1, _path) - } - } + if (!customModule) return false var file = (()=>{ try { - return fs.readFileSync(customModule[0], 'utf8') + return fs.readFileSync(customModule, 'utf8') } catch(err) { - console.error('(ERROR) Custom module not found: ' + customModule[0]) + console.error('(ERROR) Custom module not found: ' + customModule) return false } })(), @@ -66,7 +58,7 @@ class OscServer { loadJSON: (url)=>{ if (url.split('.').pop() === 'json') { try { - url = path.resolve(path.dirname(customModule[0]), url) + url = path.resolve(path.dirname(customModule), url) return JSON.parse(fs.readFileSync(url, 'utf8')) } catch(e) { console.error('(ERROR) could not load json file from ' + url) @@ -78,7 +70,7 @@ class OscServer { }, saveJSON: (url, data)=>{ if (url.split('.').pop() === 'json') { - url = path.resolve(path.dirname(customModule[0]), url) + url = path.resolve(path.dirname(customModule), url) try { return fs.writeFileSync(url, JSON.stringify(data, null, ' ')) } catch(e) { @@ -93,7 +85,6 @@ class OscServer { setTimeout, clearTimeout, setInterval, clearInterval, settings, - options: customModule.slice(1, customModule.length), module: {}, }) @@ -103,7 +94,7 @@ class OscServer { process.mainModule.require = process.dlopen = null // run mod = vm.runInContext(file, context, { - filename: customModule[0] + filename: customModule }) if (context.module.exports) mod = context.module.exports } catch(err) {