2021-11-23 00:03:59 +03:00
|
|
|
import { unpackTlSchema } from './schema'
|
2022-04-01 22:17:10 +03:00
|
|
|
import {
|
|
|
|
API_SCHEMA_JSON_FILE,
|
|
|
|
MTP_SCHEMA_JSON_FILE,
|
|
|
|
ESM_PRELUDE,
|
|
|
|
ERRORS_JSON_FILE,
|
|
|
|
} from './constants'
|
2021-11-23 00:03:59 +03:00
|
|
|
import { readFile, writeFile } from 'fs/promises'
|
|
|
|
import { parseFullTlSchema } from '@mtcute/tl-utils/src/schema'
|
2022-04-01 22:17:10 +03:00
|
|
|
import { TlErrors, TlFullSchema } from '@mtcute/tl-utils/src/types'
|
2021-11-23 00:03:59 +03:00
|
|
|
import { join } from 'path'
|
|
|
|
import { generateTypescriptDefinitionsForTlSchema } from '@mtcute/tl-utils/src/codegen/types'
|
|
|
|
import { generateReaderCodeForTlEntries } from '@mtcute/tl-utils/src/codegen/reader'
|
|
|
|
import { generateWriterCodeForTlEntries } from '@mtcute/tl-utils/src/codegen/writer'
|
|
|
|
|
|
|
|
const OUT_TYPINGS_FILE = join(__dirname, '../index.d.ts')
|
|
|
|
const OUT_TYPINGS_JS_FILE = join(__dirname, '../index.js')
|
|
|
|
const OUT_READERS_FILE = join(__dirname, '../binary/reader.js')
|
|
|
|
const OUT_WRITERS_FILE = join(__dirname, '../binary/writer.js')
|
|
|
|
|
|
|
|
async function generateTypings(
|
|
|
|
apiSchema: TlFullSchema,
|
|
|
|
apiLayer: number,
|
2022-04-01 22:17:10 +03:00
|
|
|
mtpSchema: TlFullSchema,
|
|
|
|
errors: TlErrors
|
2021-11-23 00:03:59 +03:00
|
|
|
) {
|
|
|
|
console.log('Generating typings...')
|
|
|
|
const [apiTs, apiJs] = generateTypescriptDefinitionsForTlSchema(
|
|
|
|
apiSchema,
|
2022-04-01 22:17:10 +03:00
|
|
|
apiLayer,
|
|
|
|
undefined,
|
|
|
|
errors
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
const [mtpTs, mtpJs] = generateTypescriptDefinitionsForTlSchema(
|
|
|
|
mtpSchema,
|
|
|
|
0,
|
2022-04-01 22:17:10 +03:00
|
|
|
'mtp',
|
|
|
|
errors
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
OUT_TYPINGS_FILE,
|
|
|
|
apiTs + '\n\n' + mtpTs.replace("import _Long from 'long';", '')
|
|
|
|
)
|
|
|
|
await writeFile(OUT_TYPINGS_JS_FILE, ESM_PRELUDE + apiJs + '\n\n' + mtpJs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateReaders(
|
|
|
|
apiSchema: TlFullSchema,
|
|
|
|
mtpSchema: TlFullSchema
|
|
|
|
) {
|
|
|
|
console.log('Generating readers...')
|
|
|
|
|
|
|
|
let code = generateReaderCodeForTlEntries(apiSchema.entries, 'r', false)
|
|
|
|
|
|
|
|
const mtpCode = generateReaderCodeForTlEntries(mtpSchema.entries, '')
|
|
|
|
code = code.substring(0, code.length - 1) + mtpCode.substring(7)
|
|
|
|
code += '\nexports.default = r;'
|
|
|
|
|
|
|
|
await writeFile(OUT_READERS_FILE, ESM_PRELUDE + code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateWriters(
|
|
|
|
apiSchema: TlFullSchema,
|
|
|
|
mtpSchema: TlFullSchema
|
|
|
|
) {
|
|
|
|
console.log('Generating writers...')
|
|
|
|
|
|
|
|
let code = generateWriterCodeForTlEntries(apiSchema.entries, 'r')
|
|
|
|
|
|
|
|
const mtpCode = generateWriterCodeForTlEntries(mtpSchema.entries, '', false)
|
|
|
|
code = code.substring(0, code.length - 1) + mtpCode.substring(7)
|
|
|
|
code += '\nexports.default = r;'
|
|
|
|
|
|
|
|
await writeFile(OUT_WRITERS_FILE, ESM_PRELUDE + code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2022-04-01 22:17:10 +03:00
|
|
|
const errors: TlErrors = JSON.parse(
|
|
|
|
await readFile(ERRORS_JSON_FILE, 'utf8')
|
|
|
|
)
|
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
const [apiSchema, apiLayer] = unpackTlSchema(
|
|
|
|
JSON.parse(await readFile(API_SCHEMA_JSON_FILE, 'utf8'))
|
|
|
|
)
|
|
|
|
const mtpSchema = parseFullTlSchema(
|
|
|
|
JSON.parse(await readFile(MTP_SCHEMA_JSON_FILE, 'utf8'))
|
|
|
|
)
|
|
|
|
|
2022-04-01 22:17:10 +03:00
|
|
|
await generateTypings(apiSchema, apiLayer, mtpSchema, errors)
|
2021-11-23 00:03:59 +03:00
|
|
|
await generateReaders(apiSchema, mtpSchema)
|
|
|
|
await generateWriters(apiSchema, mtpSchema)
|
|
|
|
|
|
|
|
console.log('Done!')
|
|
|
|
}
|
|
|
|
|
|
|
|
main().catch(console.error)
|