22import type { PlatformError } from "@effect/platform/Error"
33import type * as FileSystem from "@effect/platform/FileSystem"
44import type * as Path from "@effect/platform/Path"
5- import { createRequire } from "node:module"
6- import nodePath from "node:path"
75import { Effect , Match } from "effect"
86
97import { dockerGitScriptNames } from "../core/docker-git-scripts.js"
@@ -17,8 +15,8 @@ import { resolveWorkspaceRoot } from "./workspace-root.js"
1715const ensureParentDir = ( path : Path . Path , fs : FileSystem . FileSystem , filePath : string ) =>
1816 fs . makeDirectory ( path . dirname ( filePath ) , { recursive : true } )
1917
20- const require = createRequire ( import . meta. url )
2118const sessionSyncToolRelativePath = ".docker-git-tools/docker-git-session-sync"
19+ const sessionSyncPackageJsonSpecifier = "@prover-coder-ai/docker-git-session-sync/package.json"
2220
2321const fallbackHostResources = {
2422 cpuCount : 1 ,
@@ -145,26 +143,37 @@ const provisionDockerGitScripts = (
145143 }
146144 } )
147145
148- const resolveInstalledSessionSyncTool = ( ) : string | null => {
149- try {
150- const packageJsonPath = require . resolve ( "@prover-coder-ai/docker-git-session-sync/package.json" )
151- return nodePath . join ( nodePath . dirname ( packageJsonPath ) , "dist" , "docker-git-session-sync.js" )
152- } catch {
153- return null
154- }
146+ const resolveFileUrlPath = ( fileUrl : string ) : string => {
147+ const url = new URL ( fileUrl )
148+ return url . protocol === "file:" ? decodeURIComponent ( url . pathname ) : fileUrl
155149}
156150
157- const sessionSyncToolCandidates = ( path : Path . Path , workspaceRoot : string ) : ReadonlyArray < string > => {
158- const installed = resolveInstalledSessionSyncTool ( )
159- const workspaceCandidate = path . join (
160- workspaceRoot ,
161- "packages" ,
162- "docker-git-session-sync" ,
163- "dist" ,
164- "docker-git-session-sync.js"
151+ const resolveInstalledSessionSyncTool = ( path : Path . Path ) : Effect . Effect < string | null > =>
152+ Effect . try ( ( ) => import . meta. resolve ( sessionSyncPackageJsonSpecifier ) ) . pipe (
153+ Effect . match ( {
154+ onFailure : ( ) => null ,
155+ onSuccess : ( packageJsonUrl ) => {
156+ const packageJsonPath = resolveFileUrlPath ( packageJsonUrl )
157+ return path . join ( path . dirname ( packageJsonPath ) , "dist" , "docker-git-session-sync.js" )
158+ }
159+ } )
165160 )
166- return installed === null ? [ workspaceCandidate ] : [ workspaceCandidate , installed ]
167- }
161+
162+ const sessionSyncToolCandidates = (
163+ path : Path . Path ,
164+ workspaceRoot : string
165+ ) : Effect . Effect < ReadonlyArray < string > > =>
166+ Effect . gen ( function * ( _ ) {
167+ const installed = yield * _ ( resolveInstalledSessionSyncTool ( path ) )
168+ const workspaceCandidate = path . join (
169+ workspaceRoot ,
170+ "packages" ,
171+ "docker-git-session-sync" ,
172+ "dist" ,
173+ "docker-git-session-sync.js"
174+ )
175+ return installed === null ? [ workspaceCandidate ] : [ workspaceCandidate , installed ]
176+ } )
168177
169178// CHANGE: provision standalone session sync tool into the Docker build context
170179// WHY: generated containers call docker-git-session-sync directly after git push
@@ -181,7 +190,8 @@ const provisionDockerGitSessionSyncTool = (
181190 Effect . gen ( function * ( _ ) {
182191 const workspaceRoot = yield * _ ( resolveWorkspaceRoot ( process . cwd ( ) ) )
183192 const targetPath = path . join ( baseDir , sessionSyncToolRelativePath )
184- for ( const sourcePath of sessionSyncToolCandidates ( path , workspaceRoot ) ) {
193+ const candidates = yield * _ ( sessionSyncToolCandidates ( path , workspaceRoot ) )
194+ for ( const sourcePath of candidates ) {
185195 const exists = yield * _ ( fs . exists ( sourcePath ) )
186196 if ( exists ) {
187197 const contents = yield * _ ( fs . readFileString ( sourcePath ) )
@@ -190,7 +200,11 @@ const provisionDockerGitSessionSyncTool = (
190200 return
191201 }
192202 }
193- yield * _ ( Effect . dieMessage ( "docker-git-session-sync build artifact not found; run bun run --cwd packages/docker-git-session-sync build" ) )
203+ yield * _ (
204+ Effect . dieMessage (
205+ "docker-git-session-sync build artifact not found; run bun run --cwd packages/docker-git-session-sync build"
206+ )
207+ )
194208 } )
195209
196210// CHANGE: write generated docker-git files to disk
0 commit comments