-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.ts
More file actions
55 lines (49 loc) · 1.58 KB
/
extract.ts
File metadata and controls
55 lines (49 loc) · 1.58 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
/**
* @fileoverview `extractArchive` — format-detecting dispatcher that
* routes to `extractTar` / `extractTarGz` / `extractZip`.
*/
import { ErrorCtor } from '../primordials/error'
import { detectArchiveFormat } from './detect'
import { getPath } from './_internal'
import { extractTar, extractTarGz } from './tar'
import { extractZip } from './zip'
import type { ExtractOptions } from './types'
/**
* Extract an archive to a directory.
* Automatically detects format from file extension.
*
* @param archivePath - Path to archive file
* @param outputDir - Directory to extract to
* @param options - Extraction options
* @throws Error if archive format is not supported
*
* @example
* ```typescript
* await extractArchive('/tmp/package.tar.gz', '/tmp/output')
* await extractArchive('/tmp/release.zip', '/tmp/output', { strip: 1 })
* ```
*/
export async function extractArchive(
archivePath: string,
outputDir: string,
options: ExtractOptions = {},
): Promise<void> {
const format = detectArchiveFormat(archivePath)
if (!format) {
const path = getPath()
const ext = path.extname(archivePath).toLowerCase()
throw new ErrorCtor(
`Unsupported archive format${ext ? ` (extension: ${ext})` : ''}: ${archivePath}. ` +
'Supported formats: .zip, .tar, .tar.gz, .tgz',
)
}
switch (format) {
case 'zip':
return await extractZip(archivePath, outputDir, options)
case 'tar':
return await extractTar(archivePath, outputDir, options)
case 'tar.gz':
case 'tgz':
return await extractTarGz(archivePath, outputDir, options)
}
}