diff --git a/packages/backend/src/utils/JsonWatcher.spec.ts b/packages/backend/src/utils/JsonWatcher.spec.ts index bca24e88a..f146e90b1 100644 --- a/packages/backend/src/utils/JsonWatcher.spec.ts +++ b/packages/backend/src/utils/JsonWatcher.spec.ts @@ -16,7 +16,7 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ import { beforeEach, describe, expect, test, vi } from 'vitest'; -import { promises, existsSync } from 'node:fs'; +import { promises, existsSync, mkdirSync } from 'node:fs'; import type { FileSystemWatcher } from '@podman-desktop/api'; import { EventEmitter, fs } from '@podman-desktop/api'; import { JsonWatcher } from './JsonWatcher'; @@ -37,6 +37,7 @@ vi.mock('@podman-desktop/api', () => { vi.mock('node:fs', () => { return { existsSync: vi.fn(), + mkdirSync: vi.fn(), promises: { readFile: vi.fn(), }, @@ -68,6 +69,7 @@ test('should provide default value', async () => { await vi.waitFor(() => { expect(listener).toHaveBeenCalledWith('dummyDefaultvalue'); }); + expect(mkdirSync).toHaveBeenCalled(); expect(existsSync).toHaveBeenCalledWith('dummyPath'); expect(promises.readFile).not.toHaveBeenCalled(); }); diff --git a/packages/backend/src/utils/JsonWatcher.ts b/packages/backend/src/utils/JsonWatcher.ts index 95dacbcf8..019086658 100644 --- a/packages/backend/src/utils/JsonWatcher.ts +++ b/packages/backend/src/utils/JsonWatcher.ts @@ -16,7 +16,8 @@ * SPDX-License-Identifier: Apache-2.0 ***********************************************************************/ import { type Disposable, type FileSystemWatcher, fs, EventEmitter, type Event } from '@podman-desktop/api'; -import { promises, existsSync } from 'node:fs'; +import { promises, existsSync, mkdirSync } from 'node:fs'; +import path from 'node:path'; export class JsonWatcher implements Disposable { #fileSystemWatcher: FileSystemWatcher | undefined; @@ -31,6 +32,11 @@ export class JsonWatcher implements Disposable { init(): void { try { + // we create the parent directory of the watched content + // if the parent directory does not exists, the watcher is not initialized properly + mkdirSync(path.dirname(this.path), { recursive: true }); + + // create file system watcher this.#fileSystemWatcher = fs.createFileSystemWatcher(this.path); // Setup listeners this.#fileSystemWatcher.onDidChange(this.onDidChange.bind(this));