all files / src/ Parser.js

100% Statements 7/7
100% Branches 0/0
100% Functions 2/2
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35                                                       
"use strict";
 
const fs = require("fs");
 
class Parser {
 
  /**
   * Parse a file
   * @param  {String} fileName String file path of file to load
   * @return {Object}          Valid JavaScript object
   */
  parse(fileName) {
    // Make sure that a .npmpackagejsonlintrc is present
    try {
      return this._readFile(fileName);
    } catch (err) {
      throw new Error(`${fileName} does not exist :(`);
    }
  }
 
  /**
   * Sychronously reads file from file system
   * @param  {String} fileName String file path of file to load
   * @return {Object}          Valid JavaScript object
   */
  _readFile(fileName) {
    const file = fs.readFileSync(fileName);
 
    return JSON.parse(file);
  }
 
}
 
module.exports = Parser;