From 61a19d5628dcbbad79c7a608a2be8672db5fcb63 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 15:03:42 +0100 Subject: [PATCH 01/12] refactor(data_dir): add FOLDER_PERMISSION const gets rid of previously "magic number" --- src/services/data_dir.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index b87b00d32..97cc6652f 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -32,11 +32,13 @@ function getAppDataDir() { } const DIR_NAME = "trilium-data"; +const FOLDER_PERMISSIONS = 0o700; + function getTriliumDataDir() { if (process.env.TRILIUM_DATA_DIR) { if (!fs.existsSync(process.env.TRILIUM_DATA_DIR)) { - fs.mkdirSync(process.env.TRILIUM_DATA_DIR, 0o700); + fs.mkdirSync(process.env.TRILIUM_DATA_DIR, FOLDER_PERMISSIONS); } return process.env.TRILIUM_DATA_DIR; @@ -51,7 +53,7 @@ function getTriliumDataDir() { const appDataPath = getAppDataDir() + path.sep + DIR_NAME; if (!fs.existsSync(appDataPath)) { - fs.mkdirSync(appDataPath, 0o700); + fs.mkdirSync(appDataPath, FOLDER_PERMISSIONS); } return appDataPath; From 8826021c63b259a0774494564e3e88f0b618e0c4 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 15:06:42 +0100 Subject: [PATCH 02/12] refactor(data_dir): add createDirIfNotExisting function removes some code duplication --- src/services/data_dir.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 97cc6652f..4e7c0315a 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -34,13 +34,15 @@ function getAppDataDir() { const DIR_NAME = "trilium-data"; const FOLDER_PERMISSIONS = 0o700; +function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) { + if (!fs.existsSync(path)) { + fs.mkdirSync(path, permissionMode); + } +} function getTriliumDataDir() { if (process.env.TRILIUM_DATA_DIR) { - if (!fs.existsSync(process.env.TRILIUM_DATA_DIR)) { - fs.mkdirSync(process.env.TRILIUM_DATA_DIR, FOLDER_PERMISSIONS); - } - + createDirIfNotExisting(process.env.TRILIUM_DATA_DIR); return process.env.TRILIUM_DATA_DIR; } @@ -52,9 +54,7 @@ function getTriliumDataDir() { const appDataPath = getAppDataDir() + path.sep + DIR_NAME; - if (!fs.existsSync(appDataPath)) { - fs.mkdirSync(appDataPath, FOLDER_PERMISSIONS); - } + createDirIfNotExisting(appDataPath); return appDataPath; } From 3481c8ba84133944bfe1d40017f0e27cb61551c9 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 15:45:41 +0100 Subject: [PATCH 03/12] refactor(data_dir): use path.join for safer joins https://nodejs.org/api/path.html#pathjoinpaths --- src/services/data_dir.ts | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 4e7c0315a..050afd078 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -10,7 +10,7 @@ import os from "os"; import fs from "fs"; -import path from "path"; +import { join as pathJoin} from "path"; function getAppDataDir() { let appDataDir = os.homedir(); // fallback if OS is not recognized @@ -46,27 +46,24 @@ function getTriliumDataDir() { return process.env.TRILIUM_DATA_DIR; } - const homePath = os.homedir() + path.sep + DIR_NAME; - + const homePath = pathJoin(os.homedir(), DIR_NAME); if (fs.existsSync(homePath)) { return homePath; } - const appDataPath = getAppDataDir() + path.sep + DIR_NAME; - + const appDataPath = pathJoin(getAppDataDir(), DIR_NAME); createDirIfNotExisting(appDataPath); return appDataPath; } const TRILIUM_DATA_DIR = getTriliumDataDir(); -const DIR_SEP = TRILIUM_DATA_DIR + path.sep; -const DOCUMENT_PATH = process.env.TRILIUM_DOCUMENT_PATH || `${DIR_SEP}document.db`; -const BACKUP_DIR = process.env.TRILIUM_BACKUP_DIR || `${DIR_SEP}backup`; -const LOG_DIR = process.env.TRILIUM_LOG_DIR || `${DIR_SEP}log`; -const ANONYMIZED_DB_DIR = process.env.TRILIUM_ANONYMIZED_DB_DIR || `${DIR_SEP}anonymized-db`; -const CONFIG_INI_PATH = process.env.TRILIUM_CONFIG_INI_PATH || `${DIR_SEP}config.ini`; +const DOCUMENT_PATH = process.env.TRILIUM_DOCUMENT_PATH || pathJoin(TRILIUM_DATA_DIR, "document.db"); +const BACKUP_DIR = process.env.TRILIUM_BACKUP_DIR || pathJoin(TRILIUM_DATA_DIR, "backup"); +const LOG_DIR = process.env.TRILIUM_LOG_DIR || pathJoin(TRILIUM_DATA_DIR, "log"); +const ANONYMIZED_DB_DIR = process.env.TRILIUM_ANONYMIZED_DB_DIR || pathJoin(TRILIUM_DATA_DIR, "anonymized-db"); +const CONFIG_INI_PATH = process.env.TRILIUM_CONFIG_INI_PATH || pathJoin(TRILIUM_DATA_DIR, "config.ini"); export default { TRILIUM_DATA_DIR, From 7a1e8714af49a143012eb65344c4a2329547339e Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 20:38:37 +0100 Subject: [PATCH 04/12] 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` --- src/services/data_dir.ts | 61 +++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 050afd078..5b963dae7 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -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, 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(); From 759d24855b42f16238d3b8d2fa3b89228f7f6769 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 20:42:03 +0100 Subject: [PATCH 05/12] style(data_dir): fix indentation --- src/services/data_dir.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 5b963dae7..090c334b7 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -35,9 +35,9 @@ function getPlatformAppDataDir(platform: ReturnType, ENV_APP } function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) { - if (!fs.existsSync(path)) { - fs.mkdirSync(path, permissionMode); - } + if (!fs.existsSync(path)) { + fs.mkdirSync(path, permissionMode); + } } function getTriliumDataDir() { From 8b1071c4593b51cc656fed25dff8f0b256ab562c Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 21:17:44 +0100 Subject: [PATCH 06/12] refactor(data_dir): export dirs as frozen readonly object previously exported object allowed the values to be changed accidentally at runtime and buildtime --- src/services/data_dir.ts | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 090c334b7..6b7806628 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -66,19 +66,28 @@ function getTriliumDataDir() { return homePath; } +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(); +const dataDirs = getDataDirs(TRILIUM_DATA_DIR); -const DOCUMENT_PATH = process.env.TRILIUM_DOCUMENT_PATH || pathJoin(TRILIUM_DATA_DIR, "document.db"); -const BACKUP_DIR = process.env.TRILIUM_BACKUP_DIR || pathJoin(TRILIUM_DATA_DIR, "backup"); -const LOG_DIR = process.env.TRILIUM_LOG_DIR || pathJoin(TRILIUM_DATA_DIR, "log"); -const ANONYMIZED_DB_DIR = process.env.TRILIUM_ANONYMIZED_DB_DIR || pathJoin(TRILIUM_DATA_DIR, "anonymized-db"); -const CONFIG_INI_PATH = process.env.TRILIUM_CONFIG_INI_PATH || pathJoin(TRILIUM_DATA_DIR, "config.ini"); - -export default { - TRILIUM_DATA_DIR, - DOCUMENT_PATH, - BACKUP_DIR, - LOG_DIR, - ANONYMIZED_DB_DIR, - CONFIG_INI_PATH -}; +export default dataDirs; \ No newline at end of file From 94b8bcf8c930f04ea98aed209d3970d73e09151c Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 22:08:17 +0100 Subject: [PATCH 07/12] refactor(data_dir): export functions to allow for testing --- src/services/data_dir.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 6b7806628..c896e959e 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -15,7 +15,7 @@ import { join as pathJoin} from "path"; const DIR_NAME = "trilium-data"; const FOLDER_PERMISSIONS = 0o700; -function getPlatformAppDataDir(platform: ReturnType, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) { +export function getPlatformAppDataDir(platform: ReturnType, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) { switch(true) { case platform === "win32" && !!ENV_APPDATA_DIR: @@ -40,7 +40,7 @@ function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOL } } -function getTriliumDataDir() { +export function getTriliumDataDir() { // case A if (process.env.TRILIUM_DATA_DIR) { createDirIfNotExisting(process.env.TRILIUM_DATA_DIR); @@ -66,7 +66,7 @@ function getTriliumDataDir() { return homePath; } -function getDataDirs(TRILIUM_DATA_DIR: string) { +export function getDataDirs(TRILIUM_DATA_DIR: string) { const dataDirs = { "TRILIUM_DATA_DIR": TRILIUM_DATA_DIR, From 63079c093988c2c6ff683dd2adb32e065fc2ad2f Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Fri, 3 Jan 2025 22:08:56 +0100 Subject: [PATCH 08/12] test(data_dir): add tests for getPlatformAppDataDir --- spec-es6/data_dir.spec.ts | 78 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 spec-es6/data_dir.spec.ts diff --git a/spec-es6/data_dir.spec.ts b/spec-es6/data_dir.spec.ts new file mode 100644 index 000000000..7d80a2811 --- /dev/null +++ b/spec-es6/data_dir.spec.ts @@ -0,0 +1,78 @@ +import { describe, it, execute, expect } from "./mini_test.ts"; + +import { getPlatformAppDataDir } from "../src/services/data_dir.ts" + + + +describe("data_dir.ts unit tests", () => { + + describe("#getPlatformAppDataDir", () => { + + type TestCaseGetPlatformAppDataDir = [ + description: string, + fnValue: Parameters, + expectedValueFn: (val: ReturnType) => boolean + ] + const testCases: TestCaseGetPlatformAppDataDir[] = [ + + [ + "w/ unsupported OS it should return 'null'", + ["aix", undefined], + (val) => val === null + ], + + [ + "w/ win32 and no APPDATA set it should return 'null'", + ["win32", undefined], + (val) => val === null + ], + + [ + "w/ win32 and set APPDATA it should return set 'APPDATA'", + ["win32", "AppData"], + (val) => val === "AppData" + ], + + [ + "w/ linux it should return '/.local/share'", + ["linux", undefined], + (val) => val !== null && val.endsWith("/.local/share") + ], + + [ + "w/ linux and wrongly set APPDATA it should ignore APPDATA and return /.local/share", + ["linux", "FakeAppData"], + (val) => val !== null && val.endsWith("/.local/share") + ], + + [ + "w/ darwin it should return /Library/Application Support", + ["darwin", undefined], + (val) => val !== null && val.endsWith("/Library/Application Support") + ], + ]; + + testCases.forEach(testCase => { + const [testDescription, value, isExpected] = testCase; + return it(testDescription, () => { + const actual = getPlatformAppDataDir(...value); + const result = isExpected(actual); + expect(result).toBeTruthy() + + }) + }) + + + }) + + describe("#getTriliumDataDir", () => { + // TODO + }) + + describe("#getDataDirs", () => { + // TODO + }) + +}); + +execute() \ No newline at end of file From e021c0cd0e83096b4357f4e441d7aefc9eae493f Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Sat, 4 Jan 2025 00:16:26 +0100 Subject: [PATCH 09/12] test(data_dir): add tests for getDataDirs --- spec-es6/data_dir.spec.ts | 68 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/spec-es6/data_dir.spec.ts b/spec-es6/data_dir.spec.ts index 7d80a2811..70a329ff5 100644 --- a/spec-es6/data_dir.spec.ts +++ b/spec-es6/data_dir.spec.ts @@ -1,12 +1,12 @@ import { describe, it, execute, expect } from "./mini_test.ts"; -import { getPlatformAppDataDir } from "../src/services/data_dir.ts" +import { getPlatformAppDataDir, getDataDirs} from "../src/services/data_dir.ts" describe("data_dir.ts unit tests", () => { - describe("#getPlatformAppDataDir", () => { + describe("#getPlatformAppDataDir()", () => { type TestCaseGetPlatformAppDataDir = [ description: string, @@ -69,8 +69,68 @@ describe("data_dir.ts unit tests", () => { // TODO }) - describe("#getDataDirs", () => { - // TODO + describe("#getDataDirs()", () => { + + const envKeys: Omit, "TRILIUM_DATA_DIR">[] = [ + "DOCUMENT_PATH", + "BACKUP_DIR", + "LOG_DIR", + "ANONYMIZED_DB_DIR", + "CONFIG_INI_PATH", + ]; + + const setMockedEnv = (prefix: string | null) => { + envKeys.forEach(key => { + if (prefix) { + process.env[`TRILIUM_${key}`] = `${prefix}_${key}` + } else { + delete process.env[`TRILIUM_${key}`] + } + }) + }; + + it("w/ process.env values present, it should return an object using values from process.env", () => { + + // set mocked values + const mockValuePrefix = "MOCK"; + setMockedEnv(mockValuePrefix); + + // get result + const result = getDataDirs(`${mockValuePrefix}_TRILIUM_DATA_DIR`); + + for (const key in result) { + expect(result[key]).toEqual(`${mockValuePrefix}_${key}`) + } + }) + + it("w/ NO process.env values present, it should return an object using supplied TRILIUM_DATA_DIR as base", () => { + + // make sure values are undefined + setMockedEnv(null); + + const mockDataDir = "/home/test/MOCK_TRILIUM_DATA_DIR" + const result = getDataDirs(mockDataDir); + + for (const key in result) { + expect(result[key].startsWith(mockDataDir)).toBeTruthy() + } + }) + + it("should ignore attempts to change a property on the returned object", () => { + + // make sure values are undefined + setMockedEnv(null); + + const mockDataDir = "/home/test/MOCK_TRILIUM_DATA_DIR" + const result = getDataDirs(mockDataDir); + + //@ts-expect-error - attempt to change value of readonly property + result.BACKUP_DIR = "attempt to change"; + + for (const key in result) { + expect(result[key].startsWith(mockDataDir)).toBeTruthy() + } + }) }) }); From c47522eb50175f1b63b72876032cb19b54a31e66 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Mon, 6 Jan 2025 09:37:25 +0100 Subject: [PATCH 10/12] refactor(data_dir): pass DIR_NAME as argument to getTriliumDir makes it a bit cleaner and easier to test in the future, as it is one thing less that'd need mocking :-) --- src/services/data_dir.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index c896e959e..3e3ec396d 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -40,7 +40,7 @@ function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOL } } -export function getTriliumDataDir() { +export function getTriliumDataDir(dataDirName: string) { // case A if (process.env.TRILIUM_DATA_DIR) { createDirIfNotExisting(process.env.TRILIUM_DATA_DIR); @@ -48,7 +48,7 @@ export function getTriliumDataDir() { } // case B - const homePath = pathJoin(os.homedir(), DIR_NAME); + const homePath = pathJoin(os.homedir(), dataDirName); if (fs.existsSync(homePath)) { return homePath; } @@ -56,7 +56,7 @@ export function getTriliumDataDir() { // case C const platformAppDataDir = getPlatformAppDataDir(os.platform(), process.env.APPDATA); if (platformAppDataDir && fs.existsSync(platformAppDataDir)) { - const appDataDirPath = pathJoin(platformAppDataDir, DIR_NAME); + const appDataDirPath = pathJoin(platformAppDataDir, dataDirName); createDirIfNotExisting(appDataDirPath); return appDataDirPath; } @@ -87,7 +87,7 @@ export function getDataDirs(TRILIUM_DATA_DIR: string) { } -const TRILIUM_DATA_DIR = getTriliumDataDir(); +const TRILIUM_DATA_DIR = getTriliumDataDir(DIR_NAME); const dataDirs = getDataDirs(TRILIUM_DATA_DIR); export default dataDirs; \ No newline at end of file From 6818b2d54caaeb4d61f5813407725c4199d89907 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Mon, 6 Jan 2025 09:42:35 +0100 Subject: [PATCH 11/12] style: move "important" funcs to top of file --- src/services/data_dir.ts | 91 ++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 45 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 3e3ec396d..9b8c70ec6 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); From 5373ef509bdae43923df7242a72b2db785041e73 Mon Sep 17 00:00:00 2001 From: Panagiotis Papadopoulos Date: Mon, 13 Jan 2025 08:28:12 +0100 Subject: [PATCH 12/12] chore(prettier): fix code style --- src/services/data_dir.ts | 76 +++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/services/data_dir.ts b/src/services/data_dir.ts index 9b8c70ec6..2e0c2f522 100644 --- a/src/services/data_dir.ts +++ b/src/services/data_dir.ts @@ -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 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 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 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 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 + 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; + Object.freeze(dataDirs); + return dataDirs; } export function getPlatformAppDataDir(platform: ReturnType, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) { - - switch(true) { + switch (true) { case platform === "win32" && !!ENV_APPDATA_DIR: return ENV_APPDATA_DIR; @@ -77,7 +70,6 @@ export function getPlatformAppDataDir(platform: ReturnType, // if OS is not recognized return null; } - } function createDirIfNotExisting(path: fs.PathLike, permissionMode: fs.Mode = FOLDER_PERMISSIONS) { @@ -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; \ No newline at end of file +export default dataDirs;