-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgenerate.ts
More file actions
226 lines (181 loc) · 6.39 KB
/
generate.ts
File metadata and controls
226 lines (181 loc) · 6.39 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
import toml from 'toml'
import { VALID_TAGS, VALID_DATABASES, ValidTag, ValidDatabase } from './constants.js'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const DEFAULT_ICON = 'https://wu-clan.github.io/picx-images-hosting/logo/fba.svg'
interface PluginTomlPlugin {
icon?: string
summary: string
version: string
description: string
author: string
tags?: ValidTag[]
database?: ValidDatabase[]
}
interface PluginToml {
plugin: PluginTomlPlugin
}
interface GitModule {
path: string
url: string
branch: string
}
interface PluginData {
plugin: PluginTomlPlugin
git: GitModule
}
function resolveIconUrl(iconPath: string | undefined, gitUrl: string, branch: string): string {
if (!iconPath) return DEFAULT_ICON
if (iconPath.startsWith('http://') || iconPath.startsWith('https://')) {
return iconPath
}
const match = gitUrl.match(/github\.com[/:]([^/]+)\/([^/.]+)/)
if (!match) return DEFAULT_ICON
const [, owner, repo] = match
return `https://raw.githubusercontent.com/${ owner }/${ repo }/${ branch }/${ iconPath }`
}
function loadPluginToml(pluginPath: string): PluginToml | null {
const tomlPath = path.join(pluginPath, 'plugin.toml')
if (!fs.existsSync(tomlPath)) return null
try {
return toml.parse(fs.readFileSync(tomlPath, 'utf-8')) as PluginToml
} catch {
return null
}
}
function parseGitModules(gitmodulesPath: string): Map<string, GitModule> {
const modules = new Map<string, GitModule>()
if (!fs.existsSync(gitmodulesPath)) return modules
const content = fs.readFileSync(gitmodulesPath, 'utf-8')
const lines = content.split('\n')
let currentPath = ''
let currentUrl = ''
let currentBranch = ''
for (const line of lines) {
const pathMatch = line.match(/path\s*=\s*(.+)/)
const urlMatch = line.match(/url\s*=\s*(.+)/)
const branchMatch = line.match(/branch\s*=\s*(.+)/)
if (pathMatch) currentPath = pathMatch[1].trim()
if (urlMatch) currentUrl = urlMatch[1].trim()
if (branchMatch) currentBranch = branchMatch[1].trim()
if (currentPath && currentUrl) {
modules.set(currentPath, {
path: currentPath,
url: currentUrl,
branch: currentBranch || 'master',
})
currentPath = ''
currentUrl = ''
currentBranch = ''
}
}
return modules
}
function generatePluginData(pluginsDir: string, gitmodulesPath: string): PluginData[] {
const gitModules = parseGitModules(gitmodulesPath)
const pluginDataList: PluginData[] = []
const pluginDirs = fs.readdirSync(pluginsDir, { withFileTypes: true })
.filter(entry => entry.isDirectory() && !entry.name.startsWith('.'))
.map(entry => entry.name)
.sort()
for (const pluginName of pluginDirs) {
const pluginPath = path.join(pluginsDir, pluginName)
const pluginConfig = loadPluginToml(pluginPath)
if (!pluginConfig?.plugin) {
console.warn(`Warning: ${ pluginName } has no valid plugin.toml`)
continue
}
const modulePath = `plugins/${ pluginName }`
const gitModule = gitModules.get(modulePath)
if (!gitModule) {
console.warn(`Warning: ${ pluginName } has no matching git submodule`)
continue
}
const rawPlugin = pluginConfig.plugin
// Validate and filter tags
let tags: ValidTag[] | undefined
if (rawPlugin.tags && Array.isArray(rawPlugin.tags)) {
const filtered = rawPlugin.tags
.map(tag => tag.toLowerCase())
.filter((tag): tag is ValidTag => VALID_TAGS.includes(tag as ValidTag))
if (filtered.length > 0) {
// @ts-ignore
tags = [...new Set(filtered)]
}
}
// Validate and filter database
let database: ValidDatabase[] | undefined
if (rawPlugin.database && Array.isArray(rawPlugin.database)) {
const filtered = rawPlugin.database
.map(db => db.toLowerCase() as ValidDatabase)
.filter((db): db is ValidDatabase => VALID_DATABASES.includes(db))
if (filtered.length > 0) {
// @ts-ignore
database = [...new Set(filtered)]
}
}
const plugin: PluginTomlPlugin = {
icon: resolveIconUrl(rawPlugin.icon, gitModule.url, gitModule.branch),
summary: rawPlugin.summary,
version: rawPlugin.version,
description: rawPlugin.description,
author: rawPlugin.author,
...(tags && { tags }),
...(database && { database }),
}
pluginDataList.push({
plugin,
git: gitModule,
})
}
return pluginDataList
}
function generateTypeScriptCode(pluginDataList: PluginData[]): string {
return `export const validTags = ${ JSON.stringify(VALID_TAGS, null, 2) } as const
export const validDatabases = ${ JSON.stringify(VALID_DATABASES, null, 2) } as const
export type ValidTag = typeof validTags[number]
export type ValidDatabase = typeof validDatabases[number]
export interface PluginTomlPlugin {
icon: string
summary: string
version: string
description: string
author: string
tags?: ValidTag[]
database?: ValidDatabase[]
}
export interface GitModule {
path: string
url: string
branch: string
}
export interface PluginData {
plugin: PluginTomlPlugin
git: GitModule
}
export const pluginDataList: PluginData[] = ${ JSON.stringify(pluginDataList, null, 2) }
`
}
function main() {
const baseDir = __dirname
const pluginsDir = path.join(baseDir, 'plugins')
const gitmodulesPath = path.join(baseDir, '.gitmodules')
console.log('Generating plugins data...')
const pluginDataList = generatePluginData(pluginsDir, gitmodulesPath)
console.log(`Found ${ pluginDataList.length } plugins`)
fs.writeFileSync(
path.join(baseDir, 'plugins-data.ts'),
generateTypeScriptCode(pluginDataList),
'utf-8'
)
fs.writeFileSync(
path.join(baseDir, 'plugins-data.json'),
JSON.stringify({ validTags: VALID_TAGS, validDatabases: VALID_DATABASES, pluginDataList }, null, 2),
'utf-8'
)
console.log('Done')
}
main()