Best practice content dir generation

pull/5388/head
Kamran Ahmed 8 months ago
parent cdb9a87c85
commit 2ab7690271
  1. 43
      scripts/best-practice-content.cjs

@ -2,14 +2,7 @@ const fs = require('fs');
const path = require('path'); const path = require('path');
const OPEN_AI_API_KEY = process.env.OPEN_AI_API_KEY; const OPEN_AI_API_KEY = process.env.OPEN_AI_API_KEY;
const ALL_BEST_PRACTICES_DIR = path.join( const ALL_BEST_PRACTICES_DIR = path.join(__dirname, '../src/data/best-practices');
__dirname,
'../src/data/best-practices'
);
const BEST_PRACTICE_JSON_DIR = path.join(
__dirname,
'../public/jsons/best-practices'
);
const bestPracticeId = process.argv[2]; const bestPracticeId = process.argv[2];
const bestPracticeTitle = bestPracticeId.replace(/-/g, ' '); const bestPracticeTitle = bestPracticeId.replace(/-/g, ' ');
@ -26,18 +19,13 @@ if (!allowedBestPracticeIds.includes(bestPracticeId)) {
process.exit(1); process.exit(1);
} }
const BEST_PRACTICE_CONTENT_DIR = path.join( const BEST_PRACTICE_CONTENT_DIR = path.join(ALL_BEST_PRACTICES_DIR, bestPracticeId, 'content');
ALL_BEST_PRACTICES_DIR, const OpenAI = require('openai');
bestPracticeId,
'content' const openai = new OpenAI({
);
const { Configuration, OpenAIApi } = require('openai');
const configuration = new Configuration({
apiKey: OPEN_AI_API_KEY, apiKey: OPEN_AI_API_KEY,
}); });
const openai = new OpenAIApi(configuration);
function getFilesInFolder(folderPath, fileList = {}) { function getFilesInFolder(folderPath, fileList = {}) {
const files = fs.readdirSync(folderPath); const files = fs.readdirSync(folderPath);
@ -67,8 +55,8 @@ function writeTopicContent(topicTitle) {
console.log(`Generating '${topicTitle}'...`); console.log(`Generating '${topicTitle}'...`);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
openai openai.chat.completions
.createChatCompletion({ .create({
model: 'gpt-4', model: 'gpt-4',
messages: [ messages: [
{ {
@ -78,7 +66,7 @@ function writeTopicContent(topicTitle) {
], ],
}) })
.then((response) => { .then((response) => {
const article = response.data.choices[0].message.content; const article = response.choices[0].message.content;
resolve(article); resolve(article);
}) })
@ -91,7 +79,7 @@ function writeTopicContent(topicTitle) {
async function writeFileForGroup(group, topicUrlToPathMapping) { async function writeFileForGroup(group, topicUrlToPathMapping) {
const topicId = group?.properties?.controlName; const topicId = group?.properties?.controlName;
const topicTitle = group?.children?.controls?.control?.find( const topicTitle = group?.children?.controls?.control?.find(
(control) => control?.typeID === 'Label' (control) => control?.typeID === 'Label',
)?.properties?.text; )?.properties?.text;
const currTopicUrl = `/${topicId}`; const currTopicUrl = `/${topicId}`;
if (currTopicUrl.startsWith('/check:')) { if (currTopicUrl.startsWith('/check:')) {
@ -102,7 +90,6 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
if (!contentFilePath) { if (!contentFilePath) {
console.log(`Missing file for: ${currTopicUrl}`); console.log(`Missing file for: ${currTopicUrl}`);
process.exit(0);
return; return;
} }
@ -123,7 +110,7 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
return; return;
} }
const topicContent = await writeTopicContent(topicTitle); const topicContent = await writeTopicContent(currTopicUrl);
newFileContent += `\n\n${topicContent}`; newFileContent += `\n\n${topicContent}`;
console.log(`Writing ${topicId}..`); console.log(`Writing ${topicId}..`);
@ -138,14 +125,14 @@ async function writeFileForGroup(group, topicUrlToPathMapping) {
async function run() { async function run() {
const topicUrlToPathMapping = getFilesInFolder(BEST_PRACTICE_CONTENT_DIR); const topicUrlToPathMapping = getFilesInFolder(BEST_PRACTICE_CONTENT_DIR);
const bestPracticeJson = require(path.join( const bestPracticeJson = require(
BEST_PRACTICE_JSON_DIR, path.join(ALL_BEST_PRACTICES_DIR, `${bestPracticeId}/${bestPracticeId}`),
`${bestPracticeId}.json` );
));
const groups = bestPracticeJson?.mockup?.controls?.control?.filter( const groups = bestPracticeJson?.mockup?.controls?.control?.filter(
(control) => (control) =>
control.typeID === '__group__' && control.typeID === '__group__' &&
!control.properties?.controlName?.startsWith('ext_link') !control.properties?.controlName?.startsWith('ext_link'),
); );
if (!OPEN_AI_API_KEY) { if (!OPEN_AI_API_KEY) {

Loading…
Cancel
Save