2017-10-23 23:38:52 -04:00
|
|
|
"use strict";
|
|
|
|
|
2018-11-30 19:56:20 +01:00
|
|
|
/*
|
|
|
|
* This file resolves trilium data path in this order of priority:
|
2025-01-03 20:38:37 +01:00
|
|
|
* - 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
|
2018-11-30 19:56:20 +01:00
|
|
|
*/
|
|
|
|
|
2024-07-18 21:37:45 +03:00
|
|
|
import os from "os";
|
|
|
|
import fs from "fs";
|
2025-01-03 15:45:41 +01:00
|
|
|
import { join as pathJoin} from "path";
|
2017-10-23 23:30:23 -04:00
|
|
|
|
2025-01-03 20:38:37 +01:00
|
|
|
const DIR_NAME = "trilium-data";
|
|
|
|
const FOLDER_PERMISSIONS = 0o700;
|
2018-11-30 19:56:20 +01:00
|
|
|
|
2025-01-03 22:08:17 +01:00
|
|
|
export function getPlatformAppDataDir(platform: ReturnType<typeof os.platform>, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) {
|
2025-01-03 20:38:37 +01:00
|
|
|
|
|
|
|
switch(true) {
|
|
|
|
case platform === "win32" && !!ENV_APPDATA_DIR:
|
|
|
|
return ENV_APPDATA_DIR;
|
|
|
|
|
|
|
|
case platform === "linux":
|
|
|
|
return `${os.homedir()}/.local/share`;
|
2017-10-23 23:30:23 -04:00
|
|
|
|
2025-01-03 20:38:37 +01:00
|
|
|
case platform === "darwin":
|
|
|
|
return `${os.homedir()}/Library/Application Support`;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// if OS is not recognized
|
|
|
|
return null;
|
2018-11-30 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
2017-10-23 23:30:23 -04:00
|
|
|
}
|
|
|
|
|
2025-01-03 15:06:42 +01:00
|
|
|
function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) {
|
2025-01-03 20:42:03 +01:00
|
|
|
if (!fs.existsSync(path)) {
|
|
|
|
fs.mkdirSync(path, permissionMode);
|
|
|
|
}
|
2025-01-03 15:06:42 +01:00
|
|
|
}
|
2018-11-30 19:56:20 +01:00
|
|
|
|
2025-01-06 09:37:25 +01:00
|
|
|
export function getTriliumDataDir(dataDirName: string) {
|
2025-01-03 20:38:37 +01:00
|
|
|
// case A
|
2018-11-30 19:56:20 +01:00
|
|
|
if (process.env.TRILIUM_DATA_DIR) {
|
2025-01-03 15:06:42 +01:00
|
|
|
createDirIfNotExisting(process.env.TRILIUM_DATA_DIR);
|
2018-11-30 19:56:20 +01:00
|
|
|
return process.env.TRILIUM_DATA_DIR;
|
|
|
|
}
|
|
|
|
|
2025-01-03 20:38:37 +01:00
|
|
|
// case B
|
2025-01-06 09:37:25 +01:00
|
|
|
const homePath = pathJoin(os.homedir(), dataDirName);
|
2018-11-30 19:56:20 +01:00
|
|
|
if (fs.existsSync(homePath)) {
|
|
|
|
return homePath;
|
|
|
|
}
|
|
|
|
|
2025-01-03 20:38:37 +01:00
|
|
|
// case C
|
|
|
|
const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA);
|
|
|
|
if (platformAppDataDir && fs.existsSync(platformAppDataDir)) {
|
2025-01-06 09:37:25 +01:00
|
|
|
const appDataDirPath = pathJoin(platformAppDataDir, dataDirName);
|
2025-01-03 20:38:37 +01:00
|
|
|
createDirIfNotExisting(appDataDirPath);
|
|
|
|
return appDataDirPath;
|
|
|
|
}
|
2018-11-30 19:56:20 +01:00
|
|
|
|
2025-01-03 20:38:37 +01:00
|
|
|
// case D
|
|
|
|
createDirIfNotExisting(homePath);
|
|
|
|
return homePath;
|
2018-11-30 19:56:20 +01:00
|
|
|
}
|
|
|
|
|
2025-01-03 22:08:17 +01:00
|
|
|
export function getDataDirs(TRILIUM_DATA_DIR: string) {
|
2025-01-03 21:17:44 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
Object.freeze(dataDirs);
|
|
|
|
return dataDirs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-01-06 09:37:25 +01:00
|
|
|
const TRILIUM_DATA_DIR = getTriliumDataDir(DIR_NAME);
|
2025-01-03 21:17:44 +01:00
|
|
|
const dataDirs = getDataDirs(TRILIUM_DATA_DIR);
|
2022-12-22 19:50:00 +01:00
|
|
|
|
2025-01-03 21:17:44 +01:00
|
|
|
export default dataDirs;
|