<commit_before>// System Objects
var path = require('path');

// Third Party Dependencies
var fs = require('fs-extra');

// Internal
var logs = require('./logs');


var home = (process.platform.startsWith('win')) ? process.env.HOMEPATH : process.env.HOME;
var preferencesJson = path.join(home, '.tessel', 'preferences.json');
var Preferences = {};

Preferences.read = function(key, defaultValue) {
  return Preferences.load().then(contents => {
      if (contents) {
        return contents[key] || defaultValue;
      } else {
        return defaultValue;
      }
    })
    .catch(error => {
      logs.err('Error reading preference', key, error);
      return defaultValue;
    });
};

Preferences.write = function(key, value) {
  return new Promise((resolve, reject) => {
    Preferences.load()
      .then(contents => {
        contents = contents || {};
        contents[key] = value;
        fs.writeFile(preferencesJson, JSON.stringify(contents), function(error) {
          if (error) {
            logs.err('Error writing preference', key, value);
            reject(error);
          } else {
            resolve();
          }
        });
      })
      .catch(error => {
        reject(error);
      });
  });
};

Preferences.load = function() {
  return new Promise((resolve, reject) => {
    fs.exists(preferencesJson, (exists) => {
      if (exists) {
        fs.readFile(preferencesJson, (error, data) => {
          if (error) {
            reject(error);
          } else {
            resolve(JSON.parse(data));
          }
        });
      } else {
        // we don't have any local preferences
        // return falsy value
        resolve();
      }
    });
  });
};

module.exports = Preferences;
<commit_msg>Use os.homedir() to resolve the user's home directory (#751)<commit_after>// System Objects
var os = require('os');
var path = require('path');

// Third Party Dependencies
var fs = require('fs-extra');

// Internal
var logs = require('./logs');

var preferencesJson = path.join(os.homedir(), '.tessel', 'preferences.json');
var Preferences = {};

Preferences.read = function(key, defaultValue) {
  return Preferences.load().then(contents => {
      if (contents) {
        return contents[key] || defaultValue;
      } else {
        return defaultValue;
      }
    })
    .catch(error => {
      logs.err('Error reading preference', key, error);
      return defaultValue;
    });
};

Preferences.write = function(key, value) {
  return new Promise((resolve, reject) => {
    Preferences.load()
      .then(contents => {
        contents = contents || {};
        contents[key] = value;
        fs.writeFile(preferencesJson, JSON.stringify(contents), function(error) {
          if (error) {
            logs.err('Error writing preference', key, value);
            reject(error);
          } else {
            resolve();
          }
        });
      })
      .catch(error => {
        reject(error);
      });
  });
};

Preferences.load = function() {
  return new Promise((resolve, reject) => {
    fs.exists(preferencesJson, (exists) => {
      if (exists) {
        fs.readFile(preferencesJson, (error, data) => {
          if (error) {
            reject(error);
          } else {
            resolve(JSON.parse(data));
          }
        });
      } else {
        // we don't have any local preferences
        // return falsy value
        resolve();
      }
    });
  });
};

module.exports = Preferences;

{'fraction_non_alphanumeric': 0.15273358403239803, 'fraction_numerical': 0.0008678044547295343, 'mean_word_length': 2.1608775137111516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1259045', 'n_tokens_mistral': 986, 'n_tokens_neox': 972, 'n_words': 222}