index.d.mts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. type PackageManagerName = "npm" | "yarn" | "pnpm" | "bun" | "deno";
  2. type PackageManager = {
  3. name: PackageManagerName;
  4. command: string;
  5. version?: string;
  6. buildMeta?: string;
  7. majorVersion?: string;
  8. lockFile?: string | string[];
  9. files?: string[];
  10. };
  11. type OperationOptions = {
  12. cwd?: string;
  13. silent?: boolean;
  14. packageManager?: PackageManager | PackageManagerName;
  15. installPeerDependencies?: boolean;
  16. dev?: boolean;
  17. workspace?: boolean | string;
  18. global?: boolean;
  19. };
  20. type DetectPackageManagerOptions = {
  21. /**
  22. * Whether to ignore the lock file
  23. *
  24. * @default false
  25. */
  26. ignoreLockFile?: boolean;
  27. /**
  28. * Whether to ignore the package.json file
  29. *
  30. * @default false
  31. */
  32. ignorePackageJSON?: boolean;
  33. /**
  34. * Whether to include parent directories
  35. *
  36. * @default false
  37. */
  38. includeParentDirs?: boolean;
  39. /**
  40. * Weather to ignore argv[1] to detect script
  41. */
  42. ignoreArgv?: boolean;
  43. };
  44. declare const packageManagers: PackageManager[];
  45. /**
  46. * Detect the package manager used in a directory (and up) by checking various sources:
  47. *
  48. * 1. Use `packageManager` field from package.json
  49. *
  50. * 2. Known lock files and other files
  51. */
  52. declare function detectPackageManager(cwd: string, options?: DetectPackageManagerOptions): Promise<(PackageManager & {
  53. warnings?: string[];
  54. }) | undefined>;
  55. /**
  56. * Installs project dependencies.
  57. *
  58. * @param options - Options to pass to the API call.
  59. * @param options.cwd - The directory to run the command in.
  60. * @param options.silent - Whether to run the command in silent mode.
  61. * @param options.packageManager - The package manager info to use (auto-detected).
  62. * @param options.frozenLockFile - Whether to install dependencies with frozen lock file.
  63. */
  64. declare function installDependencies(options?: Pick<OperationOptions, "cwd" | "silent" | "packageManager"> & {
  65. frozenLockFile?: boolean;
  66. }): Promise<void>;
  67. /**
  68. * Adds dependency to the project.
  69. *
  70. * @param name - Name of the dependency to add.
  71. * @param options - Options to pass to the API call.
  72. * @param options.cwd - The directory to run the command in.
  73. * @param options.silent - Whether to run the command in silent mode.
  74. * @param options.packageManager - The package manager info to use (auto-detected).
  75. * @param options.dev - Whether to add the dependency as dev dependency.
  76. * @param options.workspace - The name of the workspace to use.
  77. * @param options.global - Whether to run the command in global mode.
  78. */
  79. declare function addDependency(name: string | string[], options?: OperationOptions): Promise<void>;
  80. /**
  81. * Adds dev dependency to the project.
  82. *
  83. * @param name - Name of the dev dependency to add.
  84. * @param options - Options to pass to the API call.
  85. * @param options.cwd - The directory to run the command in.
  86. * @param options.silent - Whether to run the command in silent mode.
  87. * @param options.packageManager - The package manager info to use (auto-detected).
  88. * @param options.workspace - The name of the workspace to use.
  89. * @param options.global - Whether to run the command in global mode.
  90. *
  91. */
  92. declare function addDevDependency(name: string | string[], options?: Omit<OperationOptions, "dev">): Promise<void>;
  93. /**
  94. * Removes dependency from the project.
  95. *
  96. * @param name - Name of the dependency to remove.
  97. * @param options - Options to pass to the API call.
  98. * @param options.cwd - The directory to run the command in.
  99. * @param options.silent - Whether to run the command in silent mode.
  100. * @param options.packageManager - The package manager info to use (auto-detected).
  101. * @param options.dev - Whether to remove dev dependency.
  102. * @param options.workspace - The name of the workspace to use.
  103. * @param options.global - Whether to run the command in global mode.
  104. */
  105. declare function removeDependency(name: string, options?: OperationOptions): Promise<void>;
  106. /**
  107. * Ensures dependency is installed.
  108. *
  109. * @param name - Name of the dependency.
  110. * @param options - Options to pass to the API call.
  111. * @param options.cwd - The directory to run the command in.
  112. * @param options.dev - Whether to install as dev dependency (if not already installed).
  113. * @param options.workspace - The name of the workspace to install dependency in (if not already installed).
  114. */
  115. declare function ensureDependencyInstalled(name: string, options?: Pick<OperationOptions, "cwd" | "dev" | "workspace">): Promise<true | undefined>;
  116. declare function dedupeDependencies(options?: Pick<OperationOptions, "cwd" | "silent"> & {
  117. recreateLockfile?: boolean;
  118. }): Promise<void>;
  119. declare function runScript(name: string, options?: Pick<OperationOptions, "cwd" | "silent" | "packageManager">): Promise<void>;
  120. export { type DetectPackageManagerOptions, type OperationOptions, type PackageManager, type PackageManagerName, addDependency, addDevDependency, dedupeDependencies, detectPackageManager, ensureDependencyInstalled, installDependencies, packageManagers, removeDependency, runScript };