|
| 1 | +import { createServer } from 'node:http'; |
| 2 | +import * as Command from '@effect/cli/Command'; |
| 3 | +import * as Options from '@effect/cli/Options'; |
| 4 | +import * as NodeHttpServer from '@effect/platform-node/NodeHttpServer'; |
| 5 | +import * as HttpServer from '@effect/platform/HttpServer'; |
| 6 | +import * as Console from 'effect/Console'; |
| 7 | +import * as Data from 'effect/Data'; |
| 8 | +import * as Effect from 'effect/Effect'; |
| 9 | +import * as Layer from 'effect/Layer'; |
| 10 | +import open, { type AppName } from 'open'; |
| 11 | + |
| 12 | +import * as Server from '../Server.js'; |
| 13 | + |
| 14 | +export const studio = Command.make('studio', { |
| 15 | + args: { |
| 16 | + port: Options.integer('port').pipe( |
| 17 | + Options.withAlias('p'), |
| 18 | + Options.withDefault(3000), |
| 19 | + Options.withDescription('The port to run the Typesync studio server on. Default 3000'), |
| 20 | + ), |
| 21 | + open: Options.boolean('open').pipe( |
| 22 | + Options.withDescription('If true, opens the Typesync studio in your browser'), |
| 23 | + Options.withDefault(true), |
| 24 | + ), |
| 25 | + browser: Options.choice('browser', ['chrome', 'firefox', 'edge', 'browser', 'browserPrivate']).pipe( |
| 26 | + Options.withAlias('b'), |
| 27 | + Options.withDescription('Broweser to open the Typesync studio app in. Default is your default selected browser'), |
| 28 | + Options.withDefault('browser'), |
| 29 | + ), |
| 30 | + }, |
| 31 | +}).pipe( |
| 32 | + Command.withDescription('Opens the Typesync studio for interacting with application schemas'), |
| 33 | + Command.withHandler(({ args }) => |
| 34 | + Effect.gen(function* () { |
| 35 | + yield* Server.Server.pipe( |
| 36 | + HttpServer.withLogAddress, |
| 37 | + Layer.provide(NodeHttpServer.layer(createServer, { port: args.port })), |
| 38 | + Layer.tap(() => |
| 39 | + Effect.gen(function* () { |
| 40 | + if (args.open) { |
| 41 | + return yield* openBrowser(args.port, args.browser).pipe( |
| 42 | + Effect.tapErrorCause((cause) => |
| 43 | + Console.warn( |
| 44 | + `Failure opening Typesync studio in your browser. Open at http://localhost:${args.port}`, |
| 45 | + { |
| 46 | + cause, |
| 47 | + }, |
| 48 | + ), |
| 49 | + ), |
| 50 | + Effect.orElseSucceed(() => Effect.void), |
| 51 | + ); |
| 52 | + } |
| 53 | + return Effect.void; |
| 54 | + }), |
| 55 | + ), |
| 56 | + Layer.tap(() => Console.log(`🎉 Typesync studio started and running at http://localhost:${args.port}`)), |
| 57 | + Layer.launch, |
| 58 | + ); |
| 59 | + }), |
| 60 | + ), |
| 61 | +); |
| 62 | + |
| 63 | +const openBrowser = (port: number, browser: AppName) => |
| 64 | + Effect.async<void, OpenBrowserError>((resume) => { |
| 65 | + open(`http://localhost:${port}`, { |
| 66 | + app: { name: browser }, |
| 67 | + }).then((subprocess) => { |
| 68 | + // wait for child process to start before succeeding |
| 69 | + subprocess.on('spawn', () => { |
| 70 | + resume(Effect.void); |
| 71 | + }); |
| 72 | + subprocess.on('error', (err) => { |
| 73 | + resume(Effect.fail(new OpenBrowserError({ cause: err }))); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | +export class OpenBrowserError extends Data.TaggedError('/typesync/errors/OpenBrowserError')<{ |
| 79 | + readonly cause: unknown; |
| 80 | +}> {} |
0 commit comments