@theia/core
Version:
Theia is a cloud & desktop IDE framework implemented in TypeScript.
88 lines • 3.4 kB
JavaScript
// *****************************************************************************
// Copyright (C) 2025 STMicroelectronics and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0.
//
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License v. 2.0 are satisfied: GNU General Public License, version 2
// with the GNU Classpath Exception which is available at
// https://www.gnu.org/software/classpath/license.html.
//
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
// *****************************************************************************
import { __decorate } from "tslib";
import { promises as fs } from 'fs';
import { inject, injectable, postConstruct, named } from 'inversify';
import { ILogger, URI } from '../common/index.js';
import { EnvVariablesServer } from '../common/env-variables/index.js';
import { Deferred } from '../common/promise-util.js';
export const SettingService = Symbol('SettingService');
let SettingServiceImpl = class SettingServiceImpl {
constructor() {
this.ready = new Deferred();
this.values = {};
}
init() {
const asyncInit = async () => {
const settingsFileUri = await this.getSettingsFileUri();
const path = settingsFileUri.path.fsPath();
try {
const contents = await fs.readFile(path, 'utf8');
this.values = JSON.parse(contents);
}
catch (e) {
if (e instanceof Error && 'code' in e && e.code === 'ENOENT') {
this.logger.info(`Settings file not found at '${path}'. Falling back to defaults.`);
}
else {
this.logger.warn(`Failed to read settings file at '${path}'. Falling back to defaults.`, e);
}
}
finally {
this.ready.resolve();
}
};
asyncInit();
}
async set(key, value) {
await this.ready.promise;
this.values[key] = value;
await this.writeFile();
}
async writeFile() {
const settingsFileUri = await this.getSettingsFileUri();
const path = settingsFileUri.path.fsPath();
const values = JSON.stringify(this.values);
await fs.writeFile(path, values);
}
async get(key) {
await this.ready.promise;
return this.values[key];
}
async getConfigDirUri() {
const uri = await this.envVarServer.getConfigDirUri();
return new URI(uri);
}
async getSettingsFileUri() {
const configDir = await this.getConfigDirUri();
return configDir.resolve('backend-settings.json');
}
};
__decorate([
inject(ILogger),
named('core:SettingServiceImpl')
], SettingServiceImpl.prototype, "logger", void 0);
__decorate([
inject(EnvVariablesServer)
], SettingServiceImpl.prototype, "envVarServer", void 0);
__decorate([
postConstruct()
], SettingServiceImpl.prototype, "init", null);
SettingServiceImpl = __decorate([
injectable()
], SettingServiceImpl);
export { SettingServiceImpl };
//# sourceMappingURL=setting-service.js.map