This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
AppSettings.ts
177 lines (150 loc) · 5.47 KB
/
AppSettings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import * as DeepMerge from "deepmerge";
import { existsSync } from "fs";
import { ISettingsFile } from "./doc/ISettingsFile";
import { Logger } from "../../logger";
import { ISettingsFilePersistence } from "./persistance/ISettingsFilePersistence";
import { JSONSettingsFilePersistence } from "./persistance/JSONSettingsFilePersistence";
import { IO } from "../../io";
import { ImperativeError } from "../../error";
type SettingValue = false | string;
/**
* This class represents settings for an Imperative CLI application that can be configured
* by an end user by modifying a settings file. Settings are stored in {@link AppSettings#settings}
* in a format specified by {@link ISettingsFile}.
*/
export class AppSettings {
/**
* Initialize
* @param settingsFile The settings file to load from
* @param defaults {@link ISettingsFile} Settings with default values
*/
public static initialize(settingsFile: string, defaults: ISettingsFile): AppSettings {
if (AppSettings.mInstance) {
const {SettingsAlreadyInitialized} = require("./errors/index");
throw new SettingsAlreadyInitialized();
}
const persistence = new JSONSettingsFilePersistence(settingsFile);
let settings = {};
try {
Logger.getImperativeLogger().trace(`Attempting to load settings file: ${settingsFile}`);
settings = persistence.read();
} catch (up) {
if (!existsSync(settingsFile)) {
Logger.getImperativeLogger().trace("Executing missing file recovery.");
IO.createDirsSyncFromFilePath(settingsFile);
persistence.write(defaults);
} else {
Logger.getImperativeLogger().error("Unable to recover from load failure");
Logger.getImperativeLogger().error(up.toString());
throw up;
}
}
const initialSettings = DeepMerge(defaults, settings);
AppSettings.mInstance = new AppSettings(
persistence,
initialSettings,
);
Logger.getImperativeLogger().trace("Settings were loaded");
Logger.getImperativeLogger().trace("Loaded Settings:");
Logger.getImperativeLogger().trace(initialSettings as any);
return AppSettings.mInstance;
}
/**
* This is an internal reference to the static settings instance.
*/
private static mInstance: AppSettings;
/**
* The settings loaded from the file specified in the constructor.
*/
private readonly settings: ISettingsFile;
private readonly persistence: ISettingsFilePersistence;
/**
* Constructs a new settings object
*
* @param persistence
* @param initial Initial settings object
*/
constructor(persistence: ISettingsFilePersistence, initial: ISettingsFile) {
this.persistence = persistence;
this.settings = initial;
}
/**
* Get the singleton instance of the app settings object that was initialized
* within the {@link AppSettings.initialize} function.
*
* @returns A singleton AppSettings object
*
* @throws {@link SettingsNotInitialized} When the settings singleton has not been initialized.
*/
public static get instance(): AppSettings {
if (AppSettings.mInstance == null) {
// Throw an error imported at runtime so that we minimize file that get included
// on startup.
const {SettingsNotInitialized} = require("./errors/index");
throw new SettingsNotInitialized();
}
return AppSettings.mInstance;
}
/**
* @returns true if the app settings have been initialized
*/
public static get initialized(): boolean {
return !(this.mInstance == null);
}
/**
* Set a settings option and save it to the settings file.
* @param namespace {@link ISettingsFile}
* @param key Name of a setting option to set
* @param value
*/
public set(namespace: keyof ISettingsFile, key: string, value: SettingValue): void {
this.settings[namespace][key] = value;
this.flush();
}
/**
* Get a value of settings option
* @param namespace {@link ISettingsFile}
* @param key Name of a setting option to set
*/
public get(namespace: keyof ISettingsFile, key: string): SettingValue {
if (this.settings[namespace]) {
return this.settings[namespace][key];
}
throw new ImperativeError({msg: `Namespace ${namespace} does not exist`});
}
/**
* Get a member of ISettingsFile of specified namespace
* @param namespace
*/
public getNamespace(namespace: keyof ISettingsFile) {
return this.settings[namespace];
}
/**
* Get settings
*/
public getSettings(): ISettingsFile {
return this.settings;
}
/**
* Writes settings to the file
*/
private flush() {
try {
this.persistence.write(this.settings);
} catch (err) {
Logger.getImperativeLogger().error("Unable to save settings");
Logger.getImperativeLogger().error(err.toString());
throw err;
}
}
}