48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
import { Command } from '@effect/platform';
|
|
import { Effect, pipe, Stream, String } from 'effect';
|
|
import { convertFromString } from './json-schema-to-effect';
|
|
|
|
// Helper function to collect stream output as a string
|
|
const runString = <E, R>(
|
|
stream: Stream.Stream<Uint8Array, E, R>,
|
|
): Effect.Effect<string, E, R> =>
|
|
stream.pipe(Stream.decodeText(), Stream.runFold(String.empty, String.concat));
|
|
|
|
export const runCommand = (command: Command.Command) =>
|
|
pipe(
|
|
Command.start(command),
|
|
Effect.flatMap((process) =>
|
|
Effect.all(
|
|
[
|
|
process.exitCode,
|
|
runString(process.stdout),
|
|
runString(process.stderr),
|
|
],
|
|
{ concurrency: 3 },
|
|
),
|
|
),
|
|
Effect.map(([exitCode, stdout, stderr]) => ({ exitCode, stdout, stderr })),
|
|
);
|
|
|
|
export const jsonSchemaToEffectSchema = (params: {
|
|
jsonSchema: string;
|
|
name: string;
|
|
}) =>
|
|
Effect.gen(function* () {
|
|
yield* Effect.logDebug(`Converting schema: ${params.name}`);
|
|
|
|
const result = convertFromString(params.jsonSchema, params.name);
|
|
|
|
yield* Effect.logDebug(
|
|
`Converted "${params.name}" - types: ${result.typeNames.join(', ')}`,
|
|
);
|
|
|
|
if (result.recursiveTypes.length > 0) {
|
|
yield* Effect.logInfo(
|
|
`Recursive types in ${params.name}: ${result.recursiveTypes.join(', ')}`,
|
|
);
|
|
}
|
|
|
|
return result.code;
|
|
});
|