refactor: simplify setup of asset paths

This commit is contained in:
Carlos Valente
2024-10-13 13:53:55 +02:00
committed by Carlos Valente
parent e46948772e
commit 5950551da2
17 changed files with 177 additions and 144 deletions
@@ -5,7 +5,7 @@ import { copyFile, rename } from 'fs/promises';
import { logger } from '../../classes/Logger.js';
import { getNetworkInterfaces } from '../../utils/networkInterfaces.js';
import { resolveCorruptDirectory, resolveProjectsDirectory, resolveStylesPath } from '../../setup/index.js';
import { publicDir, publicFiles } from '../../setup/index.js';
import {
appendToName,
ensureDirectory,
@@ -47,8 +47,8 @@ init();
* Ensure services has its dependencies initialized
*/
function init() {
ensureDirectory(resolveProjectsDirectory);
ensureDirectory(resolveCorruptDirectory);
ensureDirectory(publicDir.projectsDir);
ensureDirectory(publicDir.corruptDir);
}
export async function getCurrentProject() {
@@ -63,7 +63,7 @@ export async function getCurrentProject() {
* to be composed in the loading functions
*/
async function loadDemoProject(): Promise<string> {
const pathToNewFile = generateUniqueFileName(resolveProjectsDirectory, config.demoProject);
const pathToNewFile = generateUniqueFileName(publicDir.projectsDir, config.demoProject);
await initPersistence(getPathToProject(pathToNewFile), demoDb);
const newName = getFileNameFromPath(pathToNewFile);
await setLastLoadedProject(newName);
@@ -75,7 +75,7 @@ async function loadDemoProject(): Promise<string> {
* to be composed in the loading functions
*/
async function loadNewProject(): Promise<string> {
const pathToNewFile = generateUniqueFileName(resolveProjectsDirectory, config.newProject);
const pathToNewFile = generateUniqueFileName(publicDir.projectsDir, config.newProject);
await initPersistence(getPathToProject(pathToNewFile), dbModel);
const newName = getFileNameFromPath(pathToNewFile);
await setLastLoadedProject(newName);
@@ -273,7 +273,7 @@ export async function createProject(filename: string, projectData: ProjectData)
},
};
const uniqueFileName = generateUniqueFileName(resolveProjectsDirectory, filename);
const uniqueFileName = generateUniqueFileName(publicDir.projectsDir, filename);
const newFile = getPathToProject(uniqueFileName);
// change LowDB to point to new file
@@ -316,14 +316,13 @@ export async function getInfo(): Promise<GetInfo> {
// get nif and inject localhost
const ni = getNetworkInterfaces();
ni.unshift({ name: 'localhost', address: '127.0.0.1' });
const cssOverride = resolveStylesPath;
return {
networkInterfaces: ni,
version,
serverPort,
osc,
cssOverride,
cssOverride: publicFiles.cssOverride,
};
}
@@ -4,7 +4,7 @@ import { existsSync } from 'fs';
import { copyFile, readFile, rename, stat } from 'fs/promises';
import { extname, join } from 'path';
import { resolveCorruptDirectory, resolveProjectsDirectory } from '../../setup/index.js';
import { publicDir } from '../../setup/index.js';
import { getFilesFromFolder, removeFileExtension } from '../../utils/fileManagement.js';
/**
@@ -13,7 +13,7 @@ import { getFilesFromFolder, removeFileExtension } from '../../utils/fileManagem
* @param name
*/
export async function handleUploaded(filePath: string, name: string) {
const newFilePath = join(resolveProjectsDirectory, name);
const newFilePath = join(publicDir.projectsDir, name);
await rename(filePath, newFilePath);
}
@@ -29,12 +29,12 @@ export async function handleUploaded(filePath: string, name: string) {
* @throws {Error} Throws an error if there is an issue in reading the directory or fetching file statistics.
*/
export async function getProjectFiles(): Promise<ProjectFile[]> {
const allFiles = await getFilesFromFolder(resolveProjectsDirectory);
const allFiles = await getFilesFromFolder(publicDir.projectsDir);
const filteredFiles = filterProjectFiles(allFiles);
const projectFiles: ProjectFile[] = [];
for (const file of filteredFiles) {
const filePath = join(resolveProjectsDirectory, file);
const filePath = join(publicDir.projectsDir, file);
const stats = await stat(filePath);
projectFiles.push({
@@ -62,14 +62,14 @@ export function doesProjectExist(name: string): MaybeString {
* Returns the absolute path to a project file
*/
export function getPathToProject(name: string): string {
return join(resolveProjectsDirectory, name);
return join(publicDir.projectsDir, name);
}
/**
* Makes a copy of a given project to the corrupted directory
*/
export async function copyCorruptFile(filePath: string, name: string): Promise<void> {
const newPath = join(resolveCorruptDirectory, name);
const newPath = join(publicDir.corruptDir, name);
return copyFile(filePath, newPath);
}
@@ -77,7 +77,7 @@ export async function copyCorruptFile(filePath: string, name: string): Promise<v
* Moves a file permanently to the corrupted directory
*/
export async function moveCorruptFile(filePath: string, name: string): Promise<void> {
const newPath = join(resolveCorruptDirectory, name);
const newPath = join(publicDir.corruptDir, name);
return rename(filePath, newPath);
}