mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-10-27 01:51:34 +08:00
refactor(data_dir): logically order/split cases in getTriliumDataDir
- the blocks now clearly follow the intended logic described in the comments - I renamed the `getAppDataDir` to more specific `getPlatformAppDataDir`
This commit is contained in:
parent
3481c8ba84
commit
7a1e8714af
@ -2,38 +2,38 @@
|
||||
|
||||
/*
|
||||
* 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 { join as pathJoin} 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;
|
||||
}
|
||||
|
||||
const DIR_NAME = "trilium-data";
|
||||
const FOLDER_PERMISSIONS = 0o700;
|
||||
|
||||
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);
|
||||
@ -41,20 +41,29 @@ function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOL
|
||||
}
|
||||
|
||||
function getTriliumDataDir() {
|
||||
// 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(), DIR_NAME);
|
||||
if (fs.existsSync(homePath)) {
|
||||
return homePath;
|
||||
}
|
||||
|
||||
const appDataPath = pathJoin(getAppDataDir(), DIR_NAME);
|
||||
createDirIfNotExisting(appDataPath);
|
||||
// case C
|
||||
const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA);
|
||||
if (platformAppDataDir && fs.existsSync(platformAppDataDir)) {
|
||||
const appDataDirPath = pathJoin(platformAppDataDir, DIR_NAME);
|
||||
createDirIfNotExisting(appDataDirPath);
|
||||
return appDataDirPath;
|
||||
}
|
||||
|
||||
return appDataPath;
|
||||
// case D
|
||||
createDirIfNotExisting(homePath);
|
||||
return homePath;
|
||||
}
|
||||
|
||||
const TRILIUM_DATA_DIR = getTriliumDataDir();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user