custom module: add optional errorCallback argument toloadJSON and saveJSON

This commit is contained in:
jean-emmanuel 2023-04-05 22:57:42 +02:00
parent 3d36f3e4d7
commit b480889b70
3 changed files with 27 additions and 8 deletions

View file

@ -33,24 +33,32 @@ class CustomModule {
require: this.require.bind(this),
} : {
...context,
loadJSON: (url)=>{
loadJSON: (url, errorCallback)=>{
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)
if (typeof(errorCallback) === 'function') {
errorCallback(e)
} else {
console.error('(ERROR) could not load json file from ' + url)
console.error(e.message)
}
}
},
saveJSON: (url, data)=>{
saveJSON: (url, data, errorCallback)=>{
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)
if (typeof(errorCallback) === 'function') {
errorCallback(e)
} else {
console.error('(ERROR) could not save json file to ' + url)
console.error(e.message)
}
}
},
console: console,