|
1 |
| -const { writeFileSync } = require('fs') |
| 1 | +const { writeFileSync, existsSync, readFileSync } = require('fs') |
| 2 | +const { updateOrCreateSegment } = require('@ulisesgascon/text-tags-manager') |
2 | 3 | const path = require('path')
|
3 | 4 |
|
4 | 5 | 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 | +} |
5 | 16 |
|
6 | 17 | const addImplementationDetails = (check) => {
|
7 | 18 | if (!check.implementation_type) {
|
@@ -69,21 +80,49 @@ slug: /details/${check.code_name}
|
69 | 80 | ${check.description}`.trim()
|
70 | 81 | const detailsContent = renderDetails(check)
|
71 | 82 |
|
72 |
| - const fileContent = `${metadata} |
| 83 | + let fileContent = `${metadata} |
73 | 84 |
|
74 | 85 | ## Use Case
|
75 |
| -<!-- LEVELS:START --> |
| 86 | +${levelsStartTag} |
76 | 87 | ${levelsContent}
|
77 |
| -<!-- LEVELS:END --> |
| 88 | +${levelsEndTag} |
78 | 89 |
|
79 |
| -<!-- DESCRIPTION:START --> |
| 90 | +${descriptionStartTag} |
80 | 91 | ${descriptionContent}
|
81 |
| -<!-- DESCRIPTION:END --> |
| 92 | +${descriptionEndTag} |
82 | 93 |
|
83 |
| -<!-- DETAILS:START --> |
| 94 | +${detailsStartTag} |
| 95 | +${detailsContent} |
84 | 96 | ${detailsContent}
|
85 |
| -<!-- DETAILS:END --> |
86 | 97 | `
|
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) |
89 | 128 | })
|
0 commit comments