all files / file-handling/ write-report.js

91.67% Statements 11/12
75% Branches 3/4
100% Functions 1/1
100% Lines 11/11
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                                        
const fs = require('fs');
 
function report() {
  /**
   * Checks if report file exists. If not then we create it.
   * After check and creation we generate a headline with current date.
   * @param {string} pathToReport
   */
  this.initialReport = (pathToReport) => {
    const date = new Date();
    if (fs.existsSync(pathToReport)) {
      fs.appendFileSync(pathToReport, `#${date}\n`, 'UTF-8', { flags: 'a+' });
    } else {
      fs.writeFile(pathToReport, `#${date}\n`, (err) => {
        Iif (err) throw err;
      });
    }
  };
 
  /**
   * Appends text to the existing report.
   * @param {string} text
   * @param {string} pathToReport
   */
  this.writeToReport = (text, pathToReport) => {
    fs.appendFileSync(pathToReport, `${text}\n`, 'UTF-8', { flags: 'a+' });
  };
}
 
module.exports = report;