package-management 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,55 +1,21 @@
1
- import { InstallPackageOptions } from '@antfu/install-pkg';
2
1
  import { AsyncCacheFn as AsyncCacheFn$1 } from 'async-cache-fn';
3
2
  import { Options } from 'execa';
4
-
5
- interface ImportPackageOption<T = any> {
6
- /**
7
- * The name of the package to import. This should match the name used in the import statement.
8
- */
9
- name: string;
10
- /**
11
- * When enabled, the package will be installed if it is not found
12
- * @default true
13
- */
14
- install?: boolean;
15
- /**
16
- * The import statement for the package.
17
- */
18
- import: () => ImportStatement<T>;
19
- /**
20
- * When enabled, the package will be installed as a dev dependency
21
- */
22
- isDevDependency?: boolean;
23
- /**
24
- * Additional installation options
25
- */
26
- installOptions?: Omit<InstallPackageOptions, "dev">;
27
- }
28
- type ImportMap = Record<string, ImportOption>;
29
- type ImportList = ImportOption[];
30
- type ImportOption<T = any> = ImportStatement<T> | (() => ImportStatement<T>) | ImportPackageOption<T>;
31
- type ImportStatement<T = any> = Promise<T>;
32
- type ResolvedImportOption<T extends ImportOption> = ResolvedPromise<ExtractImportStatement<T>>;
33
- type ResolvedImportMap<$Imports extends ImportMap> = {
34
- [P in keyof $Imports]: ResolvedImportOption<$Imports[P]>;
35
- };
36
- type ResolvedImportMapPromise<T extends ImportMap> = Promise<ResolvedImportMap<T>>;
37
- type ResolvedImportList<$Imports extends ImportList> = {
38
- [P in keyof $Imports]: ResolvedImportOption<$Imports[P]>;
39
- };
40
- type ResolvedImportListPromise<T extends ImportList> = Promise<ResolvedImportList<T>>;
41
- type ExtractImportStatement<T extends ImportOption> = ExtractImport<T> extends () => infer I ? I : T;
42
- type ExtractImport<T extends ImportOption> = T extends {
43
- import: infer I;
44
- } ? I : T;
3
+ import * as async_cache_fn_index from 'async-cache-fn/index';
4
+ import { InstallPackageOptions as InstallPackageOptions$1 } from '@antfu/install-pkg';
5
+ import { ResolveOptions } from 'mlly';
45
6
 
46
7
  type __<T> = {
47
8
  [K in keyof T]: T[K];
48
9
  } & {};
10
+ type ValueOf<T> = T[keyof T];
11
+ type EnumToLiteral<T extends string | number> = T extends string ? `${T}` : `${T}` extends `${infer N extends number}` ? N : never;
12
+ type Func = (...args: any[]) => any;
13
+ type StringLiteral<T extends string> = T | (string & {});
49
14
  type Awaitable<T> = T | Promise<T>;
50
15
  type ResolvedPromise<T> = T extends Promise<infer U> ? U : never;
51
16
  type KeyOf<T, K> = K extends keyof T ? K : never;
52
17
  type KeyOfValue<T, K> = T[KeyOf<T, K>];
18
+ type PickKeyOf<T, K extends keyof T> = K extends keyof T ? K : never;
53
19
  type SelectionMap<T> = __<{
54
20
  [K in keyof T]?: boolean;
55
21
  }>;
@@ -70,15 +36,110 @@ type Select<T, TSelection extends SelectionMap<T>, TMode extends "pick" | "omit"
70
36
  type IsExactBoolean<T> = true extends T ? false extends T ? true : false : false;
71
37
  type IsUnion<T, U = T> = T extends U ? [U] extends [T] ? false : true : never;
72
38
  type MergeObject<T extends object, O extends object | unknown = unknown> = __<T & (O extends object ? O : Record<never, never>)>;
