Skip to content

Commit

Permalink
Added parse logic in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya18101 committed Dec 6, 2024
1 parent b442561 commit 2874e66
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/context/directory/handlers/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ function parse(context: DirectoryContext): ParsedPrompts {
const templateFilePath = path.join(promptsDirectory, template);
insertionAcc[name] = isFile(templateFilePath)
? loadFileAndReplaceKeywords(templateFilePath, {
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
}).trim()
mappings: context.mappings,
disableKeywordReplacement: context.disableKeywordReplacement,
}).trim()
: '';
return insertionAcc;
},
Expand Down
51 changes: 49 additions & 2 deletions src/context/yaml/handlers/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from 'path';
import {ensureDirSync } from 'fs-extra';
import { ensureDirSync } from 'fs-extra';
import fs from 'fs';
import { GetRendering200Response } from 'auth0';
import { YAMLHandler } from '.';
import YAMLContext from '..';
import { constants } from '../../../tools';
import { ParsedAsset } from '../../../types';
import { Prompts } from '../../../tools/auth0/handlers/prompts';
import { existsMustBeDir } from '../../../utils';

const getPromptsDirectory = (filePath: string) => path.join(filePath, constants.PROMPTS_DIRECTORY);

Expand All @@ -18,11 +20,56 @@ type ScreenRenderArray = Array<{
}
}>;

const loadScreenRenderers = (screenRenderArray: ScreenRenderArray, inputDir: string): GetRendering200Response[] => {
// Array to store loaded renderers
const loadedRenderers: GetRendering200Response[] = [];

// Iterate through each entry in the ScreenRenderArray
screenRenderArray.forEach(promptEntry => {
// Get the prompt (there will be only one key in each entry)
const prompt = Object.keys(promptEntry)[0];

// Get the screens for this prompt
const screens = promptEntry[prompt];

// Iterate through each screen for this prompt
Object.entries(screens).forEach(([, fileName]) => {
// Construct full file path
const filePath = fileName;

try {
// Read and parse the JSON file
const fileContent = fs.readFileSync(filePath, 'utf8');
const rendererData = JSON.parse(fileContent);

// Add to the loadedRenderers array
loadedRenderers.push(rendererData);
} catch (error) {
console.error(`Error loading file ${fileName}:`, error);
}
});
});

return loadedRenderers;
};

async function parse(context: YAMLContext): Promise<ParsedPrompts> {
const { prompts } = context.assets;

if (!prompts) return { prompts: null };

const promptsDirectory = getPromptsDirectory(context.basePath);
const renderSettingsDir = path.join(promptsDirectory, 'renderSettings');

if (!existsMustBeDir(renderSettingsDir)) {
prompts.screenRenderers = [];
return { prompts: null };
} // Skip

const a = prompts.screenRenderers as ScreenRenderArray;
console.log(a);

prompts.screenRenderers = loadScreenRenderers(a,renderSettingsDir);

return {
prompts,
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/auth0/handlers/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ export default class PromptsHandler extends DefaultHandler {
}

async updateScreenRenderer(screenRenderer: ScreenRenderer): Promise<void> {
const { prompt, screen, rendering_mode, ...updatePrams } = screenRenderer;
const { prompt, screen, rendering_mode, tenant, ...updatePrams } = screenRenderer;

if (!prompt || !screen) return;

Expand Down

0 comments on commit 2874e66

Please sign in to comment.