json-reader.js 460 B

123456789101112131415161718
  1. const _fs = require('fs-magic');
  2. // parse json file and allow single line comments
  3. module.exports = 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. // try to parse json
  9. try{
  10. return JSON.parse(raw);
  11. }catch(e){
  12. console.log(`Error parsing JSON file: [${filename}]`);
  13. throw e;
  14. }
  15. }