2024-08-13 04:53:07 +03:00
|
|
|
const fs = require('node:fs')
|
|
|
|
const path = require('node:path')
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
function snakeToCamel(s) {
|
2021-11-23 00:03:59 +03:00
|
|
|
return s.replace(/(?<!^|_)(_[a-z0-9])/gi, ($1) => {
|
|
|
|
return $1.substr(1).toUpperCase()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
const camelToPascal = s => s[0].toUpperCase() + s.substr(1)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
function camelToSnake(s) {
|
2023-10-06 01:47:45 +03:00
|
|
|
return s.replace(/(?<=[a-zA-Z0-9])([A-Z0-9]+(?=[A-Z]|$)|[A-Z0-9])/g, ($1) => {
|
2024-08-13 04:53:07 +03:00
|
|
|
return `_${$1.toLowerCase()}`
|
2023-10-06 01:47:45 +03:00
|
|
|
})
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
2021-07-17 17:26:31 +03:00
|
|
|
|
|
|
|
function parseUpdateTypes() {
|
|
|
|
const lines = fs
|
|
|
|
.readFileSync(path.join(__dirname, 'update-types.txt'), 'utf-8')
|
|
|
|
.split('\n')
|
2024-08-13 04:53:07 +03:00
|
|
|
.map(it => it.trim())
|
|
|
|
.filter(it => it && it[0] !== '#')
|
2021-07-17 17:26:31 +03:00
|
|
|
|
|
|
|
const ret = []
|
|
|
|
|
|
|
|
for (const line of lines) {
|
2023-10-11 08:24:11 +03:00
|
|
|
const m = line.match(/^([a-z_]+)(?:: ([a-zA-Z]+))? = ([a-zA-Z]+(?:\[\])?)( \+ State)?(?: in ([a-zA-Z]+))?$/)
|
2021-07-17 17:26:31 +03:00
|
|
|
if (!m) throw new Error(`invalid syntax: ${line}`)
|
|
|
|
ret.push({
|
|
|
|
typeName: m[1],
|
|
|
|
handlerTypeName: m[2] || camelToPascal(snakeToCamel(m[1])),
|
|
|
|
updateType: m[3],
|
2023-10-06 01:47:45 +03:00
|
|
|
funcName: m[2] ? m[2][0].toLowerCase() + m[2].substr(1) : snakeToCamel(m[1]),
|
2023-06-05 03:30:48 +03:00
|
|
|
state: Boolean(m[4]),
|
2023-10-11 08:24:11 +03:00
|
|
|
context: m[5] ?? `UpdateContext<${m[3]}>`,
|
2021-07-17 17:26:31 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
function replaceSections(filename, sections, dir = __dirname) {
|
2024-08-13 04:53:07 +03:00
|
|
|
const lines = fs.readFileSync(path.join(dir, '../src', filename), 'utf-8').split('\n')
|
2021-07-17 17:26:31 +03:00
|
|
|
|
|
|
|
const findMarker = (marker) => {
|
2024-08-13 04:53:07 +03:00
|
|
|
const idx = lines.findIndex(line => line.trim() === `// ${marker}`)
|
|
|
|
if (idx === -1) throw new Error(`${marker} not found`)
|
2023-06-05 03:30:48 +03:00
|
|
|
|
2021-07-17 17:26:31 +03:00
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [name, content] of Object.entries(sections)) {
|
|
|
|
const start = findMarker(`begin-${name}`)
|
|
|
|
const end = findMarker(`end-${name}`)
|
|
|
|
|
|
|
|
if (start > end) throw new Error('begin is after end')
|
|
|
|
|
|
|
|
lines.splice(start + 1, end - start - 1, content)
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFileSync(path.join(dir, '../src', filename), lines.join('\n'))
|
|
|
|
}
|
|
|
|
|
|
|
|
const types = parseUpdateTypes()
|
|
|
|
|
|
|
|
function toSentence(type, stype = 'inline') {
|
2023-10-06 01:47:45 +03:00
|
|
|
const name = camelToSnake(type.handlerTypeName).toLowerCase().replace(/_/g, ' ')
|
2021-07-17 17:26:31 +03:00
|
|
|
|
|
|
|
if (stype === 'inline') {
|
|
|
|
return `${name[0].match(/[aeiouy]/i) ? 'an' : 'a'} ${name} handler`
|
|
|
|
} else if (stype === 'plain') {
|
|
|
|
return `${name} handler`
|
|
|
|
}
|
2023-06-05 03:30:48 +03:00
|
|
|
|
|
|
|
return `${name[0].toUpperCase()}${name.substr(1)} handler`
|
2021-07-17 17:26:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateParsedUpdate() {
|
2024-01-31 19:29:49 +03:00
|
|
|
replaceSections('highlevel/types/updates/index.ts', {
|
2023-10-06 01:47:45 +03:00
|
|
|
codegen:
|
2024-08-13 04:53:07 +03:00
|
|
|
`export type ParsedUpdate =\n${
|
|
|
|
types.map(typ => ` | { name: '${typ.typeName}'; data: ${typ.updateType} }\n`).join('')}`,
|
2021-07-17 17:26:31 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
generateParsedUpdate()
|
|
|
|
}
|
|
|
|
|
2024-08-12 08:19:16 +03:00
|
|
|
module.exports = { types, toSentence, replaceSections }
|
2021-07-17 17:26:31 +03:00
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
main().catch(console.error)
|
|
|
|
}
|