-
Notifications
You must be signed in to change notification settings - Fork 5
/
add-readmes.js
169 lines (144 loc) · 4.77 KB
/
add-readmes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { Command } = require('commander')
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const mkdirp = require('mkdirp')
const { exec } = require('child_process')
// Add command line arguments for the root to NUbots and NUbook
const program = new Command()
program
.option(
'--main_codebase_path <path>',
'Relative path to the main codebase folder',
'../NUbots/'
)
.option('--nubook_path <path>', 'Relative path to the NUbook folder', '.')
program.parse(process.argv)
const options = program.opts()
// Constants for MDX content
const READMES_PATH = './src/book/02-system/05-modules/'
const IMAGES_PATH = path.join(READMES_PATH, 'images')
const MD_CONTENT_TEMPLATE = `---
section: System
chapter: Modules
title: {title}
description: Documentation for each {description} module in the main NUbots codebase
slug: {slug}
---
`
function isEmptyReadme(content) {
// Check if the content only contains headings and/or empty lines
const lines = content.split('\n')
return lines.every(
(line) => line.trim() === '' || line.trim().startsWith('#')
)
}
function copyImagesAndAdjustPaths(mdContent, readmeDir) {
const imageRegex = /!\[.*?\]\((.*?)\)/g
let match
let adjustedContent = mdContent
while ((match = imageRegex.exec(mdContent)) !== null) {
const imagePath = match[1]
const imageName = path.basename(imagePath)
const sourceImagePath = path.join(readmeDir, imageName)
const destImagePath = path.join(IMAGES_PATH, imageName)
if (fs.existsSync(sourceImagePath)) {
mkdirp.sync(IMAGES_PATH)
fs.copyFileSync(sourceImagePath, destImagePath)
adjustedContent = adjustedContent.replace(
imagePath,
`images/${imageName}`
)
}
}
return adjustedContent
}
function processReadmesInSubfolder(subfolder, index) {
// Find all README.md files in the given subfolder (including nested subfolders)
const readmeFiles = glob.sync(`${subfolder}/**/README.md`)
if (readmeFiles.length === 0) {
console.log(`No README.md files found in ${subfolder}.`)
return
}
// Concatenate the contents of all non-empty README.md files
let concatenatedContent = ''
readmeFiles.forEach((file) => {
const mdContent = fs.readFileSync(file, 'utf-8')
if (!isEmptyReadme(mdContent)) {
const readmeDir = path.dirname(file)
const adjustedContent = copyImagesAndAdjustPaths(
mdContent,
readmeDir
).replace(/^(#+)/gm, '#$1')
concatenatedContent += adjustedContent + '\n'
} else {
console.log(`Skipping empty README.md file: ${file}`)
}
})
if (concatenatedContent === '') {
console.log(`All README.md files in ${subfolder} are empty. Skipping.`)
return
}
// Extract the subfolder name
const subfolderName = path.basename(subfolder)
const slug = `/system/modules/${subfolderName}/`
// The header content for the MDX file
const markdownContent = MD_CONTENT_TEMPLATE.replace('{chapter}', 'Modules')
.replace(
'{title}',
subfolderName.charAt(0).toUpperCase() + subfolderName.slice(1)
)
.replace('{description}', subfolderName)
.replace('{slug}', slug)
// The path of the mdx file this readme will be added to
const mdxFilePath = path.join(
READMES_PATH,
`${String(index).padStart(2, '0')}-${subfolderName}.mdx`
)
console.log(mdxFilePath)
// Write the content to the MDX file, overriding any existing file
mkdirp.sync(path.dirname(mdxFilePath))
fs.writeFileSync(mdxFilePath, markdownContent + concatenatedContent)
}
function findImmediateSubfolders(parentPath) {
// Find all immediate subfolders in the given parent path
return fs
.readdirSync(parentPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => path.join(parentPath, dirent.name))
}
function processModules() {
const modulesPath = path.join(options.main_codebase_path, 'module')
const subfolders = findImmediateSubfolders(modulesPath)
if (subfolders.length === 0) {
console.log('No subfolders found in the modules directory.')
return
}
mkdirp.sync(path.join(READMES_PATH, options.nubook_path))
subfolders.forEach((subfolder, index) => {
// Skip the root directory itself
if (subfolder === modulesPath) {
return
}
processReadmesInSubfolder(subfolder, index + 1)
})
}
function runYarnFormat() {
exec('yarn format', (error, stdout, stderr) => {
if (error) {
console.error(`Error running yarn format: ${error.message}`)
return
}
if (stderr) {
console.error(`yarn format stderr: ${stderr}`)
return
}
console.log(`yarn format stdout: ${stdout}`)
})
}
// Create the READMES_PATH directory if it doesn't exist
mkdirp.sync(READMES_PATH)
// Process all modules
processModules()
// Run yarn format at the end
runYarnFormat()