refactor: load project

This commit is contained in:
Carlos Valente
2024-07-02 16:52:18 +02:00
committed by Carlos Valente
parent 9c5e403b18
commit 751c3329f0
11 changed files with 153 additions and 87 deletions
@@ -1,20 +1,27 @@
import { DatabaseModel, GetInfo, ProjectData, ProjectFileListResponse } from 'ontime-types';
import { DatabaseModel, GetInfo, LogOrigin, ProjectData, ProjectFileListResponse } from 'ontime-types';
import { copyFile, rename } from 'fs/promises';
import { join } from 'path';
import { initRundown } from '../rundown-service/RundownService.js';
import { DataProvider } from '../../classes/data-provider/DataProvider.js';
import { runtimeService } from '../runtime-service/RuntimeService.js';
import { logger } from '../../classes/Logger.js';
import { getNetworkInterfaces } from '../../utils/networkInterfaces.js';
import { resolveProjectsDirectory, resolveStylesPath } from '../../setup/index.js';
import { parseProjectFile } from './projectFileUtils.js';
import { appStateProvider } from '../app-state-service/AppStateService.js';
import { ensureDirectory, removeFileExtension } from '../../utils/fileManagement.js';
import { resolveCorruptDirectory, resolveProjectsDirectory, resolveStylesPath } from '../../setup/index.js';
import { appendToName, ensureDirectory, removeFileExtension } from '../../utils/fileManagement.js';
import { dbModel } from '../../models/dataModel.js';
import { deleteFile } from '../../utils/parserUtils.js';
import { switchDb } from '../../setup/loadDb.js';
import { doesProjectExist, getPathToProject, getProjectFiles } from './projectServiceUtils.js';
import { generateUniqueFileName } from '../../utils/generateUniqueFilename.js';
import { parseJson } from '../../utils/parser.js';
import { initRundown } from '../rundown-service/RundownService.js';
import { appStateProvider } from '../app-state-service/AppStateService.js';
import { runtimeService } from '../runtime-service/RuntimeService.js';
import { oscIntegration } from '../integration-service/OscIntegration.js';
import { httpIntegration } from '../integration-service/HttpIntegration.js';
import { parseProjectFile } from './projectFileUtils.js';
import { doesProjectExist, getPathToProject, getProjectFiles } from './projectServiceUtils.js';
// init dependencies
init();
@@ -26,25 +33,50 @@ function init() {
ensureDirectory(resolveProjectsDirectory);
}
type Options = {
onlyRundown?: 'true' | 'false';
};
/**
* Handles a file from the upload folder and applies its data
* Loads a data from a file into the runtime
*/
export async function applyProjectFile(name: string, options?: Options) {
export async function loadProjectFile(name: string) {
if (!(await doesProjectExist(name))) {
throw new Error('Project file not found');
}
const filePath = getPathToProject(name);
const data = parseProjectFile(filePath);
// when loading a project file, we allow parsing to fail and interrupt the process
const fileData = await parseProjectFile(filePath);
const result = parseJson(fileData);
if (result.errors.length > 0) {
logger.warning(LogOrigin.Server, 'Project loaded with errors');
// move original file to corrupted
ensureDirectory(resolveCorruptDirectory);
copyFile(filePath, join(resolveCorruptDirectory, name));
// rename file to indicate recovery
const newName = appendToName(filePath, '(recovered)');
await rename(filePath, newName);
}
// change LowDB to point to new file
await switchDb(filePath);
// apply data model
await applyDataModel(data, options);
await switchDb(filePath, result.data);
logger.info(LogOrigin.Server, `Loaded project ${name}`);
// persist the project selection
await appStateProvider.setLastLoadedProject(name);
// apply data model
runtimeService.stop();
const { rundown, customFields, osc, http } = result.data;
// apply the rundown
initRundown(rundown, customFields);
// apply integrations
oscIntegration.init(osc);
httpIntegration.init(http);
}
/**
@@ -64,11 +96,11 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
* Duplicates an existing project file
*/
export async function duplicateProjectFile(originalFile: string, newFilename: string) {
if (!doesProjectExist(originalFile)) {
if (!(await doesProjectExist(originalFile))) {
throw new Error('Project file not found');
}
if (doesProjectExist(newFilename)) {
if (await doesProjectExist(newFilename)) {
throw new Error(`Project file with name ${newFilename} already exists`);
}
@@ -82,11 +114,11 @@ export async function duplicateProjectFile(originalFile: string, newFilename: st
* Renames an existing project file
*/
export async function renameProjectFile(originalFile: string, newFilename: string) {
if (!doesProjectExist(originalFile)) {
if (!(await doesProjectExist(originalFile))) {
throw new Error('Project file not found');
}
if (doesProjectExist(newFilename)) {
if (await doesProjectExist(newFilename)) {
throw new Error(`Project file with name ${newFilename} already exists`);
}
@@ -98,7 +130,27 @@ export async function renameProjectFile(originalFile: string, newFilename: strin
// Update the last loaded project config if current loaded project is the one being renamed
const isLoaded = await appStateProvider.isLastLoadedProject(originalFile);
if (isLoaded) {
await applyProjectFile(newFilename);
const fileData = await parseProjectFile(newProjectFilePath);
const result = parseJson(fileData);
// change LowDB to point to new file
await switchDb(newProjectFilePath, result.data);
logger.info(LogOrigin.Server, `Loaded project ${newFilename}`);
// persist the project selection
await appStateProvider.setLastLoadedProject(newFilename);
// apply data model
runtimeService.stop();
const { rundown, customFields, osc, http } = result.data;
// apply the rundown
initRundown(rundown, customFields);
// apply integrations
oscIntegration.init(osc);
httpIntegration.init(http);
}
}
@@ -139,7 +191,7 @@ export async function deleteProjectFile(filename: string) {
throw new Error('Cannot delete currently loaded project');
}
if (!doesProjectExist(filename)) {
if (!(await doesProjectExist(filename))) {
throw new Error('Project file not found');
}
@@ -172,7 +224,7 @@ export async function getInfo(): Promise<GetInfo> {
* applies a partial database model
*/
// TODO: should be private as part of a load
export async function applyDataModel(data: Partial<DatabaseModel>, _options?: Options) {
export async function applyDataModel(data: Partial<DatabaseModel>) {
runtimeService.stop();
// TODO: allow partial project merge from options
@@ -1,6 +1,9 @@
import { readFileSync } from 'fs';
import { readFile } from 'fs/promises';
import { DatabaseModel } from 'ontime-types';
import { extname } from 'path';
// TODO: move to projectServiceUtils
/**
* Given an array of file names, filters out any files that do not have a '.json' extension.
* We assume these are project files
@@ -14,18 +17,18 @@ export function filterProjectFiles(files: Array<string>): Array<string> {
});
}
export function parseProjectFile(filePath: string): object {
export async function parseProjectFile(filePath: string): Promise<Partial<DatabaseModel>> {
if (!filePath.endsWith('.json')) {
throw new Error('Invalid file type');
}
const rawdata = readFileSync(filePath, 'utf-8');
const rawdata = await readFile(filePath, 'utf-8');
const uploadedJson = JSON.parse(rawdata);
// at this point, we think this is a DatabaseModel
// verify by looking for the required fields
if (uploadedJson?.settings?.app !== 'ontime') {
throw new Error('Not a ontime project file');
throw new Error('Not an Ontime project file');
}
return uploadedJson;
}
@@ -1,24 +1,21 @@
import { ProjectFile } from 'ontime-types';
import { stat } from 'fs/promises';
import { existsSync } from 'fs';
import { access, rename, stat } from 'fs/promises';
import { join } from 'path';
import { resolveProjectsDirectory } from '../../setup/index.js';
import { filterProjectFiles } from './projectFileUtils.js';
import { getFilesFromFolder, removeFileExtension } from '../../utils/fileManagement.js';
import { moveUploadedFile } from '../../utils/upload.js';
import { filterProjectFiles } from './projectFileUtils.js';
/**
* Handles the upload of a new project file
* @param filePath
* @param name
* @returns
*/
export async function upload(filePath: string, name: string) {
export async function handleUploaded(filePath: string, name: string) {
const newFilePath = join(resolveProjectsDirectory, name);
await moveUploadedFile(filePath, newFilePath);
return name;
await rename(filePath, newFilePath);
}
/**
@@ -54,9 +51,14 @@ export async function getProjectFiles(): Promise<ProjectFile[]> {
* Checks whether a project of a given name exists
* @param name
*/
export function doesProjectExist(name: string): boolean {
const projectFilePath = join(resolveProjectsDirectory, name);
return existsSync(projectFilePath);
export async function doesProjectExist(name: string): Promise<boolean> {
try {
const projectFilePath = join(resolveProjectsDirectory, name);
await access(projectFilePath);
return true;
} catch (_) {
return false;
}
}
/**