-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbuild-plugin-list.js
82 lines (67 loc) · 2.26 KB
/
build-plugin-list.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
'use strict'
const { promises: fs } = require('fs')
const path = require('path')
const log = require('pino')({
level: process.env.LOG_LEVEL || 'debug',
transport: {
target: 'pino-pretty',
options: {
colorize: true,
},
},
})
const LATEST_ECOSYSTEM_FILE = path.join(__dirname, '../versioned_docs/version-latest/Guides/Ecosystem.md')
const OUTPUT_FILE = path.join(__dirname, '../static/generated/plugins.json')
generateEcosystemJson({
ecosystemFile: LATEST_ECOSYSTEM_FILE,
outputFile: OUTPUT_FILE,
})
async function generateEcosystemJson({ ecosystemFile, outputFile }) {
log.info('Generating ecosystem data file from source %s', ecosystemFile)
const plugins = await extractEcosystemFromFile(ecosystemFile)
log.debug('Read the ecosystem file')
await fs.writeFile(outputFile, JSON.stringify(plugins, null, 2))
log.info('Wrote the ecosystem plugin file to %s', outputFile)
}
async function extractEcosystemFromFile(file) {
const content = await fs.readFile(file, 'utf8')
const [, pluginText] = content.split('#### [Core](#core)\n')
const [
corePluginsContent, //
communityPluginsContent, //
] = pluginText.split('#### [Community](#community)')
return {
corePlugins: extractPlugins(corePluginsContent),
communityPlugins: extractPlugins(communityPluginsContent),
}
}
function extractPlugins(pluginContent) {
const lines = pluginContent.split('\n').filter(Boolean) // remove empty lines
// if a line doesn't start with "-" merge it back with the previous item
const mergedLines = lines.reduce((acc, curr) => {
if (curr[0] === '-') {
acc.push(curr)
} else {
acc[acc.length - 1] += ' ' + curr
}
return acc
}, [])
const re = /\[`([-a-zA-Z\d./@]+)`\]\(([^)]+)\)(\s*(.+))?/
const plugins = mergedLines.map((line) => {
const match = re.exec(line)
if (!match) {
throw new Error(
`Invalid entry found in Plugins list (docs/Ecosystem.md): "${line}". This line did not match the expected pattern (${re})`,
)
}
const name = match[1]
const url = match[2]
const description = match[3] ? match[3].trim().replace(/ {2,}/g, '') : ''
return {
name,
url,
description: description.charAt(0).toUpperCase() + description.slice(1),
}
})
return plugins
}