39
+ type OmitIndexSignature<ObjectType> = {
40
+ [KeyType in keyof ObjectType as {} extends Record<KeyType, unknown> ? never : KeyType]: ObjectType[KeyType];
41
+ };
42
+ type RequireExactlyOne<ObjectType, KeysType extends keyof ObjectType = keyof ObjectType> = {
43
+ [Key in KeysType]: Required<Pick<ObjectType, Key>> & Partial<Record<Exclude<KeysType, Key>, never>>;
44
+ }[KeysType] & Omit<ObjectType, KeysType>;
45
+ type OmitNever<T> = Omit<T, {
46
+ [K in keyof T]: T[K] extends never ? K : never;
47
+ }[keyof T]> & {};
48
+ type OmitByValue<T, V> = OmitNever<{
49
+ [K in keyof T]: T[K] extends V ? never : T[K];
50
+ }>;
51
+
52
+ interface PathOptions {
53
+ cwd?: string;
54
+ }
73
55
  type AsyncCacheFn<TReturn = unknown, TOption extends object | undefined = undefined, C extends "required" | "optional" = "optional"> = AsyncCacheFn$1<TReturn, C extends "optional" ? [TOption | undefined] | [] : [TOption]>;
74
56
 
57
+ interface PackageManagerConfig<ID extends string = string> {
58
+ id: ID;
59
+ command: string;
60
+ name: string;
61
+ meta: {
62
+ lockfile: string | string[];
63
+ };
64
+ runner: string;
65
+ args: {
66
+ install: {
67
+ command: string;
68
+ options: {
69
+ preferOffline: string;
70
+ dev: string;
71
+ };
72
+ };
73
+ uninstall: {
74
+ command: string;
75
+ };
76
+ };
77
+ options: {
78
+ version: string;
79
+ };
80
+ }
81
+ type InstallPackageOptions = PackageManagerScriptOptions<"install">;
82
+ type UninstallPackageOptions = PackageManagerScriptOptions<"uninstall">;
83
+ type PackageManagerCommands = PackageManagerConfig["args"];
84
+ type PackageManagerCommandName = keyof PackageManagerCommands;
85
+ type PackageManagerCommandSpec<K> = KeyOfValue<PackageManagerCommands, K> extends {
86
+ options: infer O;
87
+ } ? SelectionMap<O> : Record<never, never>;
88
+ type PackageManagerScriptOptions<K extends PackageManagerCommandName | undefined = undefined> = __<{
89
+ cwd?: string;
90
+ silent?: boolean;
91
+ shellOptions?: Options;
92
+ } & PackageManagerCommandSpec<K>>;
93
+
94
+ interface ImportModuleData<T = any> {
95
+ /**
96
+ * The name of the package to import. This should match the name used in the import statement.
97
+ */
98
+ name: string;
99
+ /**
100
+ * A callback which returns a module import statement
101
+ */
102
+ import: ImportCallback<T>;
103
+ }
104
+ interface ImportPackageData<T = any> extends ImportModuleData<T> {
105
+ /**
106
+ * When enabled, the package will be installed if it is not found
107
+ * @default true
108
+ */
109
+ install?: boolean;
110
+ /**
111
+ * When enabled, the package will be installed as a dev dependency
112
+ */
113
+ dev?: boolean;
114
+ /**
115
+ * Additional installation options
116
+ */
117
+ installOptions?: Omit<InstallPackageOptions$1, "dev">;
118
+ }
119
+ type ImportOption<T = any> = Module | ImportCallback<T> | ImportModuleData<T> | ImportPackageData<T>;
120
+ type Module<T = any> = Promise<T>;
121
+ type ImportCallback<T = any> = () => Module<T>;
122
+ type ImportMap = Record<string, ImportOption>;
123
+ type ImportList = ImportOption[];
124
+ type ImportOptionData<T extends ImportOption> = ResolvedPromise<ExtractImportOptionModule<T>>;
125
+ type ResolvedImportMap<$Imports extends ImportMap> = {
126
+ [P in keyof $Imports]: ImportOptionData<$Imports[P]>;
127
+ };
128
+ type ResolvedImportMapPromise<T extends ImportMap> = Promise<ResolvedImportMap<T>>;
129
+ type ResolvedImportList<$Imports extends ImportList> = {
130
+ [P in keyof $Imports]: ImportOptionData<$Imports[P]>;
131
+ };
132
+ type ExtractImportOptionModule<TOption> = Extract<TOption extends {
133
+ import: () => infer I;
134
+ } ? I : TOption extends () => infer I ? I : TOption extends Module ? TOption : never, Module>;
135
+
75
136
  interface ImporterOptions {
76
137
  /**
77
- * When enabled, the default behavior is to install packages that are not found.
78
- * The name of the package is inferred from the key of the import map.
138
+ * When enabled, the default behavior is to install packages that are not found if the name is provided.
79
139
  * @default true
80
140
  */
81
141
  install?: boolean;
142
+ installer?: InstallerFn;
82
143
  }
