Skip to content

Commit

Permalink
Migrate settings.ini from utf16 to utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
timoschwarzer committed Nov 24, 2023
1 parent 0d6aa3e commit 81facac
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion electron/src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export function getWindow() {
}

async function createWindow() {
await SettingsService.runPreSettingsMigrations()
await SettingsService.makeSureSettingsFileExists()
await SettingsService.migrateSettingsVersion()
await SettingsService.runSettingsMigrations()

registerUIIpcApi()

Expand Down
29 changes: 27 additions & 2 deletions electron/src/lib/SettingsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,32 @@ export class SettingsService {
return events
}

static async migrateSettingsVersion() {
/**
* Runs migrations before having read the settings
* @returns {Promise<void>}
*/
static async runPreSettingsMigrations() {
if (!fs.existsSync(SETTINGS_PATH)) {
return
}

const currentVersion = (await updater.getVersion()).trim()

if (fs.existsSync(LAST_VERSION_FILE) && currentVersion !== 'develop') {
const lastVersion = (await fs.promises.readFile(LAST_VERSION_FILE, { encoding: 'utf-8' })).trim()

if (semver.lt(lastVersion, '3.0.0') && semver.gte(currentVersion, '3.0.0')) { // settings.ini encoding changed from utf16le to utf-8 in 3.0.0
const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf16le' });
await fs.promises.writeFile(SETTINGS_PATH, settingsIniContent, { encoding: 'utf-8' });
}
}
}

/**
* Run migrations after having read the settings
* @returns {Promise<void>}
*/
static async runSettingsMigrations() {
const currentVersion = (await updater.getVersion()).trim()

if (fs.existsSync(LAST_VERSION_FILE) && currentVersion !== 'develop') {
Expand Down Expand Up @@ -175,7 +200,7 @@ export class SettingsService {
let settingsObject = getDefaultSettings()

if (fs.existsSync(SETTINGS_PATH)) {
const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf16le' });
const settingsIniContent = await fs.promises.readFile(SETTINGS_PATH, { encoding: 'utf-8' });
settingsObject = merge(settingsObject, ini.parse(settingsIniContent.trimStart()))
} else {
console.log("Settings file not found. Using default settings.");
Expand Down

0 comments on commit 81facac

Please sign in to comment.