npm-config.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export * as NpmConfig from "./npm-config.js"
  2. import { fileURLToPath } from "url"
  3. import { Effect } from "effect"
  4. export const load = (dir: string) =>
  5. Effect.tryPromise({
  6. try: async () => {
  7. // @ts-expect-error npm does not publish types for this internal config API.
  8. const { default: Config } = await import("@npmcli/config")
  9. // @ts-expect-error npm does not publish types for this internal config API.
  10. const { default: npmDefinitions } = await import("@npmcli/config/lib/definitions/index.js")
  11. const { definitions, flatten, nerfDarts, shorthands } = npmDefinitions
  12. const config = new Config({
  13. // Resolved per call: on workerd import.meta.url is undefined and building
  14. // this URL at module scope fails startup validation; npm config never runs there.
  15. npmPath: fileURLToPath(new URL("..", import.meta.url)),
  16. cwd: dir,
  17. env: { ...process.env },
  18. argv: [process.execPath, process.execPath, "--prefix", dir],
  19. execPath: process.execPath,
  20. platform: process.platform,
  21. definitions,
  22. flatten,
  23. nerfDarts,
  24. shorthands,
  25. warn: false,
  26. })
  27. await config.load()
  28. return config.flat as Record<string, unknown>
  29. },
  30. catch: (cause) => cause,
  31. }).pipe(Effect.orElseSucceed(() => ({}) as Record<string, unknown>))
  32. export const registry = (dir: string) =>
  33. load(dir).pipe(
  34. Effect.map((config) => {
  35. const registry = typeof config.registry === "string" ? config.registry : "https://registry.npmjs.org"
  36. return registry.endsWith("/") ? registry.slice(0, -1) : registry
  37. }),
  38. )