fix: generate games list with padded delimiter (#635)

* Generate games list with padded table structured (based on defined columns) fixes #607

* Add comment to HeaderDefinition
This commit is contained in:
RattleSN4K3 2024-09-15 19:40:50 +02:00 committed by GitHub
parent 2c0e2985fe
commit 30ed54dfcd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 14 deletions

View file

@ -1,4 +1,6 @@
### Supported ### Supported
<!--- BEGIN GENERATED GAMES -->
| GameDig Type ID | Name | See Also | | GameDig Type ID | Name | See Also |
|----------------------|--------------------------------------------------|-------------------------------------------------| |----------------------|--------------------------------------------------|-------------------------------------------------|
| a2oa | ARMA 2: Operation Arrowhead | [Valve Protocol](#valve) | | a2oa | ARMA 2: Operation Arrowhead | [Valve Protocol](#valve) |
@ -346,6 +348,7 @@
| xpandrally | Xpand Rally | | | xpandrally | Xpand Rally | |
| zombiemaster | Zombie Master | [Valve Protocol](#valve) | | zombiemaster | Zombie Master | [Valve Protocol](#valve) |
| zps | Zombie Panic: Source | [Valve Protocol](#valve) | | zps | Zombie Panic: Source | [Valve Protocol](#valve) |
<!--- END GENERATED GAMES -->
### Not supported (yet) ### Not supported (yet)

View file

@ -20,13 +20,37 @@ sortedGamesIds.forEach(key => {
sortedGames[key] = games[key] sortedGames[key] = games[key]
}) })
let generated = '' const columnDelimiter = '|'
generated += '| GameDig Type ID | Name | See Also\n' const columnPadLeft = 1
generated += '|---|---|---\n' const columnPadRight = 1
const HeaderType = {
ID: 0,
GameName: 1,
Notes: 2
}
const HeaderNames = {
[HeaderType.ID]: { Name: 'GameDig Type ID' },
[HeaderType.GameName]: { Name: 'Name' },
[HeaderType.Notes]: { Name: 'See Also' }
}
// defines the order of columns
const HeaderDefinition = [
HeaderType.ID,
HeaderType.GameName,
HeaderType.Notes
]
const headerMap = HeaderDefinition.map(idx => Object.values(HeaderType)[idx])
const headers = Object.keys(HeaderType).map((x, idx) => HeaderNames[idx].Name)
const matrix = []
const maxLength = headers.map(x => x?.length ?? 0)
Object.entries(sortedGames).forEach(([id, game]) => {
const lineArray = Array(headerMap.length).fill('')
lineArray[HeaderType.ID] = id
lineArray[HeaderType.GameName] = game.name
for (const id in sortedGames) {
const game = sortedGames[id]
generated += '| ' + id.padEnd(10, ' ') + ' | ' + game.name
const notes = [] const notes = []
if (game?.extra?.doc_notes) { if (game?.extra?.doc_notes) {
notes.push('[Notes](#' + game.extra.doc_notes + ')') notes.push('[Notes](#' + game.extra.doc_notes + ')')
@ -37,15 +61,31 @@ for (const id in sortedGames) {
if (game.options.protocol === 'epic' || game.options.protocol === 'asa' || game.options.protocol === 'theisleevrima') { if (game.options.protocol === 'epic' || game.options.protocol === 'asa' || game.options.protocol === 'theisleevrima') {
notes.push('[EOS Protocol](#epic)') notes.push('[EOS Protocol](#epic)')
} }
if (notes.length) { lineArray[HeaderType.Notes] = notes.join(', ')
generated += ' | ' + notes.join(', ')
} lineArray.forEach((x, index) => {
generated += '\n' maxLength[index] = Math.max(maxLength[index], x?.length ?? 0)
} })
matrix.push(lineArray)
})
matrix.splice(0, 0, headers)
const padLeft = ' '.repeat(columnPadLeft)
const padRight = ' '.repeat(columnPadRight)
const lines = matrix.map(row => {
const values = headerMap.map((x, idx) => {
return padLeft + row[x].padEnd(maxLength[x], ' ') + padRight
})
return `${columnDelimiter}${''}${values.join(columnDelimiter)}${columnDelimiter}`
})
const headerSeps = ['', ...headerMap.map(x => '-'.repeat(maxLength[x] + columnPadLeft + columnPadRight)), '']
const headerSep = `${headerSeps.join(columnDelimiter)}`
lines.splice(1, 0, headerSep)
const generated = lines.join('\n')
let start = readme.indexOf(markerTop) let start = readme.indexOf(markerTop)
start += markerTop.length const end = start >= 0 ? readme.indexOf(markerBottom) : 0
const end = readme.indexOf(markerBottom) start = Math.max(0, start) + (start >= 0 ? markerTop.length : 0)
const updated = readme.substring(0, start) + '\n\n' + generated + '\n' + readme.substring(end) const updated = readme.substring(0, start) + '\n' + generated + '\n' + readme.substring(end)
fs.writeFileSync(readmeFilename, updated) fs.writeFileSync(readmeFilename, updated)