/
extract.ts
60 lines (51 loc) · 1.66 KB
/
extract.ts
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
import { mkdirP } from "@actions/io"
import { grantUserWriteAccess } from "admina"
import { warning } from "ci-log"
import { execa } from "execa"
import which from "which"
import { setupSevenZip } from "../../sevenzip/sevenzip"
export { extractTar, extractXar } from "@actions/tool-cache"
let sevenZip: string | undefined
/// Extract 7z using 7z
export async function extract7Zip(file: string, dest: string) {
await execa(await getSevenZip(), ["x", file, `-o${dest}`, "-y"], { stdio: "inherit" })
await grantUserWriteAccess(dest)
return dest
}
/// install 7z if needed
async function getSevenZip() {
if (sevenZip === undefined) {
if (which.sync("7z", { nothrow: true }) === null) {
await setupSevenZip("", "", process.arch)
}
// eslint-disable-next-line require-atomic-updates
sevenZip = "7z"
}
return sevenZip
}
/// Extract Exe using 7z
export function extractExe(file: string, dest: string) {
return extract7Zip(file, dest)
}
/// Extract Zip using 7z
export function extractZip(file: string, dest: string) {
return extract7Zip(file, dest)
}
export async function extractTarByExe(file: string, dest: string, flags = ["--strip-components=0"]) {
try {
await mkdirP(dest)
} catch {
// ignore
}
// TODO windows fails to create symlinks
// https://github.com/heroku/heroku-slugs/issues/3
try {
await execa("tar", ["xf", file, "-C", dest, ...flags], { stdio: "inherit" })
} catch (e) {
if (process.platform === "win32" && (e as Error).message.includes("Can't create '\\\\?\\C:")) {
warning(`Failed to extract symlink ${file} to ${dest}. Ignoring this symlink.`)
}
}
await grantUserWriteAccess(dest)
return dest
}