From 05b29c60f1a5056fe6e663825a9b2aaf20c07b50 Mon Sep 17 00:00:00 2001 From: axel7083 <42176370+axel7083@users.noreply.github.com> Date: Thu, 30 May 2024 11:15:21 +0200 Subject: [PATCH] fix: json watcher parent problem (#1135) * fix: json watcher parent problem Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> * Apply suggestions from code review Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> * fix: prettier Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --------- Signed-off-by: axel7083 <42176370+axel7083@users.noreply.github.com> --- packages/backend/src/utils/JsonWatcher.spec.ts | 4 +++- packages/backend/src/utils/JsonWatcher.ts | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) 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));