|
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 listStartTag = '<!-- LIST:START -->' |
| 7 | +const listEndTag = '<!-- LIST:END -->' |
5 | 8 |
|
6 | 9 | const projectStatus = ['incubating', 'active', 'retiring']
|
7 | 10 | const implementationPriority = ['expected', 'deferrable', 'recommended']
|
8 | 11 | const data = {}
|
9 | 12 | const files = {}
|
| 13 | +// @TODO: Move this function to a shared file |
10 | 14 | const capitalizeWords = str => str.split(' ').map(w => w[0].toUpperCase() + w.slice(1).toLowerCase()).join(' ')
|
| 15 | +// @TODO: Move this function to a shared file |
| 16 | +const replaceMetadata = (fileContent, metadata) => { |
| 17 | + return fileContent.replace(/---[^]*?---/, metadata) |
| 18 | +} |
11 | 19 |
|
12 | 20 | // Basic structure of the data object
|
13 | 21 | projectStatus.forEach(status => {
|
@@ -52,17 +60,31 @@ slug: /implementations/${status}
|
52 | 60 | ## ${priority.charAt(0).toUpperCase() + priority.slice(1)}
|
53 | 61 | ${addHeader()}
|
54 | 62 | ${data[status][priority].map(addRow).join('\n')}
|
55 |
| - ` |
| 63 | +` |
56 | 64 | }).join('\n')
|
57 | 65 |
|
58 |
| - const fileContent = `${metadata} |
59 |
| -<!-- LIST:START --> |
| 66 | + let fileContent = `${metadata} |
60 | 67 |
|
61 |
| -<!-- LIST:END --> |
| 68 | +${listStartTag} |
62 | 69 | ${listContent}
|
63 |
| -<!-- LIST:END --> |
| 70 | +${listEndTag} |
64 | 71 | `
|
| 72 | + const updateContent = (currentContent) => { |
| 73 | + fileContent = currentContent |
| 74 | + replaceMetadata(fileContent, metadata) |
| 75 | + fileContent = updateOrCreateSegment({ |
| 76 | + original: fileContent, |
| 77 | + replacementSegment: listContent, |
| 78 | + startTag: listStartTag, |
| 79 | + endTag: listEndTag |
| 80 | + }) |
| 81 | + } |
65 | 82 |
|
66 | 83 | const destination = path.join(process.cwd(), `docs/implementation/${status}.mdx`)
|
| 84 | + const fileExists = existsSync(destination) |
| 85 | + if (fileExists) { |
| 86 | + const currentFileContent = readFileSync(destination, 'utf8') |
| 87 | + updateContent(currentFileContent) |
| 88 | + } |
67 | 89 | writeFileSync(destination, fileContent)
|
68 | 90 | })
|
0 commit comments