diff --git a/scripts/best-practice-content.cjs b/scripts/best-practice-content.cjs index 439f4a8cc..b9dec21e3 100644 --- a/scripts/best-practice-content.cjs +++ b/scripts/best-practice-content.cjs @@ -2,14 +2,7 @@ const fs = require('fs'); const path = require('path'); const OPEN_AI_API_KEY = process.env.OPEN_AI_API_KEY; -const ALL_BEST_PRACTICES_DIR = path.join( - __dirname, - '../src/data/best-practices' -); -const BEST_PRACTICE_JSON_DIR = path.join( - __dirname, - '../public/jsons/best-practices' -); +const ALL_BEST_PRACTICES_DIR = path.join(__dirname, '../src/data/best-practices'); const bestPracticeId = process.argv[2]; const bestPracticeTitle = bestPracticeId.replace(/-/g, ' '); @@ -26,18 +19,13 @@ if (!allowedBestPracticeIds.includes(bestPracticeId)) { process.exit(1); } -const BEST_PRACTICE_CONTENT_DIR = path.join( - ALL_BEST_PRACTICES_DIR, - bestPracticeId, - 'content' -); -const { Configuration, OpenAIApi } = require('openai'); -const configuration = new Configuration({ +const BEST_PRACTICE_CONTENT_DIR = path.join(ALL_BEST_PRACTICES_DIR, bestPracticeId, 'content'); +const OpenAI = require('openai'); + +const openai = new OpenAI({ apiKey: OPEN_AI_API_KEY, }); -const openai = new OpenAIApi(configuration); - function getFilesInFolder(folderPath, fileList = {}) { const files = fs.readdirSync(folderPath); @@ -49,10 +37,10 @@ function getFilesInFolder(folderPath, fileList = {}) { getFilesInFolder(filePath, fileList); } else if (stats.isFile()) { const fileUrl = filePath - .replace(BEST_PRACTICE_CONTENT_DIR, '') // Remove the content folder - .replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem` - .replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only - .replace(/\.md$/, ''); // Remove `.md` from the end of file + .replace(BEST_PRACTICE_CONTENT_DIR, '') // Remove the content folder + .replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem` + .replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only + .replace(/\.md$/, ''); // Remove `.md` from the end of file fileList[fileUrl] = filePath; } @@ -67,31 +55,31 @@ function writeTopicContent(topicTitle) { console.log(`Generating '${topicTitle}'...`); return new Promise((resolve, reject) => { - openai - .createChatCompletion({ - model: 'gpt-4', - messages: [ - { - role: 'user', - content: prompt, - }, - ], - }) - .then((response) => { - const article = response.data.choices[0].message.content; + openai.chat.completions + .create({ + model: 'gpt-4', + messages: [ + { + role: 'user', + content: prompt, + }, + ], + }) + .then((response) => { + const article = response.choices[0].message.content; - resolve(article); - }) - .catch((err) => { - reject(err); - }); + resolve(article); + }) + .catch((err) => { + reject(err); + }); }); } async function writeFileForGroup(group, topicUrlToPathMapping) { const topicId = group?.properties?.controlName; const topicTitle = group?.children?.controls?.control?.find( - (control) => control?.typeID === 'Label' + (control) => control?.typeID === 'Label', )?.properties?.text; const currTopicUrl = `/${topicId}`; if (currTopicUrl.startsWith('/check:')) { @@ -102,7 +90,6 @@ async function writeFileForGroup(group, topicUrlToPathMapping) { if (!contentFilePath) { console.log(`Missing file for: ${currTopicUrl}`); - process.exit(0); return; } @@ -123,7 +110,7 @@ async function writeFileForGroup(group, topicUrlToPathMapping) { return; } - const topicContent = await writeTopicContent(topicTitle); + const topicContent = await writeTopicContent(currTopicUrl); newFileContent += `\n\n${topicContent}`; console.log(`Writing ${topicId}..`); @@ -138,14 +125,14 @@ async function writeFileForGroup(group, topicUrlToPathMapping) { async function run() { const topicUrlToPathMapping = getFilesInFolder(BEST_PRACTICE_CONTENT_DIR); - const bestPracticeJson = require(path.join( - BEST_PRACTICE_JSON_DIR, - `${bestPracticeId}.json` - )); + const bestPracticeJson = require( + path.join(ALL_BEST_PRACTICES_DIR, `${bestPracticeId}/${bestPracticeId}`), + ); + const groups = bestPracticeJson?.mockup?.controls?.control?.filter( - (control) => - control.typeID === '__group__' && - !control.properties?.controlName?.startsWith('ext_link') + (control) => + control.typeID === '__group__' && + !control.properties?.controlName?.startsWith('ext_link'), ); if (!OPEN_AI_API_KEY) { @@ -164,10 +151,10 @@ async function run() { } run() - .then(() => { - console.log('Done'); - }) - .catch((err) => { - console.error(err); - process.exit(1); - }); + .then(() => { + console.log('Done'); + }) + .catch((err) => { + console.error(err); + process.exit(1); + });