Skip to content
This repository was archived by the owner on Jan 20, 2025. It is now read-only.

Commit 09d66e1

Browse files
committed
feat: add support to update existing details and keep manual additions
1 parent bc826dc commit 09d66e1

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

scripts/populate-details.js

+49-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
const { writeFileSync } = require('fs')
1+
const { writeFileSync, existsSync, readFileSync } = require('fs')
2+
const { updateOrCreateSegment } = require('@ulisesgascon/text-tags-manager')
23
const path = require('path')
34

45
const checks = require('../data/checks.json')
6+
const levelsStartTag = '<!-- LEVELS:START -->'
7+
const levelsEndTag = '<!-- LEVELS:END -->'
8+
const descriptionStartTag = '<!-- DESCRIPTION:START -->'
9+
const descriptionEndTag = '<!-- DESCRIPTION:END -->'
10+
const detailsStartTag = '<!-- DETAILS:START -->'
11+
const detailsEndTag = '<!-- DETAILS:END -->'
12+
// @TODO: Move this function to a shared file
13+
const replaceMetadata = (fileContent, metadata) => {
14+
return fileContent.replace(/---[^]*?---/, metadata)
15+
}
516

617
const addImplementationDetails = (check) => {
718
if (!check.implementation_type) {
@@ -69,21 +80,49 @@ slug: /details/${check.code_name}
6980
${check.description}`.trim()
7081
const detailsContent = renderDetails(check)
7182

72-
const fileContent = `${metadata}
83+
let fileContent = `${metadata}
7384
7485
## Use Case
75-
<!-- LEVELS:START -->
86+
${levelsStartTag}
7687
${levelsContent}
77-
<!-- LEVELS:END -->
88+
${levelsEndTag}
7889
79-
<!-- DESCRIPTION:START -->
90+
${descriptionStartTag}
8091
${descriptionContent}
81-
<!-- DESCRIPTION:END -->
92+
${descriptionEndTag}
8293
83-
<!-- DETAILS:START -->
94+
${detailsStartTag}
95+
${detailsContent}
8496
${detailsContent}
85-
<!-- DETAILS:END -->
8697
`
87-
const detination = path.join(process.cwd(), `docs/details/${check.code_name}.mdx`)
88-
writeFileSync(detination, fileContent)
98+
const updateContent = (currentContent) => {
99+
fileContent = currentContent
100+
replaceMetadata(fileContent, metadata)
101+
fileContent = updateOrCreateSegment({
102+
original: fileContent,
103+
replacementSegment: levelsContent,
104+
startTag: levelsStartTag,
105+
endTag: levelsEndTag
106+
})
107+
fileContent = updateOrCreateSegment({
108+
original: fileContent,
109+
replacementSegment: descriptionContent,
110+
startTag: descriptionStartTag,
111+
endTag: descriptionEndTag
112+
})
113+
fileContent = updateOrCreateSegment({
114+
original: fileContent,
115+
replacementSegment: detailsContent,
116+
startTag: detailsStartTag,
117+
endTag: detailsEndTag
118+
})
119+
}
120+
121+
const destination = path.join(process.cwd(), `docs/details/${check.code_name}.mdx`)
122+
const fileExists = existsSync(destination)
123+
if (fileExists) {
124+
const currentFileContent = readFileSync(destination, 'utf8')
125+
updateContent(currentFileContent)
126+
}
127+
writeFileSync(destination, fileContent)
89128
})

0 commit comments

Comments
 (0)