Skip to content

Commit 2e9b289

Browse files
authored
Don't create store file if it doesn't exist
This was causing issues with app uninstalls where if an app's own data store was queried while it was in the process of being uninstalled the store file and underlying app-data directory structure would get recreated.
1 parent 0a7596c commit 2e9b289

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/umbreld/source/modules/utilities/file-store.integration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ describe('store.get()', () => {
5353

5454
test("throws if it can't read the store file", async () => {
5555
const store = new FileStore({filePath: `/`})
56-
expect(store.get()).rejects.toThrow('Unable to create initial file')
56+
expect(store.get()).rejects.toThrow('EISDIR')
5757
})
5858
})
5959

packages/umbreld/source/modules/utilities/file-store.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import yaml from 'js-yaml'
55
import {getProperty, setProperty, deleteProperty} from 'dot-prop'
66
import PQueue from 'p-queue'
77

8-
import getOrCreateFile from './get-or-create-file.js'
9-
108
type DotProp<T, P extends string> = P extends `${infer K}.${infer R}`
119
? K extends keyof T
1210
? DotProp<T[K], R>
@@ -42,11 +40,22 @@ export default class FileStore<T extends Serializable> {
4240
}
4341

4442
async #read() {
45-
const defaultValue: Serializable = {}
46-
const rawData = await getOrCreateFile(this.filePath, this.#parser.encode(defaultValue))
47-
48-
const store = (this.#parser.decode(rawData) || defaultValue) as T
43+
// Set default store value
44+
let store = {} as T
45+
46+
try {
47+
// Attempt to read and parse the store file
48+
const rawData = await fs.readFile(this.filePath, 'utf8')
49+
const data = this.#parser.decode(rawData)
50+
51+
// If we get a result, set the store value
52+
if (data) store = data as T
53+
} catch (error) {
54+
// Prevent errors if the file doesn't exist, we'll just use the default value
55+
if ((error as NodeJS.ErrnoException)?.code !== 'ENOENT') throw error
56+
}
4957

58+
// Return the store
5059
return store
5160
}
5261

0 commit comments

Comments
 (0)