Skip to content

Commit 7509652

Browse files
authored
Fix: auto-detect Things DB container dir instead of hardcoding suffix (#1)
The ThingsData-XXXXX directory suffix under the group container is randomized per Things install, so the hardcoded ThingsData-C1ON7 path fails on every other machine with 'Cannot open database because the directory does not exist'. Resolve the DB path at runtime: honor a THINGS_DB_PATH override, else scan the group container for the live ThingsData-* dir, else fall back to the legacy hardcoded path so error messages stay informative.
1 parent 6631956 commit 7509652

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

src/lib/constants.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,37 @@
22

33
const path = require('path');
44
const os = require('os');
5+
const fs = require('fs');
56

6-
const DB_PATH = path.join(
7+
const GROUP_CONTAINER = path.join(
78
os.homedir(),
8-
'Library/Group Containers/JLMPQHK86H.com.culturedcode.ThingsMac/ThingsData-C1ON7/Things Database.thingsdatabase/main.sqlite'
9+
'Library/Group Containers/JLMPQHK86H.com.culturedcode.ThingsMac'
910
);
11+
const DB_LEAF = 'Things Database.thingsdatabase/main.sqlite';
12+
13+
// The `ThingsData-XXXXX` directory suffix is randomized per Things install, so
14+
// it cannot be hardcoded. Resolution order:
15+
// 1. THINGS_DB_PATH env var (explicit override)
16+
// 2. Auto-detect the live DB under the group container (skipping Backups)
17+
// 3. Fall back to the original hardcoded path (so errors stay informative)
18+
function resolveDbPath() {
19+
if (process.env.THINGS_DB_PATH) return process.env.THINGS_DB_PATH;
20+
21+
try {
22+
const candidates = fs
23+
.readdirSync(GROUP_CONTAINER)
24+
.filter((name) => name.startsWith('ThingsData-'))
25+
.map((name) => path.join(GROUP_CONTAINER, name, DB_LEAF))
26+
.filter((p) => fs.existsSync(p));
27+
if (candidates.length) return candidates[0];
28+
} catch {
29+
// Group container missing — fall through to the legacy default.
30+
}
31+
32+
return path.join(GROUP_CONTAINER, 'ThingsData-C1ON7', DB_LEAF);
33+
}
34+
35+
const DB_PATH = resolveDbPath();
1036

1137
const TYPE = { TASK: 0, PROJECT: 1, HEADING: 2 };
1238
const STATUS = { OPEN: 0, CANCELED: 2, COMPLETED: 3 };

0 commit comments

Comments
 (0)