refactor: improve project loading

This commit is contained in:
Carlos Valente
2025-03-29 21:57:30 +01:00
committed by arc-alex
parent fc4c1bf22e
commit 5c6b06a0d4
17 changed files with 137 additions and 173 deletions
@@ -146,7 +146,6 @@ describe('test aliases import', () => {
const testData = {
rundown: [],
settings: {
app: 'ontime',
version: '2.0.0',
},
urlPresets: [
@@ -171,7 +170,6 @@ describe('test views import', () => {
const testData = {
rundown: [],
settings: {
app: 'ontime',
version: '2.0.0',
},
viewSettings: {
@@ -205,7 +203,6 @@ describe('test views import', () => {
const testData = {
rundown: [],
settings: {
app: 'ontime',
version: '2.0.0',
},
} as unknown as DatabaseModel;
@@ -249,11 +249,10 @@ describe('parseSettings()', () => {
expect(() => parseSettings({})).toThrow();
});
it('returns an a base model as long as we have the app and version', () => {
const result = parseSettings({ settings: { app: 'ontime', version: '1' } as Settings });
it('returns an a base model as long as we have the app version', () => {
const result = parseSettings({ settings: { version: '1' } as Settings });
expect(result).toBeTypeOf('object');
expect(result).toMatchObject({
app: 'ontime',
version: expect.any(String),
serverPort: 4001,
editorKey: null,
+6 -3
View File
@@ -24,6 +24,7 @@ import { createEvent, type ErrorEmitter } from './parser.js';
/**
* Parse a rundowns object along with the project custom fields
* Returns a default rundown if none exists
*/
export function parseRundowns(
data: Partial<DatabaseModel>,
@@ -32,6 +33,8 @@ export function parseRundowns(
// check custom fields first
const parsedCustomFields = parseCustomFields(data, emitError);
// ensure there is always a rundown to import
// this is important since the rest of the app assumes this exist
if (!data.rundowns || isObjectEmpty(data.rundowns)) {
emitError?.('No data found to import');
return {
@@ -194,14 +197,14 @@ export function parseProject(data: Partial<DatabaseModel>, emitError?: ErrorEmit
*/
export function parseSettings(data: Partial<DatabaseModel>): Settings {
// skip if file definition is missing
if (!data.settings || data.settings?.app !== 'ontime' || data.settings?.version == null) {
throw new Error('ERROR: unable to parse settings, missing app or version');
// TODO: skip parsing if the version is not correct
if (!data.settings || data.settings?.version == null) {
throw new Error('ERROR: unable to parse settings, missing or incorrect version');
}
console.log('Found settings, importing...');
return {
app: dbModel.settings.app,
version: dbModel.settings.version,
serverPort: data.settings.serverPort ?? dbModel.settings.serverPort,
editorKey: data.settings.editorKey ?? null,