|
|
|
@ -11,19 +11,19 @@ export interface IAppDataOptions {
|
|
|
|
|
* kvStoreKey: 'MY_ENV_VAR'
|
|
|
|
|
*/
|
|
|
|
|
envMapping?: {
|
|
|
|
|
[key: string]: string;
|
|
|
|
|
[key: string]: string | object;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class AppData {
|
|
|
|
|
export class AppData<T = any> {
|
|
|
|
|
/**
|
|
|
|
|
* creates appdata. If no pathArg is given, data will be stored here:
|
|
|
|
|
* ${PWD}/.nogit/appdata
|
|
|
|
|
* @param pathArg
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
public static async createAndInit(optionsArg: IAppDataOptions = {}) {
|
|
|
|
|
const appData = new AppData(optionsArg);
|
|
|
|
|
public static async createAndInit<T = any>(optionsArg: IAppDataOptions = {}): Promise<AppData<T>> {
|
|
|
|
|
const appData = new AppData<T>(optionsArg);
|
|
|
|
|
await appData.readyDeferred.promise;
|
|
|
|
|
return appData;
|
|
|
|
|
}
|
|
|
|
@ -67,17 +67,44 @@ export class AppData {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (this.options.envMapping) {
|
|
|
|
|
const qenvInstance = new plugins.qenv.Qenv(process.cwd(), '~/.cloudlyrc');
|
|
|
|
|
for (const key in this.options.envMapping) {
|
|
|
|
|
let envValue = await qenvInstance.getEnvVarOnDemand(key);
|
|
|
|
|
if (envValue) {
|
|
|
|
|
if (key.endsWith('_JSON')) {
|
|
|
|
|
envValue = JSON.parse(envValue);
|
|
|
|
|
const qenvInstance = new plugins.qenv.Qenv();
|
|
|
|
|
|
|
|
|
|
// 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') {
|
|
|
|
|
let envValue = await qenvInstance.getEnvVarOnDemand(mappingValue);
|
|
|
|
|
if (envValue) {
|
|
|
|
|
if (mappingValue.endsWith('_JSON')) {
|
|
|
|
|
envValue = JSON.parse(envValue);
|
|
|
|
|
}
|
|
|
|
|
// Determine the correct key to use (top-level or nested)
|
|
|
|
|
const effectiveKey = parentKey || key;
|
|
|
|
|
this.kvStore.writeKey(effectiveKey, envValue);
|
|
|
|
|
}
|
|
|
|
|
} 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
|
|
|
|
|
this.kvStore.writeKey(key, resultObject);
|
|
|
|
|
} else {
|
|
|
|
|
// For nested objects, return the constructed object instead of writing to kvStore
|
|
|
|
|
return resultObject;
|
|
|
|
|
}
|
|
|
|
|
await this.kvStore.writeKey(key, envValue);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const key in this.options.envMapping) {
|
|
|
|
|
await processEnvMapping(key, this.options.envMapping[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.readyDeferred.resolve();
|
|
|
|
|
}
|
|
|
|
|