|
|
@ -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); |
|
|
|
|
|
|
|
|
|
|
@ -49,10 +37,10 @@ function getFilesInFolder(folderPath, fileList = {}) { |
|
|
|
getFilesInFolder(filePath, fileList); |
|
|
|
getFilesInFolder(filePath, fileList); |
|
|
|
} else if (stats.isFile()) { |
|
|
|
} else if (stats.isFile()) { |
|
|
|
const fileUrl = filePath |
|
|
|
const fileUrl = filePath |
|
|
|
.replace(BEST_PRACTICE_CONTENT_DIR, '') // Remove the content folder |
|
|
|
.replace(BEST_PRACTICE_CONTENT_DIR, '') // Remove the content folder |
|
|
|
.replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem` |
|
|
|
.replace(/\/\d+-/g, '/') // Remove ordering info `/101-ecosystem` |
|
|
|
.replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only |
|
|
|
.replace(/\/index\.md$/, '') // Make the `/index.md` to become the parent folder only |
|
|
|
.replace(/\.md$/, ''); // Remove `.md` from the end of file |
|
|
|
.replace(/\.md$/, ''); // Remove `.md` from the end of file |
|
|
|
|
|
|
|
|
|
|
|
fileList[fileUrl] = filePath; |
|
|
|
fileList[fileUrl] = filePath; |
|
|
|
} |
|
|
|
} |
|
|
@ -67,31 +55,31 @@ 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: [ |
|
|
|
{ |
|
|
|
{ |
|
|
|
role: 'user', |
|
|
|
role: 'user', |
|
|
|
content: prompt, |
|
|
|
content: prompt, |
|
|
|
}, |
|
|
|
}, |
|
|
|
], |
|
|
|
], |
|
|
|
}) |
|
|
|
}) |
|
|
|
.then((response) => { |
|
|
|
.then((response) => { |
|
|
|
const article = response.data.choices[0].message.content; |
|
|
|
const article = response.choices[0].message.content; |
|
|
|
|
|
|
|
|
|
|
|
resolve(article); |
|
|
|
resolve(article); |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((err) => { |
|
|
|
.catch((err) => { |
|
|
|
reject(err); |
|
|
|
reject(err); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
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) { |
|
|
@ -164,10 +151,10 @@ async function run() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
run() |
|
|
|
run() |
|
|
|
.then(() => { |
|
|
|
.then(() => { |
|
|
|
console.log('Done'); |
|
|
|
console.log('Done'); |
|
|
|
}) |
|
|
|
}) |
|
|
|
.catch((err) => { |
|
|
|
.catch((err) => { |
|
|
|
console.error(err); |
|
|
|
console.error(err); |
|
|
|
process.exit(1); |
|
|
|
process.exit(1); |
|
|
|
}); |
|
|
|
}); |
|
|
|