npmextra/ts/npmextra.classes.appdata.ts

158 lines
5.1 KiB
TypeScript
Raw Normal View History

2023-08-24 08:39:47 +00:00
import * as plugins from './npmextra.plugins.js';
import * as paths from './npmextra.paths.js';
import { KeyValueStore } from './npmextra.classes.keyvaluestore.js';
2024-02-12 17:40:01 +00:00
import { env } from 'process';
2023-08-24 08:39:47 +00:00
2024-06-19 15:43:55 +00:00
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};
2024-06-12 18:04:04 +00:00
export interface IAppDataOptions<T = any> {
2024-02-07 20:44:00 +00:00
dirPath?: string;
2024-06-12 18:04:04 +00:00
requiredKeys?: Array<keyof T>;
2024-02-09 14:57:32 +00:00
2024-06-19 13:07:49 +00:00
/**
* wether keys should be persisted on disk or not
*/
ephermal?: boolean;
2024-02-09 14:57:32 +00:00
/**
* kvStoreKey: 'MY_ENV_VAR'
*/
2024-06-19 15:43:55 +00:00
envMapping?: DeepPartial<{
[key in keyof T]: string | object;
}>;
2024-02-07 20:44:00 +00:00
}
2024-02-12 23:48:44 +00:00
export class AppData<T = any> {
2024-01-25 12:57:55 +00:00
/**
* creates appdata. If no pathArg is given, data will be stored here:
* ${PWD}/.nogit/appdata
* @param pathArg
2024-02-09 14:57:32 +00:00
* @returns
2024-01-25 12:57:55 +00:00
*/
2024-06-12 18:04:04 +00:00
public static async createAndInit<T = any>(optionsArg: IAppDataOptions<T> = {}): Promise<AppData<T>> {
2024-02-12 23:48:44 +00:00
const appData = new AppData<T>(optionsArg);
2024-01-25 12:57:55 +00:00
await appData.readyDeferred.promise;
2023-08-26 07:56:20 +00:00
return appData;
}
// instance
2024-06-12 18:04:04 +00:00
public readyDeferred = plugins.smartpromise.defer<void>();
public options: IAppDataOptions<T>;
2024-02-12 23:50:50 +00:00
private kvStore: KeyValueStore<T>;
2024-06-12 18:04:04 +00:00
constructor(optionsArg: IAppDataOptions<T> = {}) {
2024-02-07 20:44:00 +00:00
this.options = optionsArg;
2024-01-27 18:03:06 +00:00
this.init();
2023-08-24 08:39:47 +00:00
}
2024-01-25 12:57:55 +00:00
/**
* inits app data
* @param pathArg
*/
2023-08-24 08:39:47 +00:00
private async init(pathArg?: string) {
2024-06-19 13:07:49 +00:00
if (this.options.dirPath || this.options.ephermal) {
2024-01-25 12:57:55 +00:00
// ok, nothing to do here;
2023-08-24 08:39:47 +00:00
} else {
const appDataDir = '/app/data';
const dataDir = '/data';
2023-08-24 20:59:43 +00:00
const nogitAppData = '.nogit/appdata';
2023-08-24 08:39:47 +00:00
const appDataExists = plugins.smartfile.fs.isDirectory(appDataDir);
const dataExists = plugins.smartfile.fs.isDirectory(dataDir);
if (appDataExists) {
2024-02-07 20:44:00 +00:00
this.options.dirPath = appDataDir;
2023-08-24 20:59:43 +00:00
} else if (dataExists) {
2024-02-07 20:44:00 +00:00
this.options.dirPath = dataDir;
2023-08-24 20:59:43 +00:00
} else {
await plugins.smartfile.fs.ensureDir(nogitAppData);
2024-02-07 20:44:00 +00:00
this.options.dirPath = nogitAppData;
2023-08-24 08:39:47 +00:00
}
}
2024-06-12 18:04:04 +00:00
this.kvStore = new KeyValueStore<T>({
2024-06-19 13:07:49 +00:00
typeArg: this.options.ephermal ? 'ephemeral' : 'custom',
2024-02-12 18:16:43 +00:00
identityArg: 'appkv',
customPath: this.options.dirPath,
2024-06-12 18:04:04 +00:00
mandatoryKeys: this.options.requiredKeys as Array<keyof T>
2024-02-12 18:16:43 +00:00
});
2024-02-09 14:57:32 +00:00
if (this.options.envMapping) {
2024-02-13 00:54:50 +00:00
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), plugins.path.join(process.cwd(), '.nogit'));
2024-06-12 18:04:04 +00:00
2024-02-12 19:09:26 +00:00
// Recursive function to handle nested objects, now includes key parameter
2024-06-12 18:04:04 +00:00
const processEnvMapping = async (key: keyof T, mappingValue: any, parentKey: keyof T | '' = ''): Promise<any> => {
2024-02-12 19:09:26 +00:00
if (typeof mappingValue === 'string') {
2024-06-12 18:04:04 +00:00
let envValue: string | T[keyof T];
2024-02-13 01:08:59 +00:00
if (mappingValue.startsWith('hard:')) {
2024-06-12 18:04:04 +00:00
envValue = mappingValue.replace('hard:', '') as T[keyof T];
2024-02-13 01:08:59 +00:00
} else {
2024-06-12 18:04:04 +00:00
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue) as T[keyof T];
2024-02-13 01:08:59 +00:00
}
2024-02-12 19:09:26 +00:00
if (envValue) {
2024-06-12 18:04:04 +00:00
if (typeof envValue === 'string' && mappingValue.endsWith('_JSON')) {
envValue = JSON.parse(envValue) as T[keyof T];
2024-02-12 19:09:26 +00:00
}
2024-02-13 01:01:39 +00:00
if (!parentKey) {
await this.kvStore.writeKey(key, envValue);
} else {
2024-02-13 01:10:21 +00:00
return envValue;
2024-02-13 01:01:39 +00:00
}
2024-02-13 01:08:59 +00:00
} else {
2024-02-13 01:10:21 +00:00
return undefined;
2024-02-12 19:09:26 +00:00
}
} else if (typeof mappingValue === 'object' && mappingValue !== null) {
2024-06-12 18:04:04 +00:00
const resultObject: Partial<T> = {};
2024-02-12 19:09:26 +00:00
for (const innerKey in mappingValue) {
const nestedValue = mappingValue[innerKey];
// For nested objects, call recursively but do not immediately write to kvStore
2024-06-12 18:04:04 +00:00
const nestedResult = await processEnvMapping(innerKey as keyof T, nestedValue, key);
resultObject[innerKey as keyof T] = nestedResult;
2024-02-12 19:09:26 +00:00
}
if (parentKey === '') {
// Only write to kvStore if at the top level
2024-06-12 18:04:04 +00:00
await this.kvStore.writeKey(key, resultObject as T[keyof T]);
2024-02-12 19:09:26 +00:00
} else {
// For nested objects, return the constructed object instead of writing to kvStore
return resultObject;
2024-02-12 17:40:01 +00:00
}
2024-02-09 14:57:32 +00:00
}
2024-02-12 19:09:26 +00:00
};
2024-06-12 18:04:04 +00:00
2024-02-12 19:09:26 +00:00
for (const key in this.options.envMapping) {
2024-06-12 18:04:04 +00:00
await processEnvMapping(key as keyof T, this.options.envMapping[key]);
2024-02-09 14:57:32 +00:00
}
}
2023-08-24 08:39:47 +00:00
this.readyDeferred.resolve();
}
/**
2024-06-12 18:04:04 +00:00
* returns a kvstore that resides in appdata
2023-08-24 08:39:47 +00:00
*/
2024-06-12 18:04:04 +00:00
public async getKvStore(): Promise<KeyValueStore<T>> {
2023-08-24 08:39:47 +00:00
await this.readyDeferred.promise;
return this.kvStore;
}
2024-02-09 10:52:30 +00:00
2024-06-12 18:04:04 +00:00
public async logMissingKeys(): Promise<Array<keyof T>> {
2024-02-09 10:52:30 +00:00
const kvStore = await this.getKvStore();
2024-04-14 00:10:29 +00:00
const missingMandatoryKeys = await kvStore.getMissingMandatoryKeys();
2024-02-09 10:52:30 +00:00
if (missingMandatoryKeys.length > 0) {
2024-02-09 14:57:32 +00:00
console.log(
`The following mandatory keys are missing in the appdata:\n -> ${missingMandatoryKeys.join(
',\n -> '
)}`
);
2024-02-09 10:52:30 +00:00
} else {
console.log('All mandatory keys are present in the appdata');
}
2024-02-13 01:04:04 +00:00
return missingMandatoryKeys;
2024-02-09 10:52:30 +00:00
}
2024-02-10 03:54:00 +00:00
2024-06-12 18:04:04 +00:00
public async waitForAndGetKey<K extends keyof T>(keyArg: K): Promise<T[K] | undefined> {
2024-02-10 03:54:00 +00:00
await this.readyDeferred.promise;
2024-02-10 03:55:50 +00:00
await this.kvStore.waitForKeysPresent([keyArg]);
2024-06-12 18:04:04 +00:00
return this.kvStore.readKey(keyArg);
2024-02-10 03:54:00 +00:00
}
2024-06-12 18:04:04 +00:00
}