Skip to content

Commit

Permalink
feat: Migration - handler updates from 4.xx to 4.6 and also .shot fil…
Browse files Browse the repository at this point in the history
…es from 3.xx
  • Loading branch information
olzzon committed Mar 24, 2021
1 parent 15e9b79 commit 60da92c
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 59 deletions.
2 changes: 0 additions & 2 deletions server/reducers/settingsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
SET_PAGES_LIST,
TOGGLE_SHOW_CHAN_STRIP_FULL,
} from '../reducers/settingsActions'
import { getVersion } from '../utils/migrations'

export enum PageType {
All,
Expand Down Expand Up @@ -84,7 +83,6 @@ export interface IMixerSettings {

const defaultSettingsReducerState: Array<ISettings> = [
{
sisyfosVersion: getVersion(),
showSettings: false,
showPagesSetup: false,
showChanStrip: -1,
Expand Down
69 changes: 22 additions & 47 deletions server/utils/SettingsStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ import { storeSetCompleteFaderState } from '../reducers/faderActions'
import { logger } from './logger'
import { InumberOfChannels } from '../reducers/channelsReducer'
import { IFaders } from '../reducers/fadersReducer'
import { ICustomPages } from '../reducers/settingsReducer'
import { IChannels } from '../reducers/channelsReducer'

export const loadSettings = (storeRedux: any) => {
let settingsInterface = storeRedux.settings[0]
import { ICustomPages, ISettings } from '../reducers/settingsReducer'

export interface IShotStorage {
channelState: IChannels
faderState: IFaders
}

export const loadSettings = (storeRedux: any): ISettings => {
let newSettings = storeRedux.settings[0]
try {
let newSettings = JSON.parse(
newSettings = JSON.parse(
fs.readFileSync(path.resolve('storage', 'settings.json'))
)
checkVersion(settingsInterface)
checkVersion(newSettings)
return newSettings
} catch (error) {
logger.error('Couldn´t read Settings.json file, creating af new', {})
saveSettings(settingsInterface)
return settingsInterface
logger.error('Couldn´t read Settings.json file, creating af new', error)
saveSettings(newSettings)
return newSettings
}
}

Expand All @@ -45,20 +52,22 @@ export const saveSettings = (settings: any) => {
}

export const loadSnapshotState = (
stateSnapshot: any,
stateChannelSnapshot: any,
stateSnapshot: IFaders,
stateChannelSnapshot: IChannels,
numberOfChannels: InumberOfChannels[],
numberOfFaders: number,
fileName: string,
loadAll: boolean
) => {
try {
const stateFromFile = JSON.parse(fs.readFileSync(fileName))
const stateFromFile: IShotStorage = JSON.parse(
fs.readFileSync(fileName)
)

if (loadAll) {
store.dispatch(
storeSetCompleteChState(
stateFromFile.channelState,
stateFromFile.channelState as IChannels,
numberOfChannels
)
)
Expand All @@ -68,40 +77,6 @@ export const loadSnapshotState = (
numberOfFaders
)
)
} else {
stateChannelSnapshot.channel = stateChannelSnapshot.channel.map(
(channel: any, index: number) => {
if (
stateFromFile.channelState.channel[index]
.assignedFader >= 0
) {
channel.auxLevel =
stateFromFile.channelState.channel[index]
.auxLevel || []
channel.assignedFader =
stateFromFile.channelState.channel[
index
].assignedFader
} else {
channel.assignedFader = -1
}
return channel
}
)

stateSnapshot.fader = stateSnapshot.fader.map(
(fader: any, index: number) => {
fader.monitor =
stateFromFile.faderState.fader[index].monitor || -1
return fader
}
)
store.dispatch(
storeSetCompleteChState(stateChannelSnapshot, numberOfChannels)
)
store.dispatch(
storeSetCompleteFaderState(stateSnapshot, numberOfFaders)
)
}
} catch (error) {
logger.error('Error loading Snapshot' + String(error), {})
Expand All @@ -122,7 +97,7 @@ export const saveSnapshotState = (stateSnapshot: any, fileName: string) => {
})
}

export const getSnapShotList = () => {
export const getSnapShotList = (): string[] => {
const files = fs
.readdirSync(path.resolve('storage'))
.filter((file: string) => {
Expand Down
70 changes: 60 additions & 10 deletions server/utils/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,72 @@
//@ts-ignore
import { version } from '../../package.json'
const fs = require('fs')
const path = require('path')

import { logger } from './logger'
import { ISettings } from '../reducers/settingsReducer'
import { getSnapShotList, IShotStorage, saveSettings } from './SettingsStorage'

export const checkVersion = (currentSettings: ISettings) => {
if (currentSettings.sisyfosVersion < version) {
migrateVersion(currentSettings)
}
}
const version = process.env.npm_package_version

export const getVersion = (): string => {
return version
export const checkVersion = (currentSettings: ISettings): ISettings => {
if (version > (currentSettings.sisyfosVersion || '0')) {
currentSettings = migrateVersion(currentSettings)
}
return currentSettings
}

const migrateVersion = (currentSettings: ISettings) => {
const migrateVersion = (currentSettings: ISettings): ISettings => {
console.log(
'Migrating VERSION from',
currentSettings.sisyfosVersion,
' to ',
version
)
let newSettings = currentSettings
if (version === '4.6.0') {
newSettings = migrate45to46(currentSettings)
}
return newSettings
}

const migrate45to46 = (currentSettings: ISettings): ISettings => {
let files: string[] = getSnapShotList()
files.push('default.shot')

files.forEach((fileName: string) => {
let stateFromShot = JSON.parse(
fs.readFileSync(path.resolve('storage', fileName))
)
// As this is the first implemented migration it also looks .shot files from ealier versions than 4.xx
if (stateFromShot.channelState.chConnection) {
// From Version 4.0
stateFromShot.channelState.chMixerConnection =
stateFromShot.channelState?.chConnection
delete stateFromShot.channelState.chConnection
} else if (stateFromShot.channelState.channel) {
// Version lower than 4.0
stateFromShot.channelState.chMixerConnection = [
{ channel: stateFromShot.channelState.channel },
]
delete stateFromShot.channelState.channel
}
let migratedShot: IShotStorage = stateFromShot
fs.writeFileSync(
path.resolve('storage', fileName),
JSON.stringify(migratedShot),
'utf8',
(error: any) => {
if (error) {
logger.error('Error saving Snapshot' + String(error), {})
} else {
logger.verbose(
'Snapshot ' + fileName + ' Saved to storage folder',
{}
)
}
}
)
})
currentSettings.sisyfosVersion = version
saveSettings(currentSettings)
return currentSettings
}

0 comments on commit 60da92c

Please sign in to comment.