diff --git a/scripts/populate-implementations.js b/scripts/populate-implementations.js index 7bb5c0b..edea85b 100644 --- a/scripts/populate-implementations.js +++ b/scripts/populate-implementations.js @@ -1,13 +1,21 @@ -const { writeFileSync } = require('fs') +const { writeFileSync, existsSync, readFileSync } = require('fs') +const { updateOrCreateSegment } = require('@ulisesgascon/text-tags-manager') const path = require('path') const checks = require('../data/checks.json') +const listStartTag = '' +const listEndTag = '' const projectStatus = ['incubating', 'active', 'retiring'] const implementationPriority = ['expected', 'deferrable', 'recommended'] const data = {} const files = {} +// @TODO: Move this function to a shared file const capitalizeWords = str => str.split(' ').map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join(' ') +// @TODO: Move this function to a shared file +const replaceMetadata = (fileContent, metadata) => { + return fileContent.replace(/---[^]*?---/, metadata) +} // Basic structure of the data object projectStatus.forEach(status => { @@ -52,17 +60,31 @@ slug: /implementations/${status} ## ${priority.charAt(0).toUpperCase() + priority.slice(1)} ${addHeader()} ${data[status][priority].map(addRow).join('\n')} - ` +` }).join('\n') - const fileContent = `${metadata} - + let fileContent = `${metadata} - +${listStartTag} ${listContent} - +${listEndTag} ` + const updateContent = (currentContent) => { + fileContent = currentContent + replaceMetadata(fileContent, metadata) + fileContent = updateOrCreateSegment({ + original: fileContent, + replacementSegment: listContent, + startTag: listStartTag, + endTag: listEndTag + }) + } const destination = path.join(process.cwd(), `docs/implementation/${status}.mdx`) + const fileExists = existsSync(destination) + if (fileExists) { + const currentFileContent = readFileSync(destination, 'utf8') + updateContent(currentFileContent) + } writeFileSync(destination, fileContent) })