From 81b6ca0557e36201522740866e07bde820e60a7a Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Sun, 5 Mar 2023 01:27:12 +0100 Subject: [PATCH] custom module:don't restrict file extension in `loadJSON()` and `saveJSON` functions --- CHANGELOG.md | 3 +++ src/server/custom-module.js | 36 ++++++++++++++---------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 998c0f0a..25eb5e68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ - scripting - expose file browser (from the file widget) as a scripting function +- custom module + - don't restrict file extension in `loadJSON()` and `saveJSON` functions + - misc - add polish locales (@ Manz4rk) diff --git a/src/server/custom-module.js b/src/server/custom-module.js index c7cdfb0a..20bf6310 100644 --- a/src/server/custom-module.js +++ b/src/server/custom-module.js @@ -34,31 +34,23 @@ class CustomModule { } : { ...context, loadJSON: (url)=>{ - if (url.split('.').pop() === 'json') { - try { - url = path.resolve(path.dirname(this.filename), url) - var content = fs.readFileSync(url, 'utf8') - if (content[0] === '\ufeff') content = content.slice(1) // remove BOM - return JSON.parse(content) - } catch(e) { - console.error('(ERROR) could not load json file from ' + url) - console.error(e.message) - } - } else { - console.error('(ERROR) unauthorized file type for loadJSON') + try { + url = path.resolve(path.dirname(this.filename), url) + var content = fs.readFileSync(url, 'utf8') + if (content[0] === '\ufeff') content = content.slice(1) // remove BOM + return JSON.parse(content) + } catch(e) { + console.error('(ERROR) could not load json file from ' + url) + console.error(e.message) } }, saveJSON: (url, data)=>{ - if (url.split('.').pop() === 'json') { - url = path.resolve(path.dirname(this.filename), url) - try { - return fs.writeFileSync(url, JSON.stringify(data, null, ' ')) - } catch(e) { - console.error('(ERROR) could not save json file to ' + url) - console.error(e.message) - } - } else { - console.error('(ERROR) unauthorized file type for saveJSON') + url = path.resolve(path.dirname(this.filename), url) + try { + return fs.writeFileSync(url, JSON.stringify(data, null, ' ')) + } catch(e) { + console.error('(ERROR) could not save json file to ' + url) + console.error(e.message) } }, console: console,