mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-09 17:33:55 +00:00
refactor: initialise data
This commit is contained in:
committed by
Carlos Valente
parent
dae32e89a3
commit
84792bc00d
@@ -2,6 +2,8 @@ export const config = {
|
||||
appState: 'app-state.json',
|
||||
corrupt: 'corrupt files',
|
||||
crash: 'crash logs',
|
||||
demoProject: 'demo project.json',
|
||||
newProject: 'new project.json',
|
||||
database: {
|
||||
testdb: 'test-db',
|
||||
directory: 'db',
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { fileURLToPath } from 'url';
|
||||
import { dirname, join } from 'path';
|
||||
import { readFileSync, writeFileSync } from 'fs';
|
||||
|
||||
import { config } from './config.js';
|
||||
import { ensureDirectory } from '../utils/fileManagement.js';
|
||||
@@ -70,40 +69,21 @@ export const resolvedPath = (): string => {
|
||||
return devPath;
|
||||
};
|
||||
|
||||
const testDbStartDirectory = isTest ? '../' : getAppDataPath();
|
||||
export const externalsStartDirectory = isProduction ? getAppDataPath() : join(srcDirectory, 'external');
|
||||
// resolve public directory
|
||||
export const resolvePublicDirectoy = getAppDataPath();
|
||||
ensureDirectory(resolvePublicDirectoy);
|
||||
|
||||
const testDbStartDirectory = isTest ? '../' : resolvePublicDirectoy;
|
||||
export const externalsStartDirectory = isProduction ? resolvePublicDirectoy : join(srcDirectory, 'external');
|
||||
// TODO: we only need one when they are all in the same folder
|
||||
export const resolveExternalsDirectory = join(isProduction ? getAppDataPath() : srcDirectory, 'external');
|
||||
export const resolveExternalsDirectory = join(isProduction ? resolvePublicDirectoy : srcDirectory, 'external');
|
||||
|
||||
// project files
|
||||
export const appStatePath = join(getAppDataPath(), config.appState);
|
||||
export const uploadsFolderPath = join(getAppDataPath(), config.uploads);
|
||||
|
||||
const ensureAppState = () => {
|
||||
ensureDirectory(getAppDataPath());
|
||||
writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
|
||||
};
|
||||
|
||||
const getLastLoadedProject = () => {
|
||||
try {
|
||||
const appState = JSON.parse(readFileSync(appStatePath, 'utf8'));
|
||||
if (!appState.lastLoadedProject) {
|
||||
ensureAppState();
|
||||
}
|
||||
return appState.lastLoadedProject;
|
||||
} catch {
|
||||
if (!isTest) {
|
||||
ensureAppState();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const lastLoadedProject = isTest ? 'db.json' : getLastLoadedProject();
|
||||
export const appStatePath = join(resolvePublicDirectoy, config.appState);
|
||||
export const uploadsFolderPath = join(resolvePublicDirectoy, config.uploads);
|
||||
|
||||
// path to public db
|
||||
export const resolveDbDirectory = join(testDbStartDirectory, isTest ? `../${config.database.testdb}` : config.projects);
|
||||
export const resolveDbName = lastLoadedProject ? lastLoadedProject : config.database.filename;
|
||||
export const resolveDbPath = join(resolveDbDirectory, resolveDbName);
|
||||
|
||||
export const pathToStartDb = isTest
|
||||
? join(srcDirectory, '..', config.database.testdb, config.database.filename)
|
||||
@@ -125,21 +105,22 @@ export const resolveDemoPath = config.demo.filename.map((file) => {
|
||||
return join(resolveDemoDirectory, file);
|
||||
});
|
||||
|
||||
// path to demo project
|
||||
export const pathToStartDemo = config.demo.filename.map((file) => {
|
||||
return join(srcDirectory, '/external/demo/', file);
|
||||
});
|
||||
|
||||
// path to restore file
|
||||
export const resolveRestoreFile = join(getAppDataPath(), config.restoreFile);
|
||||
export const resolveRestoreFile = join(resolvePublicDirectoy, config.restoreFile);
|
||||
|
||||
// path to sheets folder
|
||||
export const resolveSheetsDirectory = join(getAppDataPath(), config.sheets.directory);
|
||||
export const resolveSheetsDirectory = join(resolvePublicDirectoy, config.sheets.directory);
|
||||
|
||||
// path to crash reports
|
||||
export const resolveCrashReportDirectory = join(getAppDataPath(), config.crash);
|
||||
export const resolveCrashReportDirectory = join(resolvePublicDirectoy, config.crash);
|
||||
|
||||
// path to projects
|
||||
export const resolveProjectsDirectory = join(getAppDataPath(), config.projects);
|
||||
export const resolveProjectsDirectory = join(resolvePublicDirectoy, config.projects);
|
||||
|
||||
// path to corrupt files
|
||||
export const resolveCorruptDirectory = join(getAppDataPath(), config.corrupt);
|
||||
export const resolveCorruptDirectory = join(resolvePublicDirectoy, config.corrupt);
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
import { DatabaseModel } from 'ontime-types';
|
||||
|
||||
import { Low } from 'lowdb';
|
||||
import { JSONFilePreset } from 'lowdb/node';
|
||||
import { copyFileSync, existsSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
|
||||
import { ensureDirectory } from '../utils/fileManagement.js';
|
||||
import { dbModel } from '../models/dataModel.js';
|
||||
|
||||
import { pathToStartDb, resolveDbDirectory, resolveDbName } from './index.js';
|
||||
import { parseProjectFile } from '../services/project-service/projectFileUtils.js';
|
||||
import { parseJson } from '../utils/parser.js';
|
||||
import { getErrorMessage } from 'ontime-utils';
|
||||
import { appStateProvider } from '../services/app-state-service/AppStateService.js';
|
||||
import { consoleError } from '../utils/console.js';
|
||||
|
||||
/**
|
||||
* @description ensures directories exist and populates database
|
||||
* @return {string} - path to db file
|
||||
*/
|
||||
const populateDb = (directory: string, filename: string): string => {
|
||||
ensureDirectory(directory);
|
||||
let dbPath = join(directory, filename);
|
||||
|
||||
// if everything goes well, the DB in disk is the one loaded
|
||||
// if dbInDisk doesn't exist we want to use startup db
|
||||
if (!existsSync(dbPath)) {
|
||||
try {
|
||||
const dbDirectory = resolveDbDirectory;
|
||||
const startDbName = pathToStartDb.split('/').pop();
|
||||
|
||||
if (!startDbName) {
|
||||
throw new Error('Invalid path to start database');
|
||||
}
|
||||
|
||||
const newFileDirectory = join(dbDirectory, startDbName);
|
||||
|
||||
copyFileSync(pathToStartDb, newFileDirectory);
|
||||
dbPath = newFileDirectory;
|
||||
} catch (_) {
|
||||
/* we do not handle this */
|
||||
}
|
||||
}
|
||||
|
||||
return dbPath;
|
||||
};
|
||||
|
||||
/**
|
||||
* @description loads ontime db
|
||||
*/
|
||||
async function loadDb(directory: string, filename: string) {
|
||||
const dbInDisk = populateDb(directory, filename);
|
||||
|
||||
// TODO: should this be passed in somewhere?
|
||||
let newData: DatabaseModel = dbModel;
|
||||
|
||||
try {
|
||||
const maybeProjectFile = await parseProjectFile(dbInDisk);
|
||||
const result = parseJson(maybeProjectFile);
|
||||
|
||||
await appStateProvider.setLastLoadedProject(filename);
|
||||
|
||||
newData = result.data;
|
||||
} catch (error) {
|
||||
consoleError(`Unable to parse project file: ${getErrorMessage(error)}`);
|
||||
// we get here if the JSON file is corrupt
|
||||
}
|
||||
|
||||
const db = await JSONFilePreset<DatabaseModel>(dbInDisk, newData);
|
||||
db.data = newData;
|
||||
|
||||
return { db, data: newData };
|
||||
}
|
||||
|
||||
export let db = {} as Low<DatabaseModel>;
|
||||
export let data = {} as DatabaseModel;
|
||||
export const dbLoadingProcess = loadDb(resolveDbDirectory, resolveDbName);
|
||||
|
||||
/**
|
||||
* Initialises database at known location
|
||||
*/
|
||||
const init = async () => {
|
||||
const dbProvider = await dbLoadingProcess;
|
||||
db = dbProvider.db;
|
||||
data = dbProvider.data;
|
||||
};
|
||||
|
||||
/**
|
||||
* Allows to switch the database to a new file
|
||||
*/
|
||||
export const switchDb = async (filePath: string, initialData: DatabaseModel = dbModel) => {
|
||||
const newDb = await JSONFilePreset<DatabaseModel>(filePath, initialData);
|
||||
|
||||
// Read the database to initialize it
|
||||
await newDb.read();
|
||||
|
||||
db = newDb;
|
||||
data = db.data;
|
||||
};
|
||||
|
||||
init();
|
||||
Reference in New Issue
Block a user