{"version":3,"file":"NodePTY-VnQoqF_Y.mjs","names":[],"sources":["../src/terminal/Layers/NodePTY.ts"],"sourcesContent":["import { createRequire } from \"node:module\";\n\nimport * as Effect from \"effect/Effect\";\nimport * as FileSystem from \"effect/FileSystem\";\nimport * as Layer from \"effect/Layer\";\nimport * as Path from \"effect/Path\";\nimport { PtyAdapter } from \"../Services/PTY.ts\";\nimport {\n  PtySpawnError,\n  type PtyAdapterShape,\n  type PtyExitEvent,\n  type PtyProcess,\n} from \"../Services/PTY.ts\";\n\nlet didEnsureSpawnHelperExecutable = false;\n\nconst resolveNodePtySpawnHelperPath = Effect.gen(function* () {\n  const requireForNodePty = createRequire(import.meta.url);\n  const path = yield* Path.Path;\n  const fs = yield* FileSystem.FileSystem;\n\n  const packageJsonPath = requireForNodePty.resolve(\"node-pty/package.json\");\n  const packageDir = path.dirname(packageJsonPath);\n  const candidates = [\n    path.join(packageDir, \"build\", \"Release\", \"spawn-helper\"),\n    path.join(packageDir, \"build\", \"Debug\", \"spawn-helper\"),\n    path.join(packageDir, \"prebuilds\", `${process.platform}-${process.arch}`, \"spawn-helper\"),\n  ];\n\n  for (const candidate of candidates) {\n    if (yield* fs.exists(candidate)) {\n      return candidate;\n    }\n  }\n  return null;\n}).pipe(Effect.orElseSucceed(() => null));\n\nexport const ensureNodePtySpawnHelperExecutable = Effect.fn(function* (explicitPath?: string) {\n  const fs = yield* FileSystem.FileSystem;\n  if (process.platform === \"win32\") return;\n  if (!explicitPath && didEnsureSpawnHelperExecutable) return;\n\n  const helperPath = explicitPath ?? (yield* resolveNodePtySpawnHelperPath);\n  if (!helperPath) return;\n  if (!explicitPath) {\n    didEnsureSpawnHelperExecutable = true;\n  }\n\n  if (!(yield* fs.exists(helperPath))) {\n    return;\n  }\n\n  // Best-effort: avoid FileSystem.stat in packaged mode where some fs metadata can be missing.\n  yield* fs.chmod(helperPath, 0o755).pipe(Effect.orElseSucceed(() => undefined));\n});\n\nclass NodePtyProcess implements PtyProcess {\n  private readonly process: import(\"node-pty\").IPty;\n\n  constructor(process: import(\"node-pty\").IPty) {\n    this.process = process;\n  }\n\n  get pid(): number {\n    return this.process.pid;\n  }\n\n  write(data: string): void {\n    this.process.write(data);\n  }\n\n  resize(cols: number, rows: number): void {\n    this.process.resize(cols, rows);\n  }\n\n  kill(signal?: string): void {\n    this.process.kill(signal);\n  }\n\n  onData(callback: (data: string) => void): () => void {\n    const disposable = this.process.onData(callback);\n    return () => {\n      disposable.dispose();\n    };\n  }\n\n  onExit(callback: (event: PtyExitEvent) => void): () => void {\n    const disposable = this.process.onExit((event) => {\n      callback({\n        exitCode: event.exitCode,\n        signal: event.signal ?? null,\n      });\n    });\n    return () => {\n      disposable.dispose();\n    };\n  }\n}\n\nexport const layer = Layer.effect(\n  PtyAdapter,\n  Effect.gen(function* () {\n    const fs = yield* FileSystem.FileSystem;\n    const path = yield* Path.Path;\n\n    const nodePty = yield* Effect.promise(() => import(\"node-pty\"));\n\n    const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(\n      ensureNodePtySpawnHelperExecutable().pipe(\n        Effect.provideService(FileSystem.FileSystem, fs),\n        Effect.provideService(Path.Path, path),\n        Effect.orElseSucceed(() => undefined),\n      ),\n    );\n\n    return {\n      spawn: Effect.fn(function* (input) {\n        yield* ensureNodePtySpawnHelperExecutableCached;\n        const ptyProcess = yield* Effect.try({\n          try: () =>\n            nodePty.spawn(input.shell, input.args ?? [], {\n              cwd: input.cwd,\n              cols: input.cols,\n              rows: input.rows,\n              env: input.env,\n              name: globalThis.process.platform === \"win32\" ? \"xterm-color\" : \"xterm-256color\",\n            }),\n          catch: (cause) =>\n            new PtySpawnError({\n              adapter: \"node-pty\",\n              message: cause instanceof Error ? cause.message : \"Failed to spawn PTY process\",\n              cause,\n            }),\n        });\n        return new NodePtyProcess(ptyProcess);\n      }),\n    } satisfies PtyAdapterShape;\n  }),\n);\n"],"mappings":";;;;;;;;;AAcA,IAAI,iCAAiC;AAErC,MAAM,gCAAgC,OAAO,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