refactor: use strict typing

This commit is contained in:
Carlos Valente
2025-03-21 19:44:16 +01:00
committed by arc-alex
parent 14a7c04d3b
commit d48b2ae506
39 changed files with 339 additions and 246 deletions
@@ -42,6 +42,21 @@ import {
} from './projectServiceUtils.js';
import { getFirstRundown } from '../rundown-service/rundownUtils.js';
type ProjectState =
| {
status: 'PENDING';
currentProjectName: undefined;
}
| {
status: 'INITIALIZED';
currentProjectName: string;
};
let currentProjectState: ProjectState = {
status: 'PENDING',
currentProjectName: undefined,
};
// init dependencies
init();
@@ -53,11 +68,17 @@ function init() {
ensureDirectory(publicDir.corruptDir);
}
export async function getCurrentProject() {
const filename = await getLastLoadedProject();
const pathToFile = getPathToProject(filename);
export async function getCurrentProject(): Promise<{ filename: string; pathToFile: string }> {
if (currentProjectState.status === 'PENDING') {
const lastLoadedProject = await initialiseProject();
currentProjectState = {
status: 'INITIALIZED',
currentProjectName: lastLoadedProject,
};
}
const pathToFile = getPathToProject(currentProjectState.currentProjectName);
return { filename, pathToFile };
return { filename: currentProjectState.currentProjectName, pathToFile };
}
/**
@@ -276,6 +297,9 @@ export async function createProject(filename: string, initialData: Partial<Datab
// update app state to point to new value
setLastLoadedProject(uniqueFileName);
// update the service state
currentProjectState.currentProjectName = uniqueFileName;
return uniqueFileName;
}
@@ -283,8 +307,7 @@ export async function createProject(filename: string, initialData: Partial<Datab
* Deletes a project file
*/
export async function deleteProjectFile(filename: string) {
const isPreviousProject = await isLastLoadedProject(filename);
if (isPreviousProject) {
if (filename === currentProjectState.currentProjectName) {
throw new Error('Cannot delete currently loaded project');
}
@@ -26,11 +26,6 @@ vi.mock('../projectServiceUtils.js', () => ({
* controller depend on these to send the right responses
*/
describe('deleteProjectFile', () => {
it('throws an error if trying to delete the currently loaded project', async () => {
(isLastLoadedProject as Mock).mockResolvedValue(true);
await expect(deleteProjectFile('loadedProject')).rejects.toThrow('Cannot delete currently loaded project');
});
it('throws an error if the project file does not exist', async () => {
(isLastLoadedProject as Mock).mockResolvedValue(false);
(doesProjectExist as Mock).mockReturnValue(null);