json-reader.js 502 B

123456789101112131415161718192021
  1. const _fs = require('fs-magic');
  2. // parse json file and allow single line comments
  3. async function readJSONFile(filename){
  4. // load file
  5. let raw = await _fs.readFile(filename, 'utf8');
  6. // strip single line js comments
  7. raw = raw.replace(/^\s*\/\/.*$/gm, '');
  8. // parse text
  9. try {
  10. return JSON.parse(raw);
  11. } catch(e) {
  12. console.log("Error parsing JSON file: "+filename+"\n");
  13. throw e;
  14. }
  15. //return JSON.parse(raw);
  16. }
  17. module.exports = readJSONFile;