2017-10-24 11:38:52 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-12-01 02:56:20 +08:00
|
|
|
/*
|
|
|
|
* 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 previous step fails, we'll use home dir
|
|
|
|
*/
|
|
|
|
|
2017-10-24 11:30:23 +08:00
|
|
|
const os = require('os');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2018-12-01 02:56:20 +08:00
|
|
|
function getAppDataDir() {
|
|
|
|
let appDataDir = os.homedir(); // fallback if OS is not recognized
|
|
|
|
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
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';
|
|
|
|
}
|
2017-10-24 11:30:23 +08:00
|
|
|
|
2018-12-01 02:56:20 +08:00
|
|
|
if (!fs.existsSync(appDataDir)) {
|
|
|
|
// expected app data path doesn't exist, let's use fallback
|
|
|
|
appDataDir = os.homedir();
|
|
|
|
}
|
|
|
|
|
|
|
|
return appDataDir;
|
2017-10-24 11:30:23 +08:00
|
|
|
}
|
|
|
|
|
2018-12-01 02:56:20 +08:00
|
|
|
const DIR_NAME = 'trilium-data';
|
|
|
|
|
|
|
|
function getTriliumDataDir() {
|
|
|
|
if (process.env.TRILIUM_DATA_DIR) {
|
2019-04-21 15:05:55 +08:00
|
|
|
if (!fs.existsSync(process.env.TRILIUM_DATA_DIR)) {
|
|
|
|
fs.mkdirSync(process.env.TRILIUM_DATA_DIR, 0o700);
|
|
|
|
}
|
|
|
|
|
2018-12-01 02:56:20 +08:00
|
|
|
return process.env.TRILIUM_DATA_DIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
const homePath = os.homedir() + "/" + DIR_NAME;
|
|
|
|
|
|
|
|
if (fs.existsSync(homePath)) {
|
|
|
|
return homePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const appDataPath = getAppDataDir() + '/' + DIR_NAME;
|
|
|
|
|
|
|
|
if (!fs.existsSync(appDataPath)) {
|
|
|
|
fs.mkdirSync(appDataPath, 0o700);
|
|
|
|
}
|
|
|
|
|
|
|
|
return appDataPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TRILIUM_DATA_DIR = getTriliumDataDir();
|
2017-10-24 11:30:23 +08:00
|
|
|
const DOCUMENT_PATH = TRILIUM_DATA_DIR + "/document.db";
|
|
|
|
const BACKUP_DIR = TRILIUM_DATA_DIR + "/backup";
|
2017-10-25 10:04:52 +08:00
|
|
|
const LOG_DIR = TRILIUM_DATA_DIR + "/log";
|
2017-12-16 13:05:37 +08:00
|
|
|
const ANONYMIZED_DB_DIR = TRILIUM_DATA_DIR + "/anonymized-db";
|
2017-10-24 11:30:23 +08:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
TRILIUM_DATA_DIR,
|
|
|
|
DOCUMENT_PATH,
|
2017-10-25 10:04:52 +08:00
|
|
|
BACKUP_DIR,
|
2017-12-03 10:48:22 +08:00
|
|
|
LOG_DIR,
|
2017-12-16 13:05:37 +08:00
|
|
|
ANONYMIZED_DB_DIR
|
2017-10-24 11:30:23 +08:00
|
|
|
};
|