83
144
  /**
84
145
  * Dynamically imports modules and returns their exports in a tuple.
@@ -101,7 +162,16 @@ interface ImporterOptions {
101
162
  * @typeparam Imports An array type representing the dynamic imports.
102
163
  */
103
164
  declare function importer<T extends ImportList>(imports: [...T], options?: ImporterOptions): Promise<ResolvedImportList<T>>;
165
+ type DefinePackageFn = <T = any>(options: __<string | Pick<ImportPackageData, "name" | "dev">>) => ImportPackageData<T>;
166
+ declare const definePackage: DefinePackageFn;
167
+ type InstallerFn = (packageName: string | string[], options?: {
168
+ dev?: boolean;
169
+ checkExists?: boolean;
170
+ }) => Promise<void>;
104
171
 
172
+ interface ImportMapOptions extends ImporterOptions {
173
+ }
174
+ type ImportMapFn = <T extends ImportMap>(importMap: T, options?: ImportMapOptions) => ResolvedImportMapPromise<T>;
105
175
  /**
106
176
  * Asynchronously imports modules from a record of dynamic import promises.
107
177
  * Returns a promise that resolves to a record with the same keys, each mapped to the resolved import.
@@ -124,68 +194,21 @@ declare function importer<T extends ImportList>(imports: [...T], options?: Impor
124
194
  * ```
125
195
  *
126
196
  */
127
- declare function importMap<T extends ImportMap>(importMap: T, options?: ImporterOptions): Promise<ResolvedImportMap<T>>;
197
+ declare const importMap: ImportMapFn;
128
198
 
129
- declare function ensurePackage(name: string | string[], options?: InstallPackageOptions): Promise<void>;
130
-
131
- /**
132
- * Asynchronously handles the resolution of a module, extracting its default export if available.
133
- * If the module does not have a default export, it returns the module itself.
134
- *
135
- * @param m A promise representing a module import (either ES Module or CommonJS).
136
- * @returns A promise that resolves to either the default export of the module or the module itself.
137
- *
138
- * @typeparam T The type of the module being imported.
139
- *
140
- * @example
141
- * ```typescript
142
- * // Usage example with dynamic import
143
- * const module = await interopDefault(import('some-module'));
144
- *
145
- * // 'module' will be either the default export of 'some-module' or the module itself if no default export exists
146
- * ```
147
- */
148
- declare function resolveModule<T>(m: Awaitable<T>): Promise<T extends {
199
+ declare function resolveModule<T>(module: Awaitable<T>): Promise<T extends {
149
200
  default: infer U;
150
201
  } ? U : T>;
151
-
152
- declare function isPackageDependency(packageName: string | string[]): boolean;
153
-
154
- interface PackageManagerConfig<ID extends string = string> {
155
- id: ID;
156
- command: string;
157
- name: string;
158
- meta: {
159
- lockfile: string | string[];
160
- };
161
- runner: string;
162
- args: {
163
- install: {
164
- command: string;
165
- options: {
166
- preferOffline: string;
167
- isDevDependency: string;
168
- };
169
- };
170
- uninstall: {
171
- command: string;
172
- };
173
- };
174
- options: {
175
- version: string;
176
- };
202
+ interface ResolveModulePathOptions extends Omit<ResolveOptions, "url"> {
203
+ paths?: string | URL | (string | URL)[];
204
+ normalize?: ((path: string) => string) | boolean;
177
205
  }
178
- type PackageManagerCommands = PackageManagerConfig["args"];
179
- type PackageManagerCommandName = keyof PackageManagerCommands;
180
- type PackageManagerCommandSpec<K> = KeyOfValue<PackageManagerCommands, K> extends {
181
- options: infer O;
182
- } ? SelectionMap<O> : Record<never, never>;
206
+ declare function isPackageModuleFound(name: string, options?: ResolveModulePathOptions): boolean;
207
+ declare function resolvePackageModulePath(name: string, options?: ResolveModulePathOptions): string | undefined;
208
+ declare function findResolvedModulePath(paths: string[], options?: ResolveModulePathOptions): string | undefined;
209
+ declare function resolveModulePath(modulePath: string, options?: ResolveModulePathOptions): string | undefined;
183
210
 
184
- type ScriptOptions<K extends PackageManagerCommandName | undefined = undefined> = __<{
185
- cwd?: string;
186
- silent?: boolean;
187
- shellOptions?: Options;
188
- } & PackageManagerCommandSpec<K>>;
211
+ type PackageManagers = PackageManager<PackageManagerId>[];
189
212
  interface PackageManager<ID extends string = PackageManagerId> {
190
213
  id: ID;
191
214
  config: PackageManagerConfig;
@@ -198,30 +221,44 @@ interface PackageManager<ID extends string = PackageManagerId> {
198
221
  readLockfile: AsyncCacheFn<string | undefined, {
199
222
  cwd?: string;
200
223
  }>;
201
- globalVersion: AsyncCacheFn<string | undefined, ScriptOptions>;
202
- uninstallPackage: (packageNames: string | string[], options?: ScriptOptions<"uninstall">) => Promise<void>;
203
- installPackage: (packageNames: string | string[], options?: ScriptOptions<"install">) => Promise<void>;
224
+ globalVersion: AsyncCacheFn<string | undefined, PackageManagerScriptOptions>;
225
+ definePackage: DefinePackageFn;
226
+ defineImportMap: <T extends ImportMap>(importMap: T, options?: {
227
+ /**
228
+ * When enabled, the default behavior is to install packages that are not found.
229
+ * @default true
230
+ */
231
+ install?: boolean;
232
+ }) => ResolvedImportMapPromise<T>;
233
+ uninstallPackage: (packageNames: string | string[], options?: UninstallPackageOptions) => Promise<void>;
234
+ installPackage: (packageNames: string | string[], options?: PackageManagerScriptOptions<"install">) => Promise<void>;
204
235
  }
205
236
 
206
- type PackageManagerId = keyof typeof packageManagers;
207
- declare const packageManagers: {
208
- pnpm: PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">;
209
- yarn: PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">;
210
- bun: PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">;
211
- npm: PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">;
212
- };
213
-
214
237
  interface DetectPackageManagerOptions {
215
- allowed?: SelectionMap<typeof packageManagers>;
238
+ allowed?: SelectionMap<Record<PackageManagerId, unknown>>;
216
239
  cwd?: string;
217
240
  }
218
- declare function findPackageManager(options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">>;
219
- declare function findPackageManagerSafely<TAssert extends boolean = true>(options?: DetectPackageManagerOptions & {
241
+ declare function findPackageManager(packageManagers: PackageManagers, options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">>;
242
+ declare function findPackageManagerSafely<TAssert extends boolean = true>(packageManagers: PackageManagers, options?: DetectPackageManagerOptions & {
220
243
  assert?: TAssert;
221
- }): Promise<PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm"> | undefined>;
222
- declare function detectPackageManagers(options?: DetectPackageManagerOptions): Promise<(PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">)[]>;
223
- declare function detectLockfilePackageManagers(options?: DetectPackageManagerOptions): Promise<(PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">)[]>;
224
- declare function detectGlobalPackageManagers(options?: DetectPackageManagerOptions): Promise<(PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">)[]>;
225
- declare function filterPackageManagers(filterFn: (packageManager: PackageManager) => Promise<unknown>, options?: DetectPackageManagerOptions): Promise<(PackageManager<"pnpm"> | PackageManager<"yarn"> | PackageManager<"bun"> | PackageManager<"npm">)[]>;
244
+ }): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm"> | undefined>;
245
+ declare function detectPackageManagers(packageManagers: PackageManagers, options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[]>;
246
+ declare function detectLockfilePackageManagers(packageManagers: PackageManagers, options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[]>;
247
+ declare function detectGlobalPackageManagers(packageManagers: PackageManagers, options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[]>;
248
+ declare function filterPackageManagers(packageManagers: PackageManagers, filterFn: (packageManager: PackageManager) => Promise<boolean> | boolean, options?: DetectPackageManagerOptions): Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[]>;
249
+
250
+ type PackageManagerId = (typeof packageManagerConfigs)[number]["id"];
251
+ declare const packageManagerConfigs: (PackageManagerConfig<"pnpm"> | PackageManagerConfig<"yarn"> | PackageManagerConfig<"bun"> | PackageManagerConfig<"npm">)[];
252
+ declare function definePackageManagerClient(options: DetectPackageManagerOptions): {
253
+ configs: PackageManager<"pnpm" | "yarn" | "bun" | "npm">[];
254
+ findPackageManager: async_cache_fn_index.AsyncCacheFn<PackageManager<"pnpm" | "yarn" | "bun" | "npm">, [options?: DetectPackageManagerOptions | undefined]>;
255
+ findPackageManagerSafely: async_cache_fn_index.AsyncCacheFn<PackageManager<"pnpm" | "yarn" | "bun" | "npm"> | undefined, [options?: DetectPackageManagerOptions | undefined]>;
256
+ detectPackageManagers: async_cache_fn_index.AsyncCacheFn<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[], [options?: DetectPackageManagerOptions | undefined]>;
257
+ detectLockfilePackageManagers: async_cache_fn_index.AsyncCacheFn<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[], [options?: DetectPackageManagerOptions | undefined]>;
258
+ detectGlobalPackageManagers: async_cache_fn_index.AsyncCacheFn<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[], [options?: DetectPackageManagerOptions | undefined]>;
259
+ filterPackageManagers: (filterFn: (packageManager: PackageManager) => Promise<boolean> | boolean, options?: DetectPackageManagerOptions) => Promise<PackageManager<"pnpm" | "yarn" | "bun" | "npm">[]>;
260
+ };
261
+
262
+ declare function isPackageDependency(packageName: string | string[]): boolean;
226
263
 
227
- export { type AsyncCacheFn, type Awaitable, type Entry, type EntryOf, type FromEntries, type ImportList, type ImportMap, type ImportOption, type ImportPackageOption, type ImportStatement, type ImporterOptions, type IsExactBoolean, type IsUnion, type KeyOf, type KeyOfValue, type MergeObject, type PackageManagerCommandName, type PackageManagerCommandSpec, type PackageManagerCommands, type PackageManagerConfig, type PackageManagerId, type PickByValue, type ResolvedImportList, type ResolvedImportListPromise, type ResolvedImportMap, type ResolvedImportMapPromise, type ResolvedImportOption, type ResolvedPromise, type Select, type SelectionMap, type __, detectGlobalPackageManagers, detectLockfilePackageManagers, detectPackageManagers, ensurePackage, filterPackageManagers, findPackageManager, findPackageManagerSafely, importMap, importer, isPackageDependency, packageManagers, resolveModule };
264
+ export { type AsyncCacheFn, type Awaitable, type DefinePackageFn, type DetectPackageManagerOptions, type Entry, type EntryOf, type EnumToLiteral, type FromEntries, type Func, type ImportMapFn, type ImportMapOptions, type ImporterOptions, type InstallPackageOptions, type InstallerFn, type IsExactBoolean, type IsUnion, type KeyOf, type KeyOfValue, type MergeObject, type OmitByValue, type OmitIndexSignature, type OmitNever, type PackageManagerCommandName, type PackageManagerCommandSpec, type PackageManagerCommands, type PackageManagerConfig, type PackageManagerId, type PackageManagerScriptOptions, type PathOptions, type PickByValue, type PickKeyOf, type RequireExactlyOne, type ResolveModulePathOptions, type ResolvedPromise, type Select, type SelectionMap, type StringLiteral, type UninstallPackageOptions, type ValueOf, type __, definePackage, definePackageManagerClient, detectGlobalPackageManagers, detectLockfilePackageManagers, detectPackageManagers, filterPackageManagers, findPackageManager, findPackageManagerSafely, findResolvedModulePath, importMap, importer, isPackageDependency, isPackageModuleFound, packageManagerConfigs, resolveModule, resolveModulePath, resolvePackageModulePath };