<commit_before>var fs = require('fs')
var tar = require('tar-fs')
var pump = require('pump')
var mkdirp = require('mkdirp')
var path = require('path')
var app = require('electron').app
var userDataPath = app.getPath('userData')
var defaultPath = path.join(userDataPath, 'presets', 'default')
var cssPath = path.join(defaultPath, 'style.css')
var iconsPath = path.join(defaultPath, 'icons.svg')

var SETTINGS_FILES = [
  'presets.json',
  'style.css',
  'imagery.json',
  'icons.svg',
  'metadata.json'
]

var METADATA_DEFAULTS = {
  dataset_id: 'mapeodata'
}

function readJsonSync (filepath) {
  try {
    var data = fs.readFileSync(filepath, 'utf8')
    return JSON.parse(data)
  } catch (e) {
    return null
  }
}

function readFile (filepath) {
  try {
    return fs.readFileSync(filepath, 'utf8')
  } catch (e) {
    return null
  }
}

function importSettings (win, settingsFile, cb) {
  var source = fs.createReadStream(settingsFile)
  mkdirp.sync(defaultPath)
  var dest = tar.extract(defaultPath, {
    ignore: function (name) {
      return SETTINGS_FILES.indexOf(path.basename(name)) < 0
    }
  })
  pump(source, dest, function (err) {
    if (err) return cb(err)
    win.webContents.send('force-refresh-window')
    cb()
  })
}

function getSettings (type) {
  switch (type) {
    case 'css':
      return readFile(cssPath)
    case 'icons':
      return readFile(iconsPath)
    case 'presets':
    case 'imagery':
      return readJsonSync(path.join(defaultPath, type + '.json'))
    case 'metadata':
      var data = readJsonSync(path.join(defaultPath, type + '.json'))
      return Object.assign(METADATA_DEFAULTS, data)
    default:
      return null
  }
}

module.exports = {
  importSettings: importSettings,
  getSettings: getSettings
}
<commit_msg>fix: import all settings files fixes #271 (#272)<commit_after>var fs = require('fs')
var tar = require('tar-fs')
var pump = require('pump')
var mkdirp = require('mkdirp')
var path = require('path')
var app = require('electron').app
var userDataPath = app.getPath('userData')
var defaultPath = path.join(userDataPath, 'presets', 'default')
var cssPath = path.join(defaultPath, 'style.css')
var iconsPath = path.join(defaultPath, 'icons.svg')

var METADATA_DEFAULTS = {
  dataset_id: 'mapeodata'
}

function readJsonSync (filepath) {
  try {
    var data = fs.readFileSync(filepath, 'utf8')
    return JSON.parse(data)
  } catch (e) {
    return null
  }
}

function readFile (filepath) {
  try {
    return fs.readFileSync(filepath, 'utf8')
  } catch (e) {
    return null
  }
}

function importSettings (win, settingsFile, cb) {
  var source = fs.createReadStream(settingsFile)
  mkdirp.sync(defaultPath)
  var dest = tar.extract(defaultPath)
  pump(source, dest, function (err) {
    if (err) return cb(err)
    win.webContents.send('force-refresh-window')
    cb()
  })
}

function getSettings (type) {
  switch (type) {
    case 'css':
      return readFile(cssPath)
    case 'icons':
      return readFile(iconsPath)
    case 'presets':
    case 'imagery':
      return readJsonSync(path.join(defaultPath, type + '.json'))
    case 'metadata':
      var data = readJsonSync(path.join(defaultPath, type + '.json'))
      return Object.assign(METADATA_DEFAULTS, data)
    default:
      return null
  }
}

module.exports = {
  importSettings: importSettings,
  getSettings: getSettings
}

{'fraction_non_alphanumeric': 0.13635010419767787, 'fraction_numerical': 0.003274784161952962, 'mean_word_length': 3.779516358463727, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '3184489', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1140, 'n_words': 268}