Skip to content

Commit

Permalink
chore(prettier): fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
pano9000 committed Jan 13, 2025
1 parent 6818b2d commit 5373ef5
Showing 1 changed file with 37 additions and 47 deletions.
84 changes: 37 additions & 47 deletions src/services/data_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,60 +10,53 @@

import os from "os";
import fs from "fs";
import { join as pathJoin} from "path";
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;
// 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 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<typeof os.platform>, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) {

switch(true) {
switch (true) {
case platform === "win32" && !!ENV_APPDATA_DIR:
return ENV_APPDATA_DIR;

Expand All @@ -77,7 +70,6 @@ export function getPlatformAppDataDir(platform: ReturnType<typeof os.platform>,
// if OS is not recognized
return null;
}

}

function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) {
Expand All @@ -86,9 +78,7 @@ function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOL
}
}



const TRILIUM_DATA_DIR = getTriliumDataDir(DIR_NAME);
const dataDirs = getDataDirs(TRILIUM_DATA_DIR);

export default dataDirs;
export default dataDirs;

0 comments on commit 5373ef5

Please sign in to comment.