2021-11-23 00:03:59 +03:00
|
|
|
import { readFile, writeFile } from 'fs/promises'
|
2023-06-05 03:30:48 +03:00
|
|
|
import { join } from 'path'
|
|
|
|
|
2022-08-12 20:11:27 +03:00
|
|
|
import {
|
2023-06-05 03:30:48 +03:00
|
|
|
generateReaderCodeForTlEntries,
|
|
|
|
generateTypescriptDefinitionsForTlSchema,
|
|
|
|
generateWriterCodeForTlEntries,
|
2022-08-12 20:11:27 +03:00
|
|
|
parseFullTlSchema,
|
2023-09-03 02:37:51 +03:00
|
|
|
TlEntry,
|
2022-08-12 20:11:27 +03:00
|
|
|
TlErrors,
|
|
|
|
TlFullSchema,
|
|
|
|
} from '@mtcute/tl-utils'
|
2023-06-05 03:30:48 +03:00
|
|
|
|
|
|
|
import {
|
|
|
|
API_SCHEMA_JSON_FILE,
|
|
|
|
ERRORS_JSON_FILE,
|
|
|
|
ESM_PRELUDE,
|
|
|
|
MTP_SCHEMA_JSON_FILE,
|
|
|
|
} from './constants'
|
2023-09-03 02:37:51 +03:00
|
|
|
import { TlPackedSchema, unpackTlSchema } from './schema'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
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,
|
2023-06-05 03:30:48 +03:00
|
|
|
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,
|
2023-06-05 03:30:48 +03:00
|
|
|
errors,
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
const [mtpTs, mtpJs] = generateTypescriptDefinitionsForTlSchema(
|
|
|
|
mtpSchema,
|
|
|
|
0,
|
2022-04-01 22:17:10 +03:00
|
|
|
'mtp',
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
await writeFile(
|
|
|
|
OUT_TYPINGS_FILE,
|
2023-06-05 03:30:48 +03:00
|
|
|
apiTs + '\n\n' + mtpTs.replace("import _Long from 'long';", ''),
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
await writeFile(OUT_TYPINGS_JS_FILE, ESM_PRELUDE + apiJs + '\n\n' + mtpJs)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateReaders(
|
|
|
|
apiSchema: TlFullSchema,
|
2023-06-05 03:30:48 +03:00
|
|
|
mtpSchema: TlFullSchema,
|
2021-11-23 00:03:59 +03:00
|
|
|
) {
|
|
|
|
console.log('Generating readers...')
|
|
|
|
|
2023-06-25 03:09:04 +03:00
|
|
|
let code = generateReaderCodeForTlEntries(apiSchema.entries, {
|
|
|
|
variableName: 'm',
|
|
|
|
includeMethods: false,
|
2023-07-20 22:07:07 +03:00
|
|
|
includeMethodResults: true,
|
2023-06-25 03:09:04 +03:00
|
|
|
})
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2023-06-25 03:09:04 +03:00
|
|
|
const mtpCode = generateReaderCodeForTlEntries(mtpSchema.entries, {
|
|
|
|
variableName: 'm',
|
|
|
|
})
|
|
|
|
code = code.substring(0, code.length - 1) + mtpCode.substring(8)
|
|
|
|
code += '\nexports.default = m;'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
await writeFile(OUT_READERS_FILE, ESM_PRELUDE + code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateWriters(
|
|
|
|
apiSchema: TlFullSchema,
|
2023-06-05 03:30:48 +03:00
|
|
|
mtpSchema: TlFullSchema,
|
2021-11-23 00:03:59 +03:00
|
|
|
) {
|
|
|
|
console.log('Generating writers...')
|
|
|
|
|
2023-09-20 18:37:26 +03:00
|
|
|
let code = generateWriterCodeForTlEntries(
|
|
|
|
[...apiSchema.entries, ...mtpSchema.entries],
|
|
|
|
{
|
|
|
|
variableName: 'm',
|
|
|
|
includeStaticSizes: true,
|
|
|
|
},
|
|
|
|
)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2023-06-25 03:09:04 +03:00
|
|
|
code += '\nexports.default = m;'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
await writeFile(OUT_WRITERS_FILE, ESM_PRELUDE + code)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2023-09-03 02:37:51 +03:00
|
|
|
const errors = JSON.parse(
|
2023-06-05 03:30:48 +03:00
|
|
|
await readFile(ERRORS_JSON_FILE, 'utf8'),
|
2023-09-03 02:37:51 +03:00
|
|
|
) as TlErrors
|
2022-04-01 22:17:10 +03:00
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
const [apiSchema, apiLayer] = unpackTlSchema(
|
2023-09-03 02:37:51 +03:00
|
|
|
JSON.parse(
|
|
|
|
await readFile(API_SCHEMA_JSON_FILE, 'utf8'),
|
|
|
|
) as TlPackedSchema,
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
const mtpSchema = parseFullTlSchema(
|
2023-09-03 02:37:51 +03:00
|
|
|
JSON.parse(await readFile(MTP_SCHEMA_JSON_FILE, 'utf8')) as TlEntry[],
|
2021-11-23 00:03:59 +03:00
|
|
|
)
|
|
|
|
|
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)
|