Set up a watcher that checks for changes to the new db config (#1699)

Co-authored-by: Nora <norascheuch@users.noreply.github.com>
Co-authored-by: Charis Kyriakou <charisk@users.noreply.github.com>
This commit is contained in:
Shati Patel 2022-11-03 12:26:30 +00:00 коммит произвёл GitHub
Родитель 26e2021551
Коммит 684c492a43
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 559 добавлений и 2079 удалений

2598
extensions/ql-vscode/package-lock.json сгенерированный

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -1277,6 +1277,7 @@
"@vscode/codicons": "^0.0.31",
"@vscode/webview-ui-toolkit": "^1.0.1",
"child-process-promise": "^2.2.1",
"chokidar": "^3.5.3",
"classnames": "~2.2.6",
"d3": "^7.6.1",
"d3-graphviz": "^2.6.1",

Просмотреть файл

@ -1,20 +1,30 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { cloneDbConfig, DbConfig } from './db-config';
import * as chokidar from 'chokidar';
import { DisposableObject } from '../pure/disposable-object';
export class DbConfigStore {
export class DbConfigStore extends DisposableObject {
private readonly configPath: string;
private config: DbConfig;
private configWatcher: chokidar.FSWatcher | undefined;
public constructor(workspaceStoragePath: string) {
super();
this.configPath = path.join(workspaceStoragePath, 'dbconfig.json');
this.config = this.createEmptyConfig();
this.configWatcher = undefined;
}
public async initialize(): Promise<void> {
await this.loadConfig();
this.watchConfig();
}
public dispose(): void {
this.configWatcher?.unwatch(this.configPath);
}
public getConfig(): DbConfig {
@ -34,6 +44,16 @@ export class DbConfigStore {
this.config = await fs.readJSON(this.configPath);
}
private readConfigSync(): void {
this.config = fs.readJSONSync(this.configPath);
}
private watchConfig(): void {
this.configWatcher = chokidar.watch(this.configPath).on('change', () => {
this.readConfigSync();
});
}
private createEmptyConfig(): DbConfig {
return {
remote: {

Просмотреть файл

@ -3,23 +3,22 @@ import * as path from 'path';
import { DbConfigStore } from '../../../src/databases/db-config-store';
import { expect } from 'chai';
describe('db config store', async () => {
const workspaceStoragePath = path.join(__dirname, 'test-workspace');
const testStoragePath = path.join(__dirname, 'data');
const tempWorkspaceStoragePath = path.join(__dirname, 'test-workspace');
const testDataStoragePath = path.join(__dirname, 'data');
beforeEach(async () => {
await fs.ensureDir(workspaceStoragePath);
await fs.ensureDir(tempWorkspaceStoragePath);
});
afterEach(async () => {
await fs.remove(workspaceStoragePath);
await fs.remove(tempWorkspaceStoragePath);
});
it('should create a new config if one does not exist', async () => {
const configPath = path.join(workspaceStoragePath, 'dbconfig.json');
const configPath = path.join(tempWorkspaceStoragePath, 'dbconfig.json');
const configStore = new DbConfigStore(workspaceStoragePath);
const configStore = new DbConfigStore(tempWorkspaceStoragePath);
await configStore.initialize();
expect(await fs.pathExists(configPath)).to.be.true;
@ -30,7 +29,7 @@ describe('db config store', async () => {
});
it('should load an existing config', async () => {
const configStore = new DbConfigStore(testStoragePath);
const configStore = new DbConfigStore(testDataStoragePath);
await configStore.initialize();
const config = configStore.getConfig();
@ -45,7 +44,7 @@ describe('db config store', async () => {
});
it('should not allow modification of the config', async () => {
const configStore = new DbConfigStore(testStoragePath);
const configStore = new DbConfigStore(testDataStoragePath);
await configStore.initialize();
const config = configStore.getConfig();