refactor: rename project

This commit is contained in:
Carlos Valente
2024-06-27 22:36:51 +02:00
committed by Carlos Valente
parent c9ef8d55c1
commit 46195fc043
8 changed files with 58 additions and 64 deletions
@@ -46,7 +46,7 @@ class AppState {
return data.lastLoadedProject;
}
async updateDatabaseConfig(filename: string): Promise<void> {
async setLastLoadedProject(filename: string): Promise<void> {
if (isTest) return;
if (!this.didInit) {
@@ -14,7 +14,6 @@ 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 { parseJson } from '../../utils/parser.js';
import { generateUniqueFileName } from '../../utils/generateUniqueFilename.js';
// init dependencies
@@ -38,16 +37,14 @@ export async function applyProjectFile(name: string, options?: Options) {
const filePath = getPathToProject(name);
const data = parseProjectFile(filePath);
const result = parseJson(data);
// change LowDB to point to new file
await switchDb(filePath, result.data);
await switchDb(filePath);
// apply data model
await applyDataModel(data, options);
// persist the project selection
await appStateProvider.updateDatabaseConfig(name);
await appStateProvider.setLastLoadedProject(name);
}
/**
@@ -66,17 +63,17 @@ export async function getProjectList(): Promise<ProjectFileListResponse> {
/**
* Duplicates an existing project file
*/
export async function duplicateProjectFile(originalFile: string, newFileName: string) {
export async function duplicateProjectFile(originalFile: string, newFilename: string) {
if (!doesProjectExist(originalFile)) {
throw new Error('Project file not found');
}
if (doesProjectExist(newFileName)) {
throw new Error(`Project file with name ${newFileName} already exists`);
if (doesProjectExist(newFilename)) {
throw new Error(`Project file with name ${newFilename} already exists`);
}
const projectFilePath = getPathToProject(originalFile);
const duplicateProjectFilePath = getPathToProject(newFileName);
const duplicateProjectFilePath = getPathToProject(newFilename);
return copyFile(projectFilePath, duplicateProjectFilePath);
}
@@ -84,17 +81,24 @@ export async function duplicateProjectFile(originalFile: string, newFileName: st
/**
* Renames an existing project file
*/
export async function renameProjectFile(existingProjectFile: string, newName: string) {
const projectFilePath = getPathToProject(existingProjectFile);
const newProjectFilePath = getPathToProject(newName);
export async function renameProjectFile(originalFile: string, newFilename: string) {
if (!doesProjectExist(originalFile)) {
throw new Error('Project file not found');
}
if (doesProjectExist(newFilename)) {
throw new Error(`Project file with name ${newFilename} already exists`);
}
const projectFilePath = getPathToProject(originalFile);
const newProjectFilePath = getPathToProject(newFilename);
await rename(projectFilePath, newProjectFilePath);
// Update the last loaded project config if current loaded project is the one being renamed
const lastLoadedProject = await appStateProvider.getLastLoadedProject();
if (lastLoadedProject === existingProjectFile) {
await appStateProvider.updateDatabaseConfig(newName);
const isLoaded = await appStateProvider.isLastLoadedProject(originalFile);
if (isLoaded) {
await applyProjectFile(newFilename);
}
}
@@ -121,7 +125,7 @@ export async function createProject(filename: string, projectData: ProjectData)
await applyDataModel(data);
// update app state to point to new value
appStateProvider.updateDatabaseConfig(uniqueFileName);
appStateProvider.setLastLoadedProject(uniqueFileName);
return uniqueFileName;
}
@@ -1,4 +1,4 @@
import { deleteProjectFile, duplicateProjectFile } from '../ProjectService.js';
import { deleteProjectFile, duplicateProjectFile, renameProjectFile } from '../ProjectService.js';
import { appStateProvider } from '../../app-state-service/AppStateService.js';
import { doesProjectExist } from '../projectServiceUtils.js';
import { Mock } from 'vitest';
@@ -54,3 +54,20 @@ describe('duplicateProjectFile', () => {
);
});
});
describe('renameProjectFile', () => {
it('throws an error if origin project does not exist', async () => {
(doesProjectExist as Mock).mockReturnValue(false);
await expect(renameProjectFile('does not exist', 'doesnt matter')).rejects.toThrow('Project file not found');
});
it('throws an error if new file name is already a project', async () => {
// current project exists
(doesProjectExist as Mock).mockReturnValueOnce(true);
// new project exists
(doesProjectExist as Mock).mockReturnValueOnce(true);
expect(renameProjectFile('nonexistentProject', 'existingproject')).rejects.toThrow(
'Project file with name existingproject already exists',
);
});
});
@@ -59,35 +59,6 @@ export function doesProjectExist(name: string): boolean {
return existsSync(projectFilePath);
}
/**
* @description Validates the existence of project files.
* @param {object} projectFiles
* @param {string} projectFiles.projectFilename
* @param {string} projectFiles.newFilename
*
* @returns {Promise<Array<string>>} Array of errors
*
*/
export const validateProjectFiles = (projectFiles: { filename?: string; newFilename?: string }): Array<string> => {
const errors: string[] = [];
// current project must exist
if (projectFiles.filename) {
if (!doesProjectExist(projectFiles.filename)) {
errors.push('Project file does not exist');
}
}
// new project must NOT exist
if (projectFiles.newFilename) {
if (doesProjectExist(projectFiles.newFilename)) {
errors.push('New project file already exists');
}
}
return errors;
};
/**
* Returns the absolute path to a project file
*/