{"version":3,"file":"NodePtyAdapter-p_QCGByp.mjs","names":["Schema.TaggedErrorClass","Schema.String","Schema.Defect","Effect.gen","Path.Path","FileSystem.FileSystem","Effect.orElseSucceed","Effect.fn","Effect.tryPromise","Effect.orDie","Effect.cached","Effect.provideService","Effect.try","PtyAdapter.PtySpawnError","Layer.effect","PtyAdapter.PtyAdapter"],"sources":["../src/terminal/NodePtyAdapter.ts"],"sourcesContent":["import * as NodeModule 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 * as Schema from \"effect/Schema\";\nimport { HostProcessArchitecture, HostProcessPlatform } from \"@t3tools/shared/hostProcess\";\n\nimport * as PtyAdapter from \"./PtyAdapter.ts\";\n\nexport class NodePtyModuleLoadError extends Schema.TaggedErrorClass<NodePtyModuleLoadError>()(\n  \"NodePtyModuleLoadError\",\n  {\n    platform: Schema.String,\n    architecture: Schema.String,\n    cause: Schema.Defect(),\n  },\n) {\n  override get message(): string {\n    return `Failed to load node-pty for ${this.platform}-${this.architecture}.`;\n  }\n}\n\ntype NodePtyModuleLoader = () => Promise<typeof import(\"node-pty\")>;\n\nlet didEnsureSpawnHelperExecutable = false;\n\nconst resolveNodePtySpawnHelperPath = Effect.gen(function* () {\n  const requireForNodePty = NodeModule.createRequire(import.meta.url);\n  const path = yield* Path.Path;\n  const fs = yield* FileSystem.FileSystem;\n  const platform = yield* HostProcessPlatform;\n  const architecture = yield* HostProcessArchitecture;\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\", `${platform}-${architecture}`, \"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\nconst ensureNodePtySpawnHelperExecutable = Effect.fn(function* () {\n  const fs = yield* FileSystem.FileSystem;\n  const platform = yield* HostProcessPlatform;\n  if (platform === \"win32\") return;\n  if (didEnsureSpawnHelperExecutable) return;\n\n  const helperPath = yield* resolveNodePtySpawnHelperPath;\n  if (!helperPath) return;\n  didEnsureSpawnHelperExecutable = true;\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 PtyAdapter.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: PtyAdapter.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 make = Effect.fn(\"NodePtyAdapter.make\")(function* (\n  loadNodePtyModule: NodePtyModuleLoader = () => import(\"node-pty\"),\n) {\n  const fs = yield* FileSystem.FileSystem;\n  const path = yield* Path.Path;\n  const platform = yield* HostProcessPlatform;\n  const architecture = yield* HostProcessArchitecture;\n\n  const nodePty = yield* Effect.tryPromise({\n    try: loadNodePtyModule,\n    catch: (cause) =>\n      new NodePtyModuleLoadError({\n        platform,\n        architecture,\n        cause,\n      }),\n  }).pipe(Effect.orDie);\n\n  const ensureNodePtySpawnHelperExecutableCached = yield* Effect.cached(\n    ensureNodePtySpawnHelperExecutable().pipe(\n      Effect.provideService(FileSystem.FileSystem, fs),\n      Effect.provideService(Path.Path, path),\n      Effect.provideService(HostProcessPlatform, platform),\n      Effect.provideService(HostProcessArchitecture, architecture),\n      Effect.orElseSucceed(() => undefined),\n    ),\n  );\n\n  return PtyAdapter.PtyAdapter.of({\n    spawn: Effect.fn(\"NodePtyAdapter.spawn\")(function* (input) {\n      yield* ensureNodePtySpawnHelperExecutableCached;\n      // node-pty only writes `name` into the child's TERM on the Unix path;\n      // the ConPTY path leaves the environment untouched, so Windows children\n      // inherit a missing or 16-color TERM unless it is set here.\n      const env =\n        platform === \"win32\" && input.env[\"TERM\"] === undefined\n          ? { ...input.env, TERM: \"xterm-256color\" }\n          : input.env;\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,\n            name: \"xterm-256color\",\n          }),\n        catch: (cause) =>\n          new PtyAdapter.PtySpawnError({\n            adapter: \"node-pty\",\n            shell: input.shell,\n            cause,\n          }),\n      });\n      return new NodePtyProcess(ptyProcess);\n    }),\n  });\n});\n\nexport const layer = Layer.effect(PtyAdapter.PtyAdapter, make());\n"],"mappings":";;;;;;;AAWA,IAAa,yBAAb,cAA4CA,iBAAgD,CAAC,CAC3F,0BACA;CACE,UAAUC;CACV,cAAcA;CACd,OAAOC,OAAc;AACvB,CACF,CAAC,CAAC;CACA,IAAa,UAAkB;EAC7B,OAAO,+BAA+B,KAAK,SAAS,GAAG,KAAK,aAAa;CAC3E;AACF;AAIA,IAAI,iCAAiC;AAErC,MAAM,gCAAgCC,IAAW,aAAa;CAC5D,MAAM,oBAAoB,WAAW,cAAc,OAAO,KAAK,GAAG;CAClE,MAAM,OAAO,OAAOC;CACpB,MAAM,KAAK,OAAOC;CAClB,MAAM,WAAW,OAA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