custom module:don't restrict file extension in loadJSON() and saveJSON functions
This commit is contained in:
parent
f5b615566a
commit
81b6ca0557
2 changed files with 17 additions and 22 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue