Skip to content

Commit

Permalink
style: move "important" funcs to top of file
Browse files Browse the repository at this point in the history
  • Loading branch information
pano9000 committed Jan 6, 2025
1 parent e223cb8 commit d492ea0
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions src/services/data_dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof os.platform>, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) {

switch(true) {
Expand All @@ -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);
Expand Down

0 comments on commit d492ea0

Please sign in to comment.