refactor: migrate project upload (#796)

* refactor: migrate project upload
This commit is contained in:
Carlos Valente
2024-03-01 22:59:00 +01:00
committed by GitHub
parent 8df419d835
commit 5b01329c37
54 changed files with 698 additions and 1134 deletions
+22
View File
@@ -0,0 +1,22 @@
export const config = {
appState: 'app-state.json',
database: {
testdb: 'test-db',
directory: 'db',
filename: 'db.json',
},
demo: {
directory: 'demo',
filename: ['app.js', 'index.html', 'styles.css'],
},
projects: 'projects',
sheets: {
directory: 'sheets',
},
restoreFile: 'ontime.restore',
styles: {
directory: 'styles',
filename: 'override.css',
},
uploads: 'uploads',
};
+137
View File
@@ -0,0 +1,137 @@
import { fileURLToPath } from 'url';
import path, { dirname, join } from 'path';
import fs from 'fs';
import { config } from './config.js';
import { ensureDirectory } from '../utils/fileManagement.js';
// =================================================
// resolve public path
/**
* @description Returns public path depending on OS
* This is the correct path for the app running in production mode
*/
export function getAppDataPath(): string {
// handle docker
if (process.env.ONTIME_DATA) {
return path.join(process.env.ONTIME_DATA);
}
switch (process.platform) {
case 'darwin': {
return path.join(process.env.HOME!, 'Library', 'Application Support', 'Ontime');
}
case 'win32': {
return path.join(process.env.APPDATA!, 'Ontime');
}
case 'linux': {
return path.join(process.env.HOME!, '.Ontime');
}
default: {
throw new Error('Could not resolve public folder for platform');
}
}
}
// =================================================
// resolve running environment
const env = process.env.NODE_ENV || 'production';
export const isTest = Boolean(process.env.IS_TEST);
export const environment = isTest ? 'test' : env;
export const isDocker = env === 'docker';
export const isProduction = isDocker || (env === 'production' && !isTest);
// =================================================
// Resolve directory paths
// resolve file URL in both CJS and ESM (build and dev)
if (import.meta.url) {
globalThis.__dirname = fileURLToPath(import.meta.url);
}
// path to server src folder
export const srcDirectory = path.join(dirname(__dirname), '../');
// resolve path to external
const productionPath = path.join(srcDirectory, '../../resources/extraResources/client');
const devPath = path.join(srcDirectory, '../../client/build/');
const dockerPath = path.join(srcDirectory, 'client/');
export const resolvedPath = (): string => {
if (isTest) {
return devPath;
}
if (isDocker) {
return dockerPath;
}
if (isProduction) {
return productionPath;
}
return devPath;
};
const testDbStartDirectory = isTest ? '../' : getAppDataPath();
export const externalsStartDirectory = isProduction ? getAppDataPath() : join(srcDirectory, 'external');
// TODO: we only need one when they are all in the same folder
export const resolveExternalsDirectory = join(isProduction ? getAppDataPath() : srcDirectory, 'external');
// project files
export const appStatePath = join(getAppDataPath(), config.appState);
export const uploadsFolderPath = join(getAppDataPath(), config.uploads);
const getLastLoadedProject = () => {
try {
const appState = JSON.parse(fs.readFileSync(appStatePath, 'utf8'));
return appState.lastLoadedProject;
} catch {
if (!isTest) {
ensureDirectory(getAppDataPath());
fs.writeFileSync(appStatePath, JSON.stringify({ lastLoadedProject: 'db.json' }));
}
}
};
const lastLoadedProject = isTest ? 'db.json' : getLastLoadedProject();
// path to public db
export const resolveDbDirectory = join(testDbStartDirectory, isTest ? `../${config.database.testdb}` : config.projects);
export const resolveDbPath = join(resolveDbDirectory, lastLoadedProject ? lastLoadedProject : config.database.filename);
export const pathToStartDb = isTest
? join(srcDirectory, '..', config.database.testdb, config.database.filename)
: join(srcDirectory, '/preloaded-db/', config.database.filename);
// TODO: move all static files to the external directory
// path to public styles
export const resolveStylesDirectory = join(externalsStartDirectory, config.styles.directory);
export const resolveStylesPath = join(resolveStylesDirectory, config.styles.filename);
export const pathToStartStyles = join(srcDirectory, '/external/styles/', config.styles.filename);
// path to public demo
export const resolveDemoDirectory = join(
externalsStartDirectory,
isProduction ? '/external/' : '', // move to external folder in production
config.demo.directory,
);
export const resolveDemoPath = config.demo.filename.map((file) => {
return join(resolveDemoDirectory, file);
});
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);
// path to sheets folder
export const resolveSheetsDirectory = join(getAppDataPath(), config.sheets.directory);
// path to crash reports
export const resolveCrashReportDirectory = getAppDataPath();
// path to projects
export const resolveProjectsDirectory = join(getAppDataPath(), config.projects);
+83
View File
@@ -0,0 +1,83 @@
import { DatabaseModel } from 'ontime-types';
import { Low } from 'lowdb';
import { JSONFile } 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, resolveDbPath } from './index.js';
import { parseProjectFile } from '../services/project-service/projectFileUtils.js';
import { parseJson } from '../utils/parser.js';
/**
* @description ensures directories exist and populates database
* @return {string} - path to db file
*/
const populateDb = (): string => {
// if everything goes well, the DB in disk is the one loaded
let dbInDisk = resolveDbPath;
ensureDirectory(resolveDbDirectory);
// if dbInDisk doesn't exist we want to use startup db
if (!existsSync(dbInDisk)) {
try {
const dbDirectory = resolveDbDirectory;
const newFileDirectory = join(dbDirectory, pathToStartDb.split('/').pop());
copyFileSync(pathToStartDb, newFileDirectory);
dbInDisk = newFileDirectory;
} catch (_) {
/* we do not handle this */
}
}
return dbInDisk;
};
/**
* @description parses a json file to the adapter
* It will create an empty file from the model if the parsing fails
*/
const parseDatabase = async (fileToRead: string, adapterToUse: Low<DatabaseModel>) => {
try {
// this will throw if file is not valid
parseProjectFile(fileToRead);
await adapterToUse.read();
} catch (error) {
adapterToUse.data = dbModel;
}
return parseJson(adapterToUse.data);
};
/**
* @description loads ontime db
* @return {Promise<{data: (*), db: Low<unknown>}>}
*/
async function loadDb() {
const dbInDisk = populateDb();
const adapter = new JSONFile<DatabaseModel>(dbInDisk);
const db = new Low(adapter, dbModel);
const data = await parseDatabase(dbInDisk, db);
db.data = data;
await db.write();
return { db, data };
}
export let db = {} as Low<DatabaseModel>;
export let data = {} as DatabaseModel;
export const dbLoadingProcess = loadDb();
const init = async () => {
const dbProvider = await dbLoadingProcess;
db = dbProvider.db;
data = dbProvider.data;
};
init();
+21
View File
@@ -0,0 +1,21 @@
import { copyFile } from 'fs/promises';
import { pathToStartDemo, resolveDemoDirectory, resolveDemoPath } from './index.js';
import { ensureDirectory } from '../utils/fileManagement.js';
/**
* @description ensures directories exist and populates demo folder
*/
export const populateDemo = () => {
ensureDirectory(resolveDemoDirectory);
// even if demo exist we want to use startup demo
try {
Promise.all(
resolveDemoPath.map((to, index) => {
const from = pathToStartDemo[index];
return copyFile(from, to);
}),
);
} catch (_) {
/* we do not handle this */
}
};
+18
View File
@@ -0,0 +1,18 @@
import { copyFileSync, existsSync } from 'fs';
import { pathToStartStyles, resolveStylesDirectory, resolveStylesPath } from './index.js';
import { ensureDirectory } from '../utils/fileManagement.js';
/**
* @description ensures directories exist and populates stylesheet
*/
export const populateStyles = () => {
ensureDirectory(resolveStylesDirectory);
// if styles doesn't exist we want to use startup stylesheet
if (!existsSync(resolveStylesPath)) {
try {
copyFileSync(pathToStartStyles, resolveStylesPath);
} catch (_) {
/* we do not handle this */
}
}
};