refactor: create project

This commit is contained in:
Carlos Valente
2024-06-24 15:55:03 +02:00
committed by Carlos Valente
parent ad69c0ff80
commit 21454947e0
3 changed files with 31 additions and 32 deletions
@@ -14,6 +14,8 @@ import { dbModel } from '../../models/dataModel.js';
import { deleteFile } from '../../utils/parserUtils.js';
import { switchDb } from '../../setup/loadDb.js';
import { getPathToProject, getProjectFiles } from './projectServiceUtils.js';
import { parseJson } from '../../utils/parser.js';
import { generateUniqueFileName } from '../../utils/generateUniqueFilename.js';
// init dependencies
init();
@@ -36,8 +38,10 @@ 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(name);
await switchDb(filePath, result.data);
// apply data model
await applyDataModel(data, options);
@@ -89,8 +93,8 @@ export async function renameProjectFile(existingProjectFile: string, newName: st
/**
* Creates a new project file and applies its result
*/
export async function createProjectFile(filename: string, projectData: ProjectData) {
const data = {
export async function createProject(filename: string, projectData: ProjectData) {
const data: DatabaseModel = {
...dbModel,
project: {
...dbModel.project,
@@ -98,18 +102,20 @@ export async function createProjectFile(filename: string, projectData: ProjectDa
},
};
// create new file
await writeFile(newFile, JSON.stringify(data));
const newFile = getPathToProject(filename);
const uniqueFileName = generateUniqueFileName(resolveProjectsDirectory, filename);
const newFile = getPathToProject(uniqueFileName);
// change LowDB to point to new file
await switchDb(filename);
await switchDb(newFile, data);
// apply its data
// apply data to running services
// we dont need to parse since we are creating a new file
await applyDataModel(data);
// update app state to point to new value
appStateProvider.updateDatabaseConfig(filename);
appStateProvider.updateDatabaseConfig(uniqueFileName);
return uniqueFileName;
}
/**
@@ -144,6 +150,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) {
runtimeService.stop();