npm-config.ts 1.3 KB

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