Skip to content

Commit bd69dfd

Browse files
committed
fix ci
1 parent 8deb846 commit bd69dfd

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

src/repo.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getAssetNames } from "./tool"
1+
import { getAssetNames, getFetchOption } from "./tool"
22
import { Release } from "./type"
33
export class Repo {
44
name: string
@@ -22,11 +22,7 @@ export class Repo {
2222

2323
async getRelease(tag = "latest"): Promise<Release> {
2424
const url = this.getReleasesApiUrl(tag)
25-
const response = await fetch(url, {
26-
headers: {
27-
"User-Agent": "nodejs"
28-
}
29-
})
25+
const response = await fetch(url, getFetchOption())
3026
if (!response.ok) {
3127
throw new Error(`Failed to fetch ${url}`)
3228
}

src/setup.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ import path from "path"
22
import { Repo } from "./repo"
33
import { extractTo } from "./tool"
44
import type { Input, Output } from "./type"
5-
import { homedir, } from "os"
6-
import tc from "@actions/tool-cache";
5+
import { homedir } from "os"
6+
import tc from "@actions/tool-cache"
77

88
export async function setup(input: Input): Promise<Output> {
99
const { repo, version = "latest" } = input
1010
const url = await new Repo(repo).getAssetUrl(version)
1111
const installDir = path.join(homedir(), "easy-setup")
12-
const downloadPath = await tc.downloadTool(url);
12+
const downloadPath = await tc.downloadTool(url)
1313
await extractTo(downloadPath, installDir)
1414
return {
1515
version,

src/tool.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,46 @@
11
import * as fs from "fs"
22
import { execSync } from "child_process"
3-
import tc from '@actions/tool-cache'
43

5-
export async function extractTo(compressedFilePath: string, outputDir: string) {
4+
export function getFetchOption() {
5+
return {
6+
headers: { "User-Agent": "nodejs" },
7+
}
8+
}
9+
10+
export async function download(url: string, outputPath: string) {
11+
const buf = await fetch(url, getFetchOption()).then((res) =>
12+
res.arrayBuffer(),
13+
)
14+
fs.writeFileSync(outputPath, Buffer.from(buf))
15+
}
16+
17+
export function extractTo(compressedFilePath: string, outputDir: string) {
618
if (!fs.existsSync(outputDir)) {
719
fs.mkdirSync(outputDir, { recursive: true })
820
}
921
const rules = [
1022
{
1123
ext: [".zip"],
12-
cmd: () => tc.extractZip(compressedFilePath, outputDir),
24+
cmd: `unzip -o "${compressedFilePath}" -d "${outputDir}"`,
25+
},
26+
{ ext: [".tar"], cmd: `tar -xf "${compressedFilePath}" -C "${outputDir}"` },
27+
{
28+
ext: [".tar.gz", ".tgz"],
29+
cmd: `tar -xzf "${compressedFilePath}" -C "${outputDir}"`,
1330
},
14-
{ ext: [".tar", ".tar.gz", ".tgz"], cmd: () => tc.extractTar(compressedFilePath, outputDir) },
1531
{
1632
ext: [".tar.bz2"],
17-
cmd: () => tc.extractXar(compressedFilePath, outputDir),
33+
cmd: `tar -xjf "${compressedFilePath}" -C "${outputDir}"`,
1834
},
19-
{ ext: [".7z"], cmd: () => tc.extract7z(compressedFilePath, outputDir) },
35+
{ ext: [".7z"], cmd: `7z x "${compressedFilePath}" -o"${outputDir}"` },
2036
{ ext: [".rar"], cmd: `unrar x "${compressedFilePath}" "${outputDir}"` },
2137
{ ext: [".rar"], cmd: `unrar x "${compressedFilePath}" "${outputDir}"` },
2238
] as const
2339

2440
for (const { ext, cmd } of rules) {
2541
for (const e of ext) {
2642
if (compressedFilePath.endsWith(e)) {
27-
if (typeof cmd === "function") {
28-
return await cmd()
29-
}
30-
if (typeof cmd === "string") {
31-
return execSync(cmd)
32-
}
43+
return execSync(cmd)
3344
}
3445
}
3546
}

test/tool.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { expect, test } from "vitest"
2-
import { extractTo, getAssetNames } from "../src/tool"
2+
import { download, extractTo, getAssetNames } from "../src/tool"
33
import * as path from "path"
44
import * as fs from "fs"
55
import { homedir, tmpdir } from "os"
6-
import tc from '@actions/tool-cache'
6+
77
test("getAssetNames", () => {
88
expect(getAssetNames("deno", "win32", "x64")).toEqual([
99
"deno-x86_64-pc-windows-msvc",
@@ -23,9 +23,10 @@ test("getAssetNames", () => {
2323
test("extractTo zip", async () => {
2424
const url =
2525
"https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-x86_64-pc-windows-msvc.zip"
26+
const filePath = path.join(tmpdir(), "ansi2-x86_64-pc-windows-msvc.zip")
2627
const testDir = "easy-setup-test"
2728
const installDir = path.join(homedir(), testDir)
28-
const filePath = await tc.downloadTool(url)
29+
await download(url, filePath)
2930
extractTo(filePath, installDir)
3031
const ansi2Path = path.join(homedir(), testDir, "ansi2.exe")
3132
expect(fs.existsSync(ansi2Path)).toEqual(true)
@@ -36,9 +37,10 @@ test("extractTo tar.gz", async () => {
3637
if (process.platform === "win32") return
3738
const url =
3839
"https://github.com/ahaoboy/ansi2/releases/download/v0.2.11/ansi2-aarch64-apple-darwin.tar.gz"
40+
const filePath = path.join(tmpdir(), "ansi2-aarch64-apple-darwin.tar.gz")
3941
const testDir = "easy-setup-test"
4042
const installDir = path.join(homedir(), testDir)
41-
const filePath = await tc.downloadTool(url)
43+
await download(url, filePath)
4244
extractTo(filePath, installDir)
4345
const ansi2Path = path.join(homedir(), testDir, "ansi2")
4446
expect(fs.existsSync(ansi2Path)).toEqual(true)

0 commit comments

Comments
 (0)