diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 96951b242..e05c9ed7e 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -15,6 +15,52 @@ import { join as pathJoin} from "path"; const DIR_NAME = 'trilium-data'; const FOLDER_PERMISSIONS = 0o700; +export function getTriliumDataDir(dataDirName: string) { + // case A + if (process.env.TRILIUM_DATA_DIR) { + createDirIfNotExisting(process.env.TRILIUM_DATA_DIR); + return process.env.TRILIUM_DATA_DIR; + } + + // case B + const homePath = pathJoin(os.homedir(), dataDirName); + if (fs.existsSync(homePath)) { + return homePath; + } + + // case C + const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA); + if (platformAppDataDir && fs.existsSync(platformAppDataDir)) { + const appDataDirPath = pathJoin(platformAppDataDir, dataDirName); + createDirIfNotExisting(appDataDirPath); + return appDataDirPath; + } + + // case D + createDirIfNotExisting(homePath); + return homePath; +} + +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 + + Object.freeze(dataDirs); + return dataDirs; +} + export function getPlatformAppDataDir(platform: ReturnType, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) { switch(true) { @@ -40,52 +86,7 @@ function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOL } } -export function getTriliumDataDir(dataDirName: string) { - // case A - if (process.env.TRILIUM_DATA_DIR) { - createDirIfNotExisting(process.env.TRILIUM_DATA_DIR); - return process.env.TRILIUM_DATA_DIR; - } - // case B - const homePath = pathJoin(os.homedir(), dataDirName); - if (fs.existsSync(homePath)) { - return homePath; - } - - // case C - const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA); - if (platformAppDataDir && fs.existsSync(platformAppDataDir)) { - const appDataDirPath = pathJoin(platformAppDataDir, dataDirName); - createDirIfNotExisting(appDataDirPath); - return appDataDirPath; - } - - // case D - createDirIfNotExisting(homePath); - return homePath; -} - -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 - - Object.freeze(dataDirs); - return dataDirs; - -} const TRILIUM_DATA_DIR = getTriliumDataDir(DIR_NAME); const dataDirs = getDataDirs(TRILIUM_DATA_DIR);