refactor: add extract module to the utils

Update extract.ts
This commit is contained in:
Amin Yahyaabadi 2021-09-15 02:08:30 -05:00
parent 921e77430b
commit 8c32267eea
2 changed files with 26 additions and 19 deletions

View File

@ -1,10 +1,9 @@
import * as core from "@actions/core" import * as core from "@actions/core"
import { exec } from "@actions/exec"
import { mkdirP } from "@actions/io"
import * as path from "path" import * as path from "path"
import semverLte from "semver/functions/lte" import semverLte from "semver/functions/lte"
import { isValidUrl } from "../utils/http/validate_url" import { isValidUrl } from "../utils/http/validate_url"
import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin" import { InstallationInfo, PackageInfo, setupBin } from "../utils/setup/setupBin"
import { extractExe, extractTarByExe } from "../utils/setup/extract"
//================================================ //================================================
// Version // Version
@ -270,23 +269,7 @@ async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): P
url, url,
extractedFolderName: "", extractedFolderName: "",
binRelativeDir: "bin", binRelativeDir: "bin",
extractFunction: extractFunction: platform === "win32" ? extractExe : extractTarByExe,
platform === "win32"
? async (file: string, dest: string) => {
const exit = await exec("7z", ["x", file, `-o${dest}`])
if (exit !== 0) {
throw new Error(`Failed to extract ${file} to ${dest} with 7z`)
}
return dest
}
: async (file: string, dest: string) => {
await mkdirP(dest)
const exit = await exec("tar", ["xf", file, "-C", dest, "--strip-components=1"])
if (exit !== 0) {
throw new Error(`Failed to extract ${file} to ${dest} with tar`)
}
return dest
},
} }
} }

View File

@ -0,0 +1,24 @@
import { exec } from "@actions/exec"
import { mkdirP } from "@actions/io"
export { extractTar, extractXar, extract7z, extractZip } from "@actions/tool-cache"
export async function extractExe(file: string, dest: string) {
const exit = await exec("7z", ["x", file, `-o${dest}`])
if (exit !== 0) {
throw new Error(`Failed to extract ${file} to ${dest} with 7z`)
}
return dest
}
export async function extractTarByExe(file: string, dest: string, flags = ["--strip-components=1"]) {
try {
await mkdirP(dest)
} catch {
// ignore
}
const exit = await exec("tar", ["xf", file, "-C", dest, ...flags])
if (exit !== 0) {
throw new Error(`Failed to extract ${file} to ${dest} with tar`)
}
return dest
}