Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import * as BunPath from '@effect/platform-bun/BunPath'; |
| 2 | import * as BunFileSystem from '@effect/platform-bun/BunFileSystem'; |
| 3 | import * as FileSystem from 'effect/FileSystem'; |
| 4 | import * as Path from 'effect/Path'; |
| 5 | import { Data, Effect, Match, Context, Layer } from 'effect'; |
| 6 | import { ExpectedFailure } from 'src/effects/expected-failure'; |
| 7 | import { getAncestors } from 'src/utils/get-ancestors'; |
| 8 | import { HOST_CONFIG } from 'src/config'; |
| 9 | |
| 10 | const toError = (e: unknown): Error => (e instanceof Error ? e : new Error(String(e))); |
| 11 | |
| 12 | export type JsLanguage = 'typescript' | 'javascript'; |
| 13 | export type JsPackageManager = 'pnpm' | 'bun' | 'yarn' | 'npm' | 'deno'; |
| 14 | export type PythonPackageManager = 'uv' | 'pip'; |
| 15 | |
| 16 | export type ProjectEnvironment = |
| 17 | | { |
| 18 | kind: 'js'; |
| 19 | language: JsLanguage; |
| 20 | packageManager: JsPackageManager; |
| 21 | rootDir: string; |
| 22 | evidence: string[]; |
| 23 | } |
| 24 | | { |
| 25 | kind: 'python'; |
| 26 | language: 'python'; |
| 27 | packageManager: PythonPackageManager; |
| 28 | rootDir: string; |
| 29 | evidence: string[]; |
| 30 | }; |
| 31 | |
| 32 | export class ProjectEnvironmentDetectorError extends Data.TaggedError( |
| 33 | 'services/ProjectEnvironmentDetectorError' |
| 34 | )<{ |
| 35 | readonly cause: Error; |
| 36 | readonly message: string; |
| 37 | readonly checkedPaths: string[]; |
| 38 | readonly details?: string; |
| 39 | }> { |
| 40 | get [ExpectedFailure]() { |
| 41 | return true; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // --------------------------------------------------------------------------- |
| 46 | // Constants |
| 47 | // --------------------------------------------------------------------------- |
| 48 | |
| 49 | const JS_LOCK_FILES: Record<JsPackageManager, string[]> = { |
| 50 | pnpm: ['pnpm-lock.yaml', 'pnpm-workspace.yaml'], |
| 51 | bun: ['bun.lockb', 'bun.lock'], |
| 52 | yarn: ['yarn.lock'], |
| 53 | npm: ['package-lock.json', 'npm-shrinkwrap.json'], |
| 54 | deno: ['deno.lock', 'deno.lock.json'], |
| 55 | }; |
| 56 | |
| 57 | const PYTHON_INDICATORS = new Set([ |
| 58 | 'pyproject.toml', |
| 59 | 'setup.py', |
| 60 | 'setup.cfg', |
| 61 | 'requirements.txt', |
| 62 | 'requirements-dev.txt', |
| 63 | 'requirements.in', |
| 64 | 'pipfile', |
| 65 | 'pipfile.lock', |
| 66 | 'poetry.lock', |
| 67 | 'uv.lock', |
| 68 | 'environment.yml', |
| 69 | 'conda.yaml', |
| 70 | 'tox.ini', |
| 71 | 'pytest.ini', |
| 72 | ]); |
| 73 | |
| 74 | const JS_CONFIG_FILES = new Set(['jsconfig.json', 'deno.json', 'deno.jsonc']); |
| 75 | |
| 76 | const COMMON_SOURCE_DIRS = ['src', 'lib', 'app', 'apps', 'packages', 'scripts']; |
| 77 | |
| 78 | // --------------------------------------------------------------------------- |
| 79 | // Internal types |
| 80 | // --------------------------------------------------------------------------- |
| 81 | |
| 82 | type DirEvidence = { |
| 83 | dir: string; |
| 84 | jsScore: number; |
| 85 | tsScore: number; |
| 86 | pyScore: number; |
| 87 | strongJs: boolean; |
| 88 | strongPy: boolean; |
| 89 | jsHints: { |
| 90 | tsConfig: boolean; |
| 91 | tsDependency: boolean; |
| 92 | tsFiles: boolean; |
| 93 | jsFiles: boolean; |
| 94 | denoConfig: boolean; |
| 95 | }; |
| 96 | evidence: string[]; |
| 97 | }; |
| 98 | |
| 99 | // --------------------------------------------------------------------------- |
| 100 | // Pure helpers (no dependencies) |
| 101 | // --------------------------------------------------------------------------- |
| 102 | |
| 103 | const isTsConfig = (name: string) => { |
| 104 | const lower = name.toLowerCase(); |
| 105 | return lower === 'tsconfig.json' || (lower.startsWith('tsconfig.') && lower.endsWith('.json')); |
| 106 | }; |
| 107 | |
| 108 | const countExtensions = (path: Path.Path, files: string[]) => { |
| 109 | let ts = 0; |
| 110 | let js = 0; |
| 111 | let py = 0; |
| 112 | |
| 113 | for (const file of files) { |
| 114 | const ext = path.extname(file).toLowerCase(); |
| 115 | if (ext === '.ts' || ext === '.tsx' || ext === '.mts') { |
| 116 | ts += 1; |
| 117 | } else if (ext === '.js' || ext === '.jsx' || ext === '.mjs') { |
| 118 | js += 1; |
| 119 | } else if (ext === '.py' || ext === '.pyi' || ext === '.pyw' || ext === '.pyx') { |
| 120 | py += 1; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return { ts, js, py }; |
| 125 | }; |
| 126 | |
| 127 | const chooseJsLanguage = (evidence: DirEvidence): JsLanguage => { |
| 128 | if (evidence.tsScore > evidence.jsScore) return 'typescript'; |
| 129 | if (evidence.jsScore > evidence.tsScore) return 'javascript'; |
| 130 | if (evidence.jsHints.tsConfig || evidence.jsHints.tsDependency) return 'typescript'; |
| 131 | if (evidence.jsHints.tsFiles && !evidence.jsHints.jsFiles) return 'typescript'; |
| 132 | if (evidence.jsHints.jsFiles && !evidence.jsHints.tsFiles) return 'javascript'; |
| 133 | if (evidence.jsHints.denoConfig) return 'typescript'; |
| 134 | return 'javascript'; |
| 135 | }; |
| 136 | |
| 137 | const pickBestWeak = (candidate: DirEvidence, depth: number) => { |
| 138 | const bestScore = Math.max(candidate.tsScore, candidate.jsScore, candidate.pyScore); |
| 139 | if (bestScore <= 0) return null; |
| 140 | const language = |
| 141 | candidate.pyScore >= candidate.tsScore && candidate.pyScore >= candidate.jsScore |
| 142 | ? 'python' |
| 143 | : chooseJsLanguage(candidate); |
| 144 | return { |
| 145 | dir: candidate.dir, |
| 146 | depth, |
| 147 | language, |
| 148 | score: bestScore - depth * 0.5, |
| 149 | evidence: candidate, |
| 150 | } as const; |
| 151 | }; |
| 152 | |
| 153 | /** Known system paths that must not be treated as project roots. */ |
| 154 | const SYSTEM_ROOTS = new Set([ |
| 155 | '/', |
| 156 | '/tmp', |
| 157 | '/var', |
| 158 | '/usr', |
| 159 | '/private', |
| 160 | '/private/tmp', |
| 161 | '/private/var', |
| 162 | ]); |
| 163 | |
| 164 | /** Reject system roots (/, /tmp, /var, /private/tmp, etc.) as project roots. */ |
| 165 | const isSystemRoot = (path: Path.Path, dir: string): boolean => { |
| 166 | const resolved = path.resolve(dir); |
| 167 | if (SYSTEM_ROOTS.has(resolved)) return true; |
| 168 | const parent = path.dirname(resolved); |
| 169 | if (parent === resolved) return true; // root |
| 170 | return parent === path.resolve('/'); // direct child of root, e.g. /tmp, /var, /usr |
| 171 | }; |
| 172 | |
| 173 | const parsePackageManagerField = (pm: string) => |
| 174 | Match.value(pm).pipe( |
| 175 | Match.when( |
| 176 | pm => pm.startsWith('pnpm@'), |
| 177 | () => 'pnpm' as const |
| 178 | ), |
| 179 | Match.when( |
| 180 | pm => pm.startsWith('bun@'), |
| 181 | () => 'bun' as const |
| 182 | ), |
| 183 | Match.when( |
| 184 | pm => pm.startsWith('yarn@'), |
| 185 | () => 'yarn' as const |
| 186 | ), |
| 187 | Match.when( |
| 188 | pm => pm.startsWith('npm@'), |
| 189 | () => 'npm' as const |
| 190 | ), |
| 191 | Match.orElse(() => null) |
| 192 | ); |
| 193 | |
| 194 | /** |
| 195 | * Detect package manager from the `npm_config_user_agent` env var. |
| 196 | * Set automatically when running via `npx`, `pnpm dlx`, `bunx`, etc. |
| 197 | * Inspired by https://github.com/prisma/create-prisma/blob/main/src/utils/package-manager.ts |
| 198 | */ |
| 199 | const parseUserAgent = (userAgent: string | undefined): JsPackageManager | null => { |
| 200 | if (!userAgent) return null; |
| 201 | if (userAgent.startsWith('pnpm')) return 'pnpm'; |
| 202 | if (userAgent.startsWith('bun')) return 'bun'; |
| 203 | if (userAgent.startsWith('yarn')) return 'yarn'; |
| 204 | if (userAgent.startsWith('npm')) return 'npm'; |
| 205 | return null; |
| 206 | }; |
| 207 | |
| 208 | // --------------------------------------------------------------------------- |
| 209 | // FS helpers (take FileSystem as parameter) |
| 210 | // --------------------------------------------------------------------------- |
| 211 | |
| 212 | const makeReadDirectoryOptional = (fs: FileSystem.FileSystem) => (dir: string) => |
| 213 | fs.readDirectory(dir).pipe(Effect.catch(() => Effect.succeed<string[]>([]))); |
| 214 | |
| 215 | const makeReadFileStringOptional = (fs: FileSystem.FileSystem) => (filePath: string) => |
| 216 | fs.readFileString(filePath).pipe(Effect.catch(() => Effect.succeed<string | null>(null))); |
| 217 | |
| 218 | const makeReadPackageJson = (fs: FileSystem.FileSystem, path: Path.Path) => { |
| 219 | const readFileStringOptional = makeReadFileStringOptional(fs); |
| 220 | return (dir: string) => |
| 221 | Effect.gen(function* () { |
| 222 | const content = yield* readFileStringOptional(path.join(dir, 'package.json')); |
| 223 | if (!content) return null; |
| 224 | return yield* Effect.try({ |
| 225 | try: () => JSON.parse(content) as Record<string, unknown>, |
| 226 | catch: toError, |
| 227 | }).pipe(Effect.catch(() => Effect.succeed(null))); |
| 228 | }); |
| 229 | }; |
| 230 | |
| 231 | // --------------------------------------------------------------------------- |
| 232 | // Analysis (split into focused functions) |
| 233 | // --------------------------------------------------------------------------- |
| 234 | |
| 235 | const analyzeDirectory = ( |
| 236 | fs: FileSystem.FileSystem, |
| 237 | path: Path.Path, |
| 238 | dir: string |
| 239 | ): Effect.Effect<DirEvidence, ProjectEnvironmentDetectorError> => |
| 240 | Effect.gen(function* () { |
| 241 | const files = yield* fs.readDirectory(dir).pipe( |
| 242 | Effect.mapError( |
| 243 | e => |
| 244 | new ProjectEnvironmentDetectorError({ |
| 245 | cause: toError(e), |
| 246 | message: `Failed to read directory ${dir}`, |
| 247 | checkedPaths: [dir], |
| 248 | }) |
| 249 | ) |
| 250 | ); |
| 251 | const readDirectoryOptional = makeReadDirectoryOptional(fs); |
| 252 | const readFileStringOptional = makeReadFileStringOptional(fs); |
| 253 | const readPackageJson = makeReadPackageJson(fs, path); |
| 254 | |
| 255 | const lowerFiles = files.map(file => file.toLowerCase()); |
| 256 | const fileSet = new Set(lowerFiles); |
| 257 | const fileByLower = new Map<string, string>(); |
| 258 | for (const file of files) { |
| 259 | const lower = file.toLowerCase(); |
| 260 | if (!fileByLower.has(lower)) fileByLower.set(lower, file); |
| 261 | } |
| 262 | |
| 263 | let jsScore = 0; |
| 264 | let tsScore = 0; |
| 265 | let pyScore = 0; |
| 266 | let strongJs = false; |
| 267 | let strongPy = false; |
| 268 | const evidence: string[] = []; |
| 269 | const jsHints = { |
| 270 | tsConfig: false, |
| 271 | tsDependency: false, |
| 272 | tsFiles: false, |
| 273 | jsFiles: false, |
| 274 | denoConfig: false, |
| 275 | }; |
| 276 | |
| 277 | // Score file indicators |
| 278 | for (const file of lowerFiles) { |
| 279 | if (file === 'package.json') { |
| 280 | strongJs = true; |
| 281 | jsScore += 3; |
| 282 | evidence.push('package.json'); |
| 283 | } |
| 284 | if (JS_CONFIG_FILES.has(file)) { |
| 285 | strongJs = true; |
| 286 | jsScore += 3; |
| 287 | if (file === 'deno.json' || file === 'deno.jsonc') { |
| 288 | jsHints.denoConfig = true; |
| 289 | tsScore += 2; |
| 290 | evidence.push(file); |
| 291 | } |
| 292 | } |
| 293 | if (isTsConfig(file)) { |
| 294 | strongJs = true; |
| 295 | tsScore += 5; |
| 296 | jsHints.tsConfig = true; |
| 297 | evidence.push('tsconfig'); |
| 298 | } |
| 299 | if (file === 'jsconfig.json') { |
| 300 | strongJs = true; |
| 301 | jsScore += 4; |
| 302 | evidence.push('jsconfig.json'); |
| 303 | } |
| 304 | if (file === 'pnpm-workspace.yaml') { |
| 305 | strongJs = true; |
| 306 | jsScore += 3; |
| 307 | evidence.push('pnpm-workspace.yaml'); |
| 308 | } |
| 309 | if (PYTHON_INDICATORS.has(file)) { |
| 310 | strongPy = true; |
| 311 | pyScore += 3; |
| 312 | evidence.push(file); |
| 313 | if (file === 'uv.lock') pyScore += 2; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Score JS lock files |
| 318 | for (const [, lockFiles] of Object.entries(JS_LOCK_FILES)) { |
| 319 | if (lockFiles.some(lf => fileSet.has(lf))) { |
| 320 | strongJs = true; |
| 321 | jsScore += 4; |
| 322 | evidence.push(lockFiles.find(lf => fileSet.has(lf)) ?? lockFiles[0]); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | // Score from package.json content |
| 327 | const packageJson = yield* readPackageJson(dir); |
| 328 | if (packageJson) { |
| 329 | const deps = (packageJson.dependencies ?? {}) as Record<string, string>; |
| 330 | const devDeps = (packageJson.devDependencies ?? {}) as Record<string, string>; |
| 331 | if ({ ...deps, ...devDeps }.typescript) { |
| 332 | tsScore += 4; |
| 333 | jsHints.tsDependency = true; |
| 334 | evidence.push('typescript dependency'); |
| 335 | } |
| 336 | const pm = |
| 337 | typeof packageJson.packageManager === 'string' |
| 338 | ? parsePackageManagerField(packageJson.packageManager) |
| 339 | : null; |
| 340 | if (pm) { |
| 341 | jsScore += 2; |
| 342 | strongJs = true; |
| 343 | evidence.push(`packageManager:${pm}`); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | // Score from pyproject.toml content |
| 348 | const pyProject = yield* readFileStringOptional(path.join(dir, 'pyproject.toml')); |
| 349 | if (pyProject) { |
| 350 | const hasPythonSections = [ |
| 351 | '[project]', |
| 352 | '[tool.poetry]', |
| 353 | '[tool.setuptools]', |
| 354 | '[tool.uv]', |
| 355 | '[build-system]', |
| 356 | ].some(s => pyProject.includes(s)); |
| 357 | if (hasPythonSections) { |
| 358 | strongPy = true; |
| 359 | pyScore += 2; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | // Score from file extensions |
| 364 | const extensionCounts = countExtensions(path, files); |
| 365 | if (extensionCounts.ts > 0) { |
| 366 | tsScore += Math.min(3, extensionCounts.ts); |
| 367 | jsHints.tsFiles = true; |
| 368 | } |
| 369 | if (extensionCounts.js > 0) { |
| 370 | jsScore += Math.min(3, extensionCounts.js); |
| 371 | jsHints.jsFiles = true; |
| 372 | } |
| 373 | if (extensionCounts.py > 0) { |
| 374 | pyScore += Math.min(3, extensionCounts.py); |
| 375 | } |
| 376 | |
| 377 | // Score from subdirectory extensions |
| 378 | for (const subdir of COMMON_SOURCE_DIRS) { |
| 379 | if (!fileSet.has(subdir)) continue; |
| 380 | const actualSubdir = fileByLower.get(subdir) ?? subdir; |
| 381 | const subFiles = yield* readDirectoryOptional(path.join(dir, actualSubdir)); |
| 382 | const subCounts = countExtensions(path, subFiles); |
| 383 | if (subCounts.ts > 0) { |
| 384 | tsScore += Math.min(2, subCounts.ts); |
| 385 | jsHints.tsFiles = true; |
| 386 | } |
| 387 | if (subCounts.js > 0) { |
| 388 | jsScore += Math.min(2, subCounts.js); |
| 389 | jsHints.jsFiles = true; |
| 390 | } |
| 391 | if (subCounts.py > 0) { |
| 392 | pyScore += Math.min(2, subCounts.py); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | return { |
| 397 | dir, |
| 398 | jsScore, |
| 399 | tsScore, |
| 400 | pyScore, |
| 401 | strongJs, |
| 402 | strongPy, |
| 403 | jsHints, |
| 404 | evidence, |
| 405 | } satisfies DirEvidence; |
| 406 | }); |
| 407 | |
| 408 | // --------------------------------------------------------------------------- |
| 409 | // Detection functions |
| 410 | // --------------------------------------------------------------------------- |
| 411 | |
| 412 | const detectLanguage = (fs: FileSystem.FileSystem, cwd: string) => |
| 413 | Effect.gen(function* () { |
| 414 | const path = yield* Path.Path; |
| 415 | const dirs = yield* getAncestors(cwd); |
| 416 | let bestWeak: ReturnType<typeof pickBestWeak> | null = null; |
| 417 | let ambiguous: { dir: string; details: string } | null = null; |
| 418 | |
| 419 | for (const [depth, dir] of dirs.entries()) { |
| 420 | const evidence = yield* analyzeDirectory(fs, path, dir); |
| 421 | const systemRoot = isSystemRoot(path, dir); |
| 422 | |
| 423 | if (evidence.strongJs && evidence.strongPy) { |
| 424 | if (depth === 0 && !systemRoot) { |
| 425 | return yield* Effect.fail( |
| 426 | new ProjectEnvironmentDetectorError({ |
| 427 | cause: new Error('Ambiguous project language'), |
| 428 | message: `Detected both JavaScript/TypeScript and Python indicators in ${dir}`, |
| 429 | details: `Indicators: ${evidence.evidence.join(', ')}`, |
| 430 | checkedPaths: dirs, |
| 431 | }) |
| 432 | ); |
| 433 | } |
| 434 | if (!systemRoot && !ambiguous) |
| 435 | ambiguous = { dir, details: `Indicators: ${evidence.evidence.join(', ')}` }; |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | if (!systemRoot && evidence.strongJs) |
| 440 | return { language: chooseJsLanguage(evidence), rootDir: dir, evidence } as const; |
| 441 | if (!systemRoot && evidence.strongPy) |
| 442 | return { language: 'python' as const, rootDir: dir, evidence } as const; |
| 443 | |
| 444 | // Only allow weak detection at the requested cwd. Walking weak signals up |
| 445 | // arbitrary ancestor directories causes false positives in temp dirs and |
| 446 | // other shared parent paths outside the actual project. |
| 447 | const weakCandidate = depth === 0 && !systemRoot ? pickBestWeak(evidence, depth) : null; |
| 448 | if (weakCandidate && (!bestWeak || weakCandidate.score > bestWeak.score)) |
| 449 | bestWeak = weakCandidate; |
| 450 | } |
| 451 | |
| 452 | if (bestWeak && !isSystemRoot(path, bestWeak.dir)) |
| 453 | return { |
| 454 | language: bestWeak.language, |
| 455 | rootDir: bestWeak.dir, |
| 456 | evidence: bestWeak.evidence, |
| 457 | } as const; |
| 458 | |
| 459 | if (ambiguous) { |
| 460 | return yield* Effect.fail( |
| 461 | new ProjectEnvironmentDetectorError({ |
| 462 | cause: new Error('Ambiguous project language'), |
| 463 | message: `Detected both JavaScript/TypeScript and Python indicators in ${ambiguous.dir}`, |
| 464 | details: ambiguous.details, |
| 465 | checkedPaths: dirs, |
| 466 | }) |
| 467 | ); |
| 468 | } |
| 469 | |
| 470 | return yield* Effect.fail( |
| 471 | new ProjectEnvironmentDetectorError({ |
| 472 | cause: new Error('No project language detected'), |
| 473 | message: `No recognizable JavaScript/TypeScript or Python project indicators found in ${cwd}`, |
| 474 | checkedPaths: dirs, |
| 475 | }) |
| 476 | ); |
| 477 | }); |
| 478 | |
| 479 | const detectJsPackageManager = (fs: FileSystem.FileSystem, cwd: string) => |
| 480 | Effect.gen(function* () { |
| 481 | const path = yield* Path.Path; |
| 482 | const readDirectoryOptional = makeReadDirectoryOptional(fs); |
| 483 | const readPackageJson = makeReadPackageJson(fs, path); |
| 484 | const dirs = yield* getAncestors(cwd); |
| 485 | |
| 486 | for (const dir of dirs) { |
| 487 | const files = yield* readDirectoryOptional(dir); |
| 488 | const fileSet = new Set(files.map(f => f.toLowerCase())); |
| 489 | |
| 490 | if (fileSet.has('package.json')) { |
| 491 | const pkg = yield* readPackageJson(dir); |
| 492 | if (pkg && typeof pkg.packageManager === 'string') { |
| 493 | const pm = parsePackageManagerField(pkg.packageManager); |
| 494 | if (pm) return pm; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | if (fileSet.has('deno.json') || fileSet.has('deno.jsonc') || fileSet.has('deno.lock')) |
| 499 | return 'deno' as const; |
| 500 | |
| 501 | for (const pm of ['pnpm', 'bun', 'yarn', 'npm'] as const) { |
| 502 | if (JS_LOCK_FILES[pm].some(lf => fileSet.has(lf))) return pm; |
| 503 | } |
| 504 | |
| 505 | if (fileSet.has('pnpm-workspace.yaml')) return 'pnpm' as const; |
| 506 | } |
| 507 | |
| 508 | const userAgent = parseUserAgent(yield* Effect.orDie(HOST_CONFIG.NPM_CONFIG_USER_AGENT)); |
| 509 | if (userAgent) return userAgent; |
| 510 | |
| 511 | return 'npm' as const; |
| 512 | }); |
| 513 | |
| 514 | const detectPythonPackageManager = (fs: FileSystem.FileSystem, cwd: string) => |
| 515 | Effect.gen(function* () { |
| 516 | const path = yield* Path.Path; |
| 517 | const readDirectoryOptional = makeReadDirectoryOptional(fs); |
| 518 | const readFileStringOptional = makeReadFileStringOptional(fs); |
| 519 | const dirs = yield* getAncestors(cwd); |
| 520 | let fallback: PythonPackageManager | null = null; |
| 521 | |
| 522 | for (const dir of dirs) { |
| 523 | const files = yield* readDirectoryOptional(dir); |
| 524 | const fileSet = new Set(files.map(f => f.toLowerCase())); |
| 525 | |
| 526 | if (fileSet.has('uv.lock')) return 'uv' as const; |
| 527 | |
| 528 | if (fileSet.has('pyproject.toml')) { |
| 529 | const content = yield* readFileStringOptional(path.join(dir, 'pyproject.toml')); |
| 530 | if (content?.includes('[tool.uv]')) return 'uv' as const; |
| 531 | } |
| 532 | |
| 533 | if ( |
| 534 | [ |
| 535 | 'requirements.txt', |
| 536 | 'requirements-dev.txt', |
| 537 | 'requirements.in', |
| 538 | 'setup.py', |
| 539 | 'setup.cfg', |
| 540 | 'pipfile', |
| 541 | 'pipfile.lock', |
| 542 | 'poetry.lock', |
| 543 | 'environment.yml', |
| 544 | 'conda.yaml', |
| 545 | ].some(f => fileSet.has(f)) |
| 546 | ) { |
| 547 | fallback = 'pip'; |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | return fallback ?? 'pip'; |
| 552 | }); |
| 553 | |
| 554 | // --------------------------------------------------------------------------- |
| 555 | // Service |
| 556 | // --------------------------------------------------------------------------- |
| 557 | |
| 558 | const makeProjectEnvironmentDetector = Effect.gen(function* () { |
| 559 | const fs = yield* FileSystem.FileSystem; |
| 560 | const path = yield* Path.Path; |
| 561 | |
| 562 | const detectProjectEnvironment = ( |
| 563 | cwd: string |
| 564 | ): Effect.Effect<ProjectEnvironment, ProjectEnvironmentDetectorError> => |
| 565 | Effect.gen(function* () { |
| 566 | const detection = yield* detectLanguage(fs, cwd); |
| 567 | |
| 568 | if (detection.language === 'python') { |
| 569 | const packageManager = yield* detectPythonPackageManager(fs, detection.rootDir); |
| 570 | return { |
| 571 | kind: 'python', |
| 572 | language: 'python', |
| 573 | packageManager, |
| 574 | rootDir: detection.rootDir, |
| 575 | evidence: detection.evidence.evidence, |
| 576 | } satisfies ProjectEnvironment; |
| 577 | } |
| 578 | |
| 579 | const packageManager = yield* detectJsPackageManager(fs, detection.rootDir); |
| 580 | return { |
| 581 | kind: 'js', |
| 582 | language: detection.language, |
| 583 | packageManager, |
| 584 | rootDir: detection.rootDir, |
| 585 | evidence: detection.evidence.evidence, |
| 586 | } satisfies ProjectEnvironment; |
| 587 | }).pipe(Effect.provideService(Path.Path, path)); |
| 588 | |
| 589 | return { |
| 590 | detectProjectEnvironment, |
| 591 | detectJsPackageManager: (cwd: string) => |
| 592 | detectJsPackageManager(fs, cwd).pipe(Effect.provideService(Path.Path, path)), |
| 593 | detectPythonPackageManager: (cwd: string) => |
| 594 | detectPythonPackageManager(fs, cwd).pipe(Effect.provideService(Path.Path, path)), |
| 595 | }; |
| 596 | }); |
| 597 | |
| 598 | export type ProjectEnvironmentDetectorShape = Effect.Success<typeof makeProjectEnvironmentDetector>; |
| 599 | |
| 600 | export class ProjectEnvironmentDetector extends Context.Service< |
| 601 | ProjectEnvironmentDetector, |
| 602 | ProjectEnvironmentDetectorShape |
| 603 | >()('services/ProjectEnvironmentDetector') { |
| 604 | static readonly Default = Layer.effect( |
| 605 | ProjectEnvironmentDetector, |
| 606 | makeProjectEnvironmentDetector |
| 607 | ).pipe(Layer.provide(Layer.mergeAll(BunFileSystem.layer, BunPath.layer))); |
| 608 | } |