-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
build.ts
113 lines (106 loc) · 4.62 KB
/
build.ts
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
import axios from 'axios'
import YAML from 'yaml'
import * as fs from 'fs';
import * as path from 'path';
import { LocalCollection, LocalRepository, LocalSidecar, RemoteIndex, RemotePlugin } from './types'
import { Plugin } from './plugin'
import { infoLog, warnLog } from './utils'
import { debuglog } from 'util';
// iterate over folder
async function searchRepository(pathName: string = "plugins"): Promise<Plugin[]> {
const repoPath = path.resolve(`./repositories/${pathName}`)
const repoFiles = fs.readdirSync(repoPath)
const repositories: LocalRepository[] = []
// find all files
repoFiles.forEach(file => {
if (file.endsWith(".yml")) {
const fileData = fs.readFileSync(`${repoPath}/${file}`, 'utf8')
const localRepo: LocalRepository = YAML.parse(fileData)
// set name to filename if not defined
if (!localRepo.name) localRepo.name = file.replace(".yml", "")
repositories.push(localRepo)
}
})
// iterate over repositories
const plugins: Plugin[] = []
for (const repo of repositories) {
const plugin = await parseRepository(repo)
plugins.push(...plugin)
}
// fetch all readmes of plugins
const readmePromises = plugins.map(plugin => plugin.checkReadme())
await Promise.all(readmePromises)
// sort plugins and print to md
const sortedPlugins = plugins
.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase()))
return sortedPlugins
}
function printPlugins(outputName: string, sortedPlugins: Plugin[]) {
// create folder if not exists
if (!fs.existsSync(`./dist`)) fs.mkdirSync(`./dist`)
if (!fs.existsSync(`./dist/${outputName}`)) fs.mkdirSync(`./dist/${outputName}`)
// print to file
const outputPath = `./dist/${outputName}/list.md`
const stream = fs.createWriteStream(outputPath)
stream.write(`# List of ${outputName} \n\n`)
// iterate over plugins
for (const plugin of sortedPlugins) {
stream.write(plugin.printMD())
stream.write("\n")
}
stream.end()
}
async function parseRepository(localRepository: LocalRepository): Promise<Plugin[]> {
// load from parsed
const repoDefaults: LocalCollection = localRepository.collection
const repoSidecars: LocalSidecar[] = localRepository.scripts
// grab from index.yml
const indexData: RemoteIndex = await axios.get(repoDefaults.index)
.then(res => YAML.parse(res.data))
// iterate over remote index and match with sidecars
const indexPlugins: Plugin[] = []
const idxMissingScar: Set<RemotePlugin> = new Set()
const allIdx: Set<RemotePlugin> = new Set()
for (const index of indexData) {
const sidecarMatch = repoSidecars.find(sidecar => sidecar.id == index.id)
if (sidecarMatch) {
if (sidecarMatch.id == "example") continue // if example, skip
else if (sidecarMatch.hide) { // skip but warn if hidden
debuglog(`Skipping hidden plugin: ${index.name}`)
continue
} else allIdx.add(index) // add to sidecars
} else { // sidecar not found
if (repoDefaults.exclusive) continue // if exclusive, skip
idxMissingScar.add(index) // add to missing
}
const plugin = new Plugin(repoDefaults, sidecarMatch, index)
indexPlugins.push(plugin)
}
// check if there are leftover sidecars
// not named example
// not in indexPlugins and not hidden
const extraSidecars = repoSidecars.filter(sidecar => sidecar.id != "example" && !sidecar.hide && !indexPlugins.find(plugin => plugin.id == sidecar.id))
if (extraSidecars.length > 0) {
warnLog(`Found ${extraSidecars.length} extra sidecars in ${localRepository.name}`)
extraSidecars.forEach(sidecar => warnLog(` ${sidecar.id}`))
}
// check for plugins without sidecars
const missingSCars = Array.from(idxMissingScar).filter(plugin => allIdx.has(plugin))
if (missingSCars.length > 0) {
infoLog(`Found ${missingSCars.length} missing sidecars in ${localRepository.name}`)
missingSCars.forEach(sidecar => infoLog(` ${sidecar.name}`))
}
return indexPlugins
}
async function run() {
// generate themes first then exclude
const themes = await searchRepository("themes")
printPlugins("themes", themes)
// generate plugins with themes excluded
const plugins = await searchRepository("plugins")
// remove themes from plugins
const filteredPlugins = plugins.filter(plugin => !themes.some(theme => theme.id == plugin.id))
printPlugins("plugins", filteredPlugins)
console.log("finished building plugin index")
}
run()