const fs = require('fs');

const helpers = require('../helpers');
const settingsFilePath = 'settingsData.json'

function getSettings() {
  if (fs.existsSync(settingsFilePath)) {
    let data = fs.readFileSync(settingsFilePath);
    return JSON.parse(data);
  }
  return {};
}

function update(argv) {
  let settingsData = getSettings();
  let settingsDataUpdate = Object.create(settingsData);

  if (argv.timezone) {
    settingsDataUpdate.timezone = argv.timezone;
  }

  let settingsJSON = JSON.stringify(settingsDataUpdate);
  if (settingsData.timezone !== settingsDataUpdate.timezone) {
    fs.writeFile(settingsFilePath, settingsJSON, (err) => {
      if (err) throw err;
      helpers.printMessage('The file has been saved!');
    });
  }
};

module.exports = {
  getSettings,
  update
};

const fs = require('fs');
const chalk = require('chalk');

const helpers = require('../helpers');
const settingsFilePath = 'settingsData.json';

function getSettings() {
  if (fs.existsSync(settingsFilePath)) {
    let data = fs.readFileSync(settingsFilePath);
    return JSON.parse(data);
  }
  return {};
}

function update(argv) {
  let settingsData = getSettings();
  let settingsDataUpdate = Object.create(settingsData);

  if (argv.timezone) {
    settingsDataUpdate.timezone = argv.timezone;
  }

  let settingsJSON = JSON.stringify(settingsDataUpdate);

  if (settingsData.timezone !== settingsDataUpdate.timezone) {
    return fs.writeFile(settingsFilePath, settingsJSON, (error) => {
      if (error) {
        return helpers.printError(error.message);
      }
      let message = chalk.bgGreen('Success!') + ' ' + 'The file has been saved!';
      helpers.printMessage(message);
    });
  }

  let message = chalk.bgGreen('OK') + ' ' + 'Settings are correct, no changes required.';
  return helpers.printMessage(message);
};

module.exports = {
  getSettings,
  update
};

Add info and error messages
Add info and error messages
