Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 41 additions & 41 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
{
"name": "polar-cli",
"version": "1.3.0",
"description": "",
"bin": "bin/cli.js",
"type": "module",
"scripts": {
"build": "tsup ./src/cli.ts --format esm --outDir bin",
"dev": "tsc --watch",
"test": "echo \"Error: no test specified\" && exit 1",
"check": "biome check --write ./src",
"build:binary": "bun build ./src/cli.ts --compile --outfile polar",
"build:binary:darwin-arm64": "bun build ./src/cli.ts --compile --target=bun-darwin-arm64 --outfile polar",
"build:binary:darwin-x64": "bun build ./src/cli.ts --compile --target=bun-darwin-x64 --outfile polar",
"build:binary:linux-x64": "bun build ./src/cli.ts --compile --target=bun-linux-x64 --outfile polar"
},
"files": [
"bin",
"dist"
],
"keywords": [],
"author": "",
"license": "Apache-2.0",
"packageManager": "pnpm@10.5.2",
"dependencies": {
"@effect/cli": "^0.69.0",
"@effect/platform": "^0.90.2",
"@effect/platform-bun": "^0.87.1",
"@lemonsqueezy/lemonsqueezy.js": "^4.0.0",
"@polar-sh/sdk": "^0.43.1",
"effect": "^3.17.7",
"eventsource": "^4.1.0",
"open": "^10.2.0"
},
"devDependencies": {
"@biomejs/biome": "^2.1.4",
"@effect/language-service": "^0.35.2",
"@sindresorhus/tsconfig": "^8.0.1",
"@types/node": "^24.2.1",
"tsup": "^8.5.0",
"typescript": "^5.9.2"
}
"name": "polar-cli",
"version": "1.3.0",
"description": "",
"bin": "bin/cli.js",
"type": "module",
"scripts": {
"build": "tsup ./src/cli.ts --format esm --outDir bin",
"dev": "tsc --watch",
"test": "echo \"Error: no test specified\" && exit 1",
"check": "biome check --write ./src",
"build:binary": "bun build ./src/cli.ts --compile --outfile polar",
"build:binary:darwin-arm64": "bun build ./src/cli.ts --compile --target=bun-darwin-arm64 --outfile polar",
"build:binary:darwin-x64": "bun build ./src/cli.ts --compile --target=bun-darwin-x64 --outfile polar",
"build:binary:linux-x64": "bun build ./src/cli.ts --compile --target=bun-linux-x64 --outfile polar"
},
"files": [
"bin",
"dist"
],
"keywords": [],
"author": "",
"license": "Apache-2.0",
"packageManager": "pnpm@10.5.2",
"dependencies": {
"@effect/cli": "^0.69.0",
"@effect/platform": "^0.90.2",
"@effect/platform-bun": "^0.87.1",
"@lemonsqueezy/lemonsqueezy.js": "^4.0.0",
"@polar-sh/sdk": "^0.43.1",
"effect": "^3.17.7",
"eventsource": "^4.1.0",
"open": "^10.2.0"
},
"devDependencies": {
"@biomejs/biome": "^2.1.4",
"@effect/language-service": "^0.35.2",
"@sindresorhus/tsconfig": "^8.0.1",
"@types/node": "^24.2.1",
"tsup": "^8.5.0",
"typescript": "^5.9.2"
}
}
35 changes: 31 additions & 4 deletions src/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Command } from "@effect/cli";
import { Console, Effect, Schema } from "effect";
import { createHash } from "crypto";
import { chmod, mkdtemp, rm } from "fs/promises";
import { chmod, mkdtemp, rename, rm, unlink } from "fs/promises";
import { tmpdir } from "os";
import { join } from "path";
import { dirname, join } from "path";
import { VERSION } from "../version";

const REPO = "polarsource/cli";
Expand Down Expand Up @@ -177,13 +177,40 @@ const downloadAndUpdate = (

const binaryPath = process.execPath;
const newBinaryPath = join(tempDir, "polar");
const tempPath = join(dirname(binaryPath), `.polar-update-${Date.now()}`);

yield* Console.log(`${dim}Replacing binary...${reset}`);

yield* Effect.tryPromise({
try: async () => {
const newBinary = await Bun.file(newBinaryPath).arrayBuffer();
await Bun.write(binaryPath, newBinary);
await chmod(newBinaryPath, 0o755);

let needsSudo = false;
try {
const newBinary = await Bun.file(newBinaryPath).arrayBuffer();
await Bun.write(tempPath, newBinary);
await rename(tempPath, binaryPath);
} catch (e: any) {
unlink(tempPath).catch(() => {});
if (e?.code === "EACCES") {
needsSudo = true;
} else {
throw e;
}
}

if (needsSudo) {
const proc = Bun.spawn(["sudo", "mv", newBinaryPath, binaryPath], {
stdout: "inherit",
stderr: "inherit",
stdin: "inherit",
});
const exitCode = await proc.exited;
if (exitCode !== 0) {
throw new Error("sudo mv failed");
}
}

await chmod(binaryPath, 0o755);
},
catch: (e) =>
Expand Down