#!/usr/bin/env node import { program } from 'commander' import chalk from 'chalk' import { PropertiesSchema } from './schema' import prettier from 'prettier' import fs from 'fs' import { getPropertyType } from './types' import path from 'path' await program.parseAsync() const url = program.args[0] if (!url) { console.error(chalk.red('Usage: adofai-typegen ')) process.exit(1) } console.log(chalk.blue`Fetching properties json`) const data: PropertiesSchema = await (await fetch(url)).json() console.log(chalk.blue`Generating...`) let result: string[] = [] result.push(` export interface Action { floor: number type: T } export type Tile = [number, TileType] export enum TileType { ThisTile = "ThisTile", FirstTile = "FirstTile", LastTile = "LastTile" } export type Color = \`#\${string}\` export type Vector2 = [number, number] `) const enumValues: string[] = [] const enumImports = new Set() const blacklistedKeys = ['type', 'floor'] for (const event of data.levelEvents) { console.log(chalk.gray(`> ${event.name}`)) const properties: string[] = [] for (const property of event.properties) { if (blacklistedKeys.includes(property.name)) { continue } if (property.type.startsWith('Enum:')) enumImports.add(property.type.slice(5)) properties.push(`${property.name}: ${getPropertyType(property.type)}`) } enumValues.push(event.name) result.push(`export interface ${event.name} extends Action { ${properties.join('\n')} }`) } result.unshift(` import { ${Array.from(enumImports).join(', ')} } from './types' export enum LevelEventType { ${enumValues.map((x) => `${x} = ${JSON.stringify(x)}`).join(',\n')} } `) console.log(chalk.blue`Saving...`) const formatted = prettier.format(result.join('\n\n'), { parser: 'typescript' }) await fs.promises.writeFile('output.ts', formatted) console.log( chalk.green`Done! You should implement below enums at ${path.join( process.cwd(), 'types.ts' )}` ) for (const i of enumImports) { console.log(chalk.yellow(i)) }