A local-first personal finance app
🐛 CLI: Fix ACTUAL_DATA_DIR env variable not respected in some cases (#4825)
Michael Clark153eaeecc5c8
diff --git a/packages/sync-server/bin/actual-server.js b/packages/sync-server/bin/actual-server.jsindex 973d187dda..ef6955035a 100644--- a/packages/sync-server/bin/actual-server.js+++ b/packages/sync-server/bin/actual-server.js@@ -59,7 +59,30 @@ if (values.version) {process.exit();}-// Read the config argument if specified+const setupDataDir = (dataDir = undefined) => {+ if (process.env.ACTUAL_DATA_DIR) {+ return; // Env variables must not be overwritten+ }++ if (dataDir) {+ process.env.ACTUAL_DATA_DIR = dataDir; // Use the dir specified+ } else {+ // Setup defaults+ if (existsSync('./data')) {+ // The default data directory exists - use it+ console.info('Found existing data directory');+ process.env.ACTUAL_DATA_DIR = './data';+ } else {+ console.info(+ 'Using default data directory. You can specify a custom config with --config',+ );+ process.env.ACTUAL_DATA_DIR = './';+ }++ console.info(`Data directory: ${process.env.ACTUAL_DATA_DIR}`);+ }+};+if (values.config) {const configExists = existsSync(values.config);@@ -69,19 +92,25 @@ if (values.config) {);process.exit();- } else if (values.config) {+ } else {console.log(`Loading config from ${values.config}`);+ const configJson = JSON.parse(readFileSync(values.config, 'utf-8'));process.env.ACTUAL_CONFIG_PATH = values.config;+ setupDataDir(configJson.dataDir);}} else {- // No config specified, use reasonable defaults- console.info(- 'Using default config. You can specify a custom config with --config',- );- process.env.ACTUAL_DATA_DIR = './';- console.info(- 'user-files and server-files will be created in the current directory',- );+ // If no config is specified, check for a default config in the current directory+ const defaultConfigJsonFile = './config.json';+ const configExists = existsSync(defaultConfigJsonFile);++ if (configExists) {+ console.info('Found config.json in the current directory');+ const configJson = JSON.parse(readFileSync(defaultConfigJsonFile, 'utf-8'));+ process.env.ACTUAL_CONFIG_PATH = defaultConfigJsonFile;+ setupDataDir(configJson.dataDir);+ } else {+ setupDataDir(); // No default config exists - setup data dir with defaults+ }}// start the sync serverdiff --git a/upcoming-release-notes/4825.md b/upcoming-release-notes/4825.mdnew file mode 100644index 0000000000..cc09b6385a--- /dev/null+++ b/upcoming-release-notes/4825.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [MikesGlitch]+---++Fix CLI issue where ACTAUL_DATA_DIR env variable was not being respected when no config is present