mirror of
https://github.com/Tencent/BrowserSkill.git
synced 2026-09-10 13:01:23 +08:00
feat: add bsk CLI auto update
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -8,10 +8,13 @@
|
||||
* --repo Tencent/BrowserSkill \
|
||||
* --server-url https://github.com \
|
||||
* --branch main \
|
||||
* --dist dist \
|
||||
* --out version.json
|
||||
*/
|
||||
|
||||
import { writeFileSync } from "node:fs";
|
||||
import { createHash } from "node:crypto";
|
||||
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { parseArgs } from "node:util";
|
||||
|
||||
/** Platform key -> Rust target triple */
|
||||
@@ -29,6 +32,7 @@ const { values } = parseArgs({
|
||||
repo: { type: "string" },
|
||||
"server-url": { type: "string", default: "https://github.com" },
|
||||
branch: { type: "string", default: "main" },
|
||||
dist: { type: "string" },
|
||||
out: { type: "string" },
|
||||
},
|
||||
});
|
||||
@@ -37,11 +41,12 @@ const version = values.version?.replace(/^v/, "");
|
||||
const repo = values.repo;
|
||||
const serverUrl = values["server-url"]?.replace(/\/$/, "") ?? "https://github.com";
|
||||
const branch = values.branch ?? "main";
|
||||
const dist = values.dist;
|
||||
const out = values.out;
|
||||
|
||||
if (!version || !repo || !out) {
|
||||
console.error(
|
||||
"Usage: --version <semver> --repo <owner/repo> --out <path> [--server-url URL] [--branch main]",
|
||||
"Usage: --version <semver> --repo <owner/repo> --out <path> [--server-url URL] [--branch main] [--dist dist]",
|
||||
);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -56,10 +61,33 @@ function assetFilename(platformKey, triple) {
|
||||
return `bsk-v${version}-${triple}.tar.gz`;
|
||||
}
|
||||
|
||||
function sha256File(path) {
|
||||
return createHash("sha256").update(readFileSync(path)).digest("hex");
|
||||
}
|
||||
|
||||
function assetEntry(platformKey, triple) {
|
||||
const filename = assetFilename(platformKey, triple);
|
||||
const url = `${releaseBase}/${filename}`;
|
||||
if (!dist) {
|
||||
return url;
|
||||
}
|
||||
|
||||
const path = join(dist, filename);
|
||||
if (!existsSync(path)) {
|
||||
console.error(`missing release asset for checksum: ${path}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
sha256: sha256File(path),
|
||||
};
|
||||
}
|
||||
|
||||
const assets = Object.fromEntries(
|
||||
Object.entries(PLATFORMS).map(([key, triple]) => [
|
||||
key,
|
||||
`${releaseBase}/${assetFilename(key, triple)}`,
|
||||
assetEntry(key, triple),
|
||||
]),
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import test from "node:test";
|
||||
|
||||
const scriptDir = dirname(fileURLToPath(import.meta.url));
|
||||
const scriptPath = join(scriptDir, "render-version-json.mjs");
|
||||
|
||||
test("renders asset URLs with sha256 checksums from dist files", () => {
|
||||
const tmp = mkdtempSync(join(tmpdir(), "bsk-version-json-"));
|
||||
const dist = join(tmp, "dist");
|
||||
const out = join(tmp, "version.json");
|
||||
const version = "9.8.7";
|
||||
const archive = `bsk-v${version}-aarch64-apple-darwin.tar.gz`;
|
||||
const archiveBytes = Buffer.from("fake darwin arm64 archive\n");
|
||||
mkdirSync(dist);
|
||||
writeFileSync(join(dist, archive), archiveBytes);
|
||||
writeFileSync(join(dist, `bsk-v${version}-x86_64-apple-darwin.tar.gz`), "darwin x64\n");
|
||||
writeFileSync(
|
||||
join(dist, `bsk-v${version}-x86_64-unknown-linux-musl.tar.gz`),
|
||||
"linux x64\n",
|
||||
);
|
||||
writeFileSync(
|
||||
join(dist, `bsk-v${version}-aarch64-unknown-linux-musl.tar.gz`),
|
||||
"linux arm64\n",
|
||||
);
|
||||
writeFileSync(join(dist, `bsk-v${version}-x86_64-pc-windows-msvc.zip`), "windows x64\n");
|
||||
|
||||
execFileSync(process.execPath, [
|
||||
scriptPath,
|
||||
"--version",
|
||||
version,
|
||||
"--repo",
|
||||
"Tencent/BrowserSkill",
|
||||
"--dist",
|
||||
dist,
|
||||
"--out",
|
||||
out,
|
||||
]);
|
||||
|
||||
const manifest = JSON.parse(readFileSync(out, "utf8"));
|
||||
const expectedSha = createHash("sha256").update(archiveBytes).digest("hex");
|
||||
assert.equal(
|
||||
manifest.assets["darwin-arm64"].url,
|
||||
`https://github.com/Tencent/BrowserSkill/releases/download/cli-v${version}/${archive}`,
|
||||
);
|
||||
assert.equal(manifest.assets["darwin-arm64"].sha256, expectedSha);
|
||||
});
|
||||
Reference in New Issue
Block a user