import { Command, FileSystem, Path } from '@effect/platform'; import { BunContext, BunRuntime } from '@effect/platform-bun'; import { Effect, Logger, LogLevel } from 'effect'; import { jsonSchemaToEffectSchema, runCommand } from './lib/script-utils'; const getDscJsonSchema = (schemaType: string) => Effect.gen(function* () { const commandParts = ['dsc', 'schema', '--type', schemaType] as const; yield* Effect.logDebug(`Running command: ${commandParts.join(' ')}`); const schemaString = yield* Command.make(...commandParts).pipe( Command.string, ); return schemaString.trim(); }); const getPossibleDscSchemaTypes = () => Effect.gen(function* () { yield* Effect.logDebug('Getting possible DSC schema types...'); const brokenCommand = Command.make( 'dsc', 'schema', '--type', 'something-that-hopefully-never-exists', ); const { stderr: brokenCommandOutput } = yield* runCommand(brokenCommand); const possibleTypes = brokenCommandOutput .split('\n') .filter((line) => line.includes('possible values:')) .map((line) => { const values = line.match(/\[possible values: (.+)\]/)?.[1]; return values?.split(', ') ?? []; })[0]; if (!possibleTypes) return yield* Effect.die('No possible types found'); yield* Effect.logDebug(`Found ${possibleTypes.length} schema types`); return possibleTypes; }); const genDscTypes = Effect.gen(function* () { yield* Effect.log('Starting DSC types generation...'); const possibleTypes = yield* getPossibleDscSchemaTypes(); yield* Effect.log(`Found schema types: ${possibleTypes.join(', ')}`); const fs = yield* FileSystem.FileSystem; const path = yield* Path.Path; const outputDir = path.join(process.cwd(), 'src', 'dsc-schema-types'); yield* Effect.logDebug(`Output directory: ${outputDir}`); if (yield* fs.exists(outputDir)) { yield* Effect.log('Removing existing output directory...'); yield* fs.remove(outputDir, { recursive: true, force: true }); } yield* fs.makeDirectory(outputDir, { recursive: true }); for (const schemaType of possibleTypes) { yield* Effect.logDebug(`Processing: ${schemaType}`); const jsonSchema = yield* getDscJsonSchema(schemaType); const effectSchema = yield* jsonSchemaToEffectSchema({ jsonSchema, name: schemaType, }); const outputPath = path.join(outputDir, `${schemaType}.gen.ts`); yield* fs.writeFileString(outputPath, effectSchema); yield* Effect.log(`Generated: ${schemaType}.gen.ts`); } yield* Effect.log('DSC types generation complete!'); }); BunRuntime.runMain( genDscTypes.pipe( Effect.provide(BunContext.layer), Logger.withMinimumLogLevel(LogLevel.Debug), Effect.scoped, ), );