Merge pull request #880 from pano9000/refactor_data_dir

refactor(data_dir): simplify logic and make code robust and testable
This commit is contained in:
Elian Doran
2025-01-14 20:20:32 +02:00
committed by GitHub
2 changed files with 197 additions and 51 deletions

View File

@@ -2,75 +2,83 @@
/*
* This file resolves trilium data path in this order of priority:
* - if TRILIUM_DATA_DIR environment variable exists, then its value is used as the path
* - if "trilium-data" dir exists directly in the home dir, then it is used
* - based on OS convention, if the "app data directory" exists, we'll use or create "trilium-data" directory there
* - as a fallback if the previous step fails, we'll use home dir
* - case A) if TRILIUM_DATA_DIR environment variable exists, then its value is used as the path
* - case B) if "trilium-data" dir exists directly in the home dir, then it is used
* - case C) based on OS convention, if the "app data directory" exists, we'll use or create "trilium-data" directory there
* - case D) as a fallback if the previous step fails, we'll use home dir
*/
import os from "os";
import fs from "fs";
import path from "path";
function getAppDataDir() {
let appDataDir = os.homedir(); // fallback if OS is not recognized
if (os.platform() === "win32" && process.env.APPDATA) {
appDataDir = process.env.APPDATA;
} else if (os.platform() === "linux") {
appDataDir = `${os.homedir()}/.local/share`;
} else if (os.platform() === "darwin") {
appDataDir = `${os.homedir()}/Library/Application Support`;
}
if (!fs.existsSync(appDataDir)) {
// expected app data path doesn't exist, let's use fallback
appDataDir = os.homedir();
}
return appDataDir;
}
import { join as pathJoin } from "path";
const DIR_NAME = "trilium-data";
const FOLDER_PERMISSIONS = 0o700;
function getTriliumDataDir() {
export function getTriliumDataDir(dataDirName: string) {
// case A
if (process.env.TRILIUM_DATA_DIR) {
if (!fs.existsSync(process.env.TRILIUM_DATA_DIR)) {
fs.mkdirSync(process.env.TRILIUM_DATA_DIR, 0o700);
}
createDirIfNotExisting(process.env.TRILIUM_DATA_DIR);
return process.env.TRILIUM_DATA_DIR;
}
const homePath = os.homedir() + path.sep + DIR_NAME;
// case B
const homePath = pathJoin(os.homedir(), dataDirName);
if (fs.existsSync(homePath)) {
return homePath;
}
const appDataPath = getAppDataDir() + path.sep + DIR_NAME;
if (!fs.existsSync(appDataPath)) {
fs.mkdirSync(appDataPath, 0o700);
// case C
const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA);
if (platformAppDataDir && fs.existsSync(platformAppDataDir)) {
const appDataDirPath = pathJoin(platformAppDataDir, dataDirName);
createDirIfNotExisting(appDataDirPath);
return appDataDirPath;
}
return appDataPath;
// case D
createDirIfNotExisting(homePath);
return homePath;
}
const TRILIUM_DATA_DIR = getTriliumDataDir();
const DIR_SEP = TRILIUM_DATA_DIR + path.sep;
export function getDataDirs(TRILIUM_DATA_DIR: string) {
const dataDirs = {
TRILIUM_DATA_DIR: TRILIUM_DATA_DIR,
DOCUMENT_PATH: process.env.TRILIUM_DOCUMENT_PATH || pathJoin(TRILIUM_DATA_DIR, "document.db"),
BACKUP_DIR: process.env.TRILIUM_BACKUP_DIR || pathJoin(TRILIUM_DATA_DIR, "backup"),
LOG_DIR: process.env.TRILIUM_LOG_DIR || pathJoin(TRILIUM_DATA_DIR, "log"),
ANONYMIZED_DB_DIR: process.env.TRILIUM_ANONYMIZED_DB_DIR || pathJoin(TRILIUM_DATA_DIR, "anonymized-db"),
CONFIG_INI_PATH: process.env.TRILIUM_CONFIG_INI_PATH || pathJoin(TRILIUM_DATA_DIR, "config.ini")
} as const;
const DOCUMENT_PATH = process.env.TRILIUM_DOCUMENT_PATH || `${DIR_SEP}document.db`;
const BACKUP_DIR = process.env.TRILIUM_BACKUP_DIR || `${DIR_SEP}backup`;
const LOG_DIR = process.env.TRILIUM_LOG_DIR || `${DIR_SEP}log`;
const ANONYMIZED_DB_DIR = process.env.TRILIUM_ANONYMIZED_DB_DIR || `${DIR_SEP}anonymized-db`;
const CONFIG_INI_PATH = process.env.TRILIUM_CONFIG_INI_PATH || `${DIR_SEP}config.ini`;
Object.freeze(dataDirs);
return dataDirs;
}
export default {
TRILIUM_DATA_DIR,
DOCUMENT_PATH,
BACKUP_DIR,
LOG_DIR,
ANONYMIZED_DB_DIR,
CONFIG_INI_PATH
};
export function getPlatformAppDataDir(platform: ReturnType<typeof os.platform>, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) {
switch (true) {
case platform === "win32" && !!ENV_APPDATA_DIR:
return ENV_APPDATA_DIR;
case platform === "linux":
return `${os.homedir()}/.local/share`;
case platform === "darwin":
return `${os.homedir()}/Library/Application Support`;
default:
// if OS is not recognized
return null;
}
}
function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) {
if (!fs.existsSync(path)) {
fs.mkdirSync(path, permissionMode);
}
}
const TRILIUM_DATA_DIR = getTriliumDataDir(DIR_NAME);
const dataDirs = getDataDirs(TRILIUM_DATA_DIR);
export default dataDirs;