MCPcopy Create free account
hub / github.com/LearnPrompt/LearnPrompt / processFile

Function processFile

scripts/dyno_to_new_embed.ts:72–123  ·  view source on GitHub ↗
(filePath: string)

Source from the content-addressed store, hash-verified

70}
71// Function to process a single file
72function processFile(filePath: string) {
73 fs.readFile(filePath, 'utf8', function(err, data) {
74 if (err) throw err;
75
76 // Replace each dyno embed with the new embed
77 let updatedData = data.replace(/<div trydyno-embed(.*?)<\/div>/gs, function(match, embedConfig) {
78 // Create a mapping from the embed properties to the config properties
79 const propertyMapping = {
80 'model': 'model',
81 'prompt': 'prompt',
82 'response': 'output',
83 'tokens': 'maxTokens',
84 'temp': 'temperature',
85 'p': 'topP',
86 'rows': 'boxRows',
87 };
88
89 // Create a new dictionary for each field of the embed
90 let configDict: { [key: string]: string | number } = {};
91 let configItems = embedConfig.match(/(\w+)="(.*?)"/gs);
92 configItems.forEach(item => {
93 let [key, value] = item.replace(/"/g, '').split('=');
94 // Use the property mapping to get the correct config property
95 let configKey = propertyMapping[key];
96 if (['maxTokens', 'temperature', 'topP', 'boxRows'].includes(configKey)) {
97 configDict[configKey] = Number(value);
98 } else {
99 configDict[configKey] = value;
100 }
101 });
102
103 // Validate and create UrlConfig
104 let validatedConfig;
105 try {
106 validatedConfig = urlConfigSchema.validateSync(configDict);
107 } catch (error) {
108 console.log('Error in validation:', error);
109 return match; // If validation fails, return the original embed
110 }
111
112 // Here you can use validatedConfig which is of type UrlConfig
113 const jsxCode = createEmbedCode(validatedConfig);
114 console.log(filePath, validatedConfig, jsxCode);
115
116 // Return the new embed to replace the old one
117 return jsxCode;
118 });
119
120 // Write the updated content back to the file
121 fs.writeFileSync(filePath, updatedData, 'utf8');
122 });
123}
124
125processDirectory('../i18n');

Callers 1

processDirectoryFunction · 0.85

Calls 1

createEmbedCodeFunction · 0.85

Tested by

no test coverage detected