forked from silexlabs/Silex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-submodules-doc.js
More file actions
executable file
·65 lines (59 loc) · 1.95 KB
/
generate-submodules-doc.js
File metadata and controls
executable file
·65 lines (59 loc) · 1.95 KB
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
#!/usr/bin/env node
import { access, readFile } from 'fs/promises';
import ini from 'js-ini';
async function exists(path) {
try {
await access(path)
return true
} catch(e) {
return false
}
}
async function main() {
try {
const data = await readFile('.gitmodules', 'utf-8')
const parsedData = ini.parse(data)
const array = Object.keys(parsedData)
.sort((a, b) => b.localeCompare(a))
.map((key) => ({
// From submodule "submodules/..." to "..."
name: key.replace(/^submodule \"packages\//, '').replace(/\"$/, ''),
...parsedData[key],
}))
let markdown = `
# Silex packages
| Name | Directory | Repo | Description |
| ---- | --------- | ---- | ----------- |
`
let readmeCount = 0
for(const project of array) {
let title = project.name
let description = ''
try {
const readmeFile =
await exists(`${project.path}/README.md`) ? `${project.path}/README.md` :
await exists(`${project.path}/readme.md`) ? `${project.path}/readme.md` :
await exists(`${project.path}/README`) ? `${project.path}/README` : null
if (readmeFile) {
const readme = await readFile(readmeFile, 'utf-8')
readmeCount++
const lines = readme.split('\n')
const titleIndex = lines.findIndex((line) => line.match(/^(#+)/))
title = titleIndex >= 0 ? lines[titleIndex].replace(/^(#+)/, '').trim() : project.name
description = titleIndex >= 0 ? lines.slice(titleIndex + 1).find((line) => line.length > 0) ?? '' : ''
} else {
console.log('Skipping', title, '- No readme file found')
}
} catch (err) {
console.log('Skipping', title)
if(err.code === 'ENOENT') continue
else throw err
}
markdown += `| ${title} | \`packages/${project.name}\` | \`${project.url}\` | ${description} |\n`
}
console.log(markdown)
} catch (err) {
console.error(err)
}
}
main()