-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtarball.ts
More file actions
95 lines (90 loc) · 2.69 KB
/
tarball.ts
File metadata and controls
95 lines (90 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* @file Package tarball operations: extract a package to a directory and pack a
* spec into a tarball, both via pacote/libnpmpack with the shared packument
* cache.
*/
import { getPackumentCache, getPacoteCachePath } from '../constants/packages'
import cacache from '../external/cacache'
import libnpmpack from '../external/libnpmpack'
import pacote from '../external/pacote'
import { getAbortSignal } from '../process/abort'
import type { ExtractOptions, PacoteOptions } from './types'
const abortSignal = getAbortSignal()
const packumentCache = getPackumentCache()
const pacoteCachePath = getPacoteCachePath()
/**
* Extract a package to a destination directory.
*
* @example
* ;```typescript
* await extractPackage('lodash@4.17.21', { dest: '/tmp/lodash' })
* ```
*/
export async function extractPackage(
pkgNameOrId: string,
options?: ExtractOptions,
callback?: (destPath: string) => Promise<unknown>,
): Promise<void> {
let actualCallback = callback
let actualOptions = options
// biome-ignore lint/complexity/noArguments: Function overload support.
if (arguments.length === 2 && typeof options === 'function') {
actualCallback = options
actualOptions = undefined
}
const { dest, tmpPrefix, ...extractOptions_ } = {
__proto__: null,
...actualOptions,
} as ExtractOptions
const extractOptions = {
packumentCache,
preferOffline: true,
...extractOptions_,
}
/* c8 ignore start - External package registry extraction */
// pacote is imported at the top
if (typeof dest === 'string') {
await pacote.extract(pkgNameOrId, dest, extractOptions)
if (typeof actualCallback === 'function') {
await actualCallback(dest)
}
} else {
// The DefinitelyTyped types for cacache.tmp.withTmp are incorrect.
// It DOES returns a promise.
// cacache is imported at the top
await cacache.tmp.withTmp(
pacoteCachePath,
{ tmpPrefix },
async (tmpDirPath: string) => {
await pacote.extract(pkgNameOrId, tmpDirPath, extractOptions)
if (typeof actualCallback === 'function') {
await actualCallback(tmpDirPath)
}
},
)
}
/* c8 ignore stop */
}
/**
* Pack a package tarball using pacote.
*
* @example
* ;```typescript
* const tarball = await packPackage('lodash@4.17.21')
* ```
*/
export async function packPackage(
spec: string,
options?: PacoteOptions,
): Promise<unknown> {
/* c8 ignore start - External package registry packing */
// libnpmpack is imported at the top as libnpmpack
return await libnpmpack(spec, {
__proto__: null,
signal: abortSignal,
...options,
packumentCache,
preferOffline: true,
} as PacoteOptions)
/* c8 ignore stop */
}