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-02-07 20:44:00 +00:00
|
|
|
export interface IAppDataOptions {
|
|
|
|
dirPath?: string;
|
|
|
|
requiredKeys?: string[];
|
2024-02-09 14:57:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* kvStoreKey: 'MY_ENV_VAR'
|
|
|
|
*/
|
|
|
|
envMapping?: {
|
2024-02-12 19:09:26 +00:00
|
|
|
[key: string]: string | object;
|
2024-02-09 14:57:32 +00:00
|
|
|
};
|
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-02-12 23:48:44 +00:00
|
|
|
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
|
|
|
|
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
|
2023-08-24 08:39:47 +00:00
|
|
|
public readyDeferred = plugins.smartpromise.defer();
|
2024-02-07 20:44:00 +00:00
|
|
|
public options: IAppDataOptions;
|
2024-02-12 23:50:50 +00:00
|
|
|
private kvStore: KeyValueStore<T>;
|
2024-02-07 20:44:00 +00:00
|
|
|
constructor(optionsArg: IAppDataOptions = {}) {
|
|
|
|
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-02-07 20:44:00 +00:00
|
|
|
if (this.options.dirPath) {
|
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-02-12 18:16:43 +00:00
|
|
|
this.kvStore = new KeyValueStore({
|
|
|
|
typeArg: 'custom',
|
|
|
|
identityArg: 'appkv',
|
|
|
|
customPath: this.options.dirPath,
|
|
|
|
mandatoryKeys: this.options.requiredKeys
|
|
|
|
});
|
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-02-12 19:09:26 +00:00
|
|
|
|
|
|
|
// Recursive function to handle nested objects, now includes key parameter
|
|
|
|
const processEnvMapping = async (key: string, mappingValue: any, parentKey: string = ''): Promise<any> => {
|
|
|
|
if (typeof mappingValue === 'string') {
|
2024-02-13 01:08:59 +00:00
|
|
|
let envValue: string;
|
|
|
|
if (mappingValue.startsWith('hard:')) {
|
|
|
|
envValue = mappingValue.replace('hard:', '');
|
|
|
|
} else {
|
|
|
|
envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
|
|
|
|
}
|
2024-02-12 19:09:26 +00:00
|
|
|
if (envValue) {
|
|
|
|
if (mappingValue.endsWith('_JSON')) {
|
|
|
|
envValue = JSON.parse(envValue);
|
|
|
|
}
|
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) {
|
|
|
|
const resultObject = {};
|
|
|
|
for (const innerKey in mappingValue) {
|
|
|
|
const nestedValue = mappingValue[innerKey];
|
|
|
|
// For nested objects, call recursively but do not immediately write to kvStore
|
|
|
|
const nestedResult = await processEnvMapping(innerKey, nestedValue, key);
|
|
|
|
resultObject[innerKey] = nestedResult;
|
|
|
|
}
|
|
|
|
if (parentKey === '') {
|
|
|
|
// Only write to kvStore if at the top level
|
2024-02-13 00:46:22 +00:00
|
|
|
await this.kvStore.writeKey(key, resultObject);
|
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
|
|
|
};
|
|
|
|
|
|
|
|
for (const key in this.options.envMapping) {
|
|
|
|
await processEnvMapping(key, this.options.envMapping[key]);
|
2024-02-09 14:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-12 19:09:26 +00:00
|
|
|
|
|
|
|
|
2024-02-09 14:57:32 +00:00
|
|
|
|
2023-08-24 08:39:47 +00:00
|
|
|
this.readyDeferred.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a kvtore that resides in appdata
|
|
|
|
*/
|
|
|
|
public async getKvStore() {
|
|
|
|
await this.readyDeferred.promise;
|
|
|
|
return this.kvStore;
|
|
|
|
}
|
2024-02-09 10:52:30 +00:00
|
|
|
|
|
|
|
public async logMissingKeys() {
|
|
|
|
const kvStore = await this.getKvStore();
|
|
|
|
const missingMandatoryKeys = kvStore.getMissingMandatoryKeys();
|
|
|
|
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-02-10 03:55:50 +00:00
|
|
|
public async waitForAndGetKey(keyArg: string) {
|
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]);
|
|
|
|
return this.kvStore.readKey[keyArg];
|
2024-02-10 03:54:00 +00:00
|
|
|
}
|
2024-02-09 14:57:32 +00:00
|
|
|
}
|