<commit_before>/* eslint-disable camelcase */

module.exports = {
  run
}

const fs = require('fs')
const path = require('path')
const semver = require('semver')

const config = require('../../config')

// Change `state.saved` (which will be saved back to config.json on exit) as
// needed, for example to deal with config.json format changes across versions
function run (state) {
  // Replace '{ version: 1 }' with app version (semver)
  if (!semver.valid(state.saved.version)) {
    state.saved.version = '0.0.0' // Pre-0.7.0 version, so run all migrations
  }

  const version = state.saved.version
  const saved = state.saved

  if (semver.lt(version, '0.7.0')) migrate_0_7_0(saved)
  if (semver.lt(version, '0.7.2')) migrate_0_7_2(saved)
  if (semver.lt(version, '0.11.0')) migrate_0_11_0(saved)
  if (semver.lt(version, '0.12.0')) migrate_0_12_0(saved)
  if (semver.lt(version, '0.14.0')) migrate_0_14_0(saved)
  if (semver.lt(version, '0.17.0')) migrate_0_17_0(saved)
  if (semver.lt(version, '0.17.2')) migrate_0_17_2(saved)
  if (semver.lt(version, '0.21.0')) migrate_0_21_0(saved)
  if (semver.lt(version, '0.22.0')) migrate_0_22_0(saved)

  // Config is now on the new version
  state.saved.version = config.APP_VERSION
}

function migrate_0_7_0 (saved) {
  const { copyFileSync } = require('fs')
  const path = require('path')

  saved.torrents.forEach(function (ts) {
    const infoHash = ts.infoHash

    // Replace torrentPath with torrentFileName
    // There are a number of cases to handle here:
    // * Originally we used absolute paths
    // * Then, relative paths for the default torrents, eg '../static/sintel.torrent'<commit_msg>Ensure that default file/protocol handlers are re-installed after updating. (#997)<commit_after>/* eslint-disable camelcase */

module.exports = {
  run
}

const fs = require('fs')
const path = require('path')
const semver = require('semver')

const config = require('../../config')

// Change `state.saved` (which will be saved back to config.json on exit) as
// needed, for example to deal with config.json format changes across versions
function run (state) {
  // Replace '{ version: 1 }' with app version (semver)
  if (!semver.valid(state.saved.version)) {
    state.saved.version = '0.0.0' // Pre-0.7.0 version, so run all migrations
  }

  const version = state.saved.version
  const saved = state.saved

  if (semver.lt(version, '0.7.0')) migrate_0_7_0(saved)
  if (semver.lt(version, '0.7.2')) migrate_0_7_2(saved)
  if (semver.lt(version, '0.11.0')) migrate_0_11_0(saved)
  if (semver.lt(version, '0.12.0')) migrate_0_12_0(saved)
  if (semver.lt(version, '0.14.0')) migrate_0_14_0(saved)
  if (semver.lt(version, '0.17.0')) migrate_0_17_0(saved)
  if (semver.lt(version, '0.17.2')) migrate_0_17_2(saved)
  if (semver.lt(version, '0.21.0')) migrate_0_21_0(saved)
  if (semver.lt(version, '0.22.0')) migrate_0_22_0(saved)

  if (semver.lt(version, config.APP_VERSION)) {
    installHandlers(state.saved)
  }

  // Config is now on the new version
  state.saved.version = config.APP_VERSION
}

// Whenever the app is updated,  re-install default handlers if the user has
// enabled them.
function installHandlers (saved) {
  if (saved.prefs.isFileHandler) {
    const ipcRenderer = require('electron').ipcRenderer
    ipcRenderer.send('setDefaultFileHandler', true)
  }
}

function migrate_0_7_0 (saved) {
  const { copyFileSync } = require('fs')
  const path = require('path')

  saved.torrents.forEach(function (ts) {
    const infoHash = ts.infoHash

    // Replace torrentPath with torrentFileName
    // There are a number of cases to handle here:
    // * Originally we used absolute paths
    // * Then, relative paths for the default torrents, eg '../static/sintel.torrent'
{'fraction_non_alphanumeric': 0.1450320512820513, 'fraction_numerical': 0.04246794871794872, 'mean_word_length': 3.8510362694300517, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6414234', 'n_tokens_mistral': 1386, 'n_tokens_neox': 1305, 'n_words': 340}