Files
kandev/apps/cli/src/service/metadata.ts
T
José Almeida 041a19f060 feat: service-managed UI self-update (#1210)
* feat: add service-managed self-update

* chore: add temporary self-update test scripts

* chore: simplify temporary script markdown fences

* chore: run frontend in temporary self-update scripts

* fix: show self-update job progress after apply

* chore: add real npm self-update test script

* fix: isolate npm self-update prefix

* fix: detect npm service installs from bin shim

* chore: reset service before real npm update test

* chore: expand real npm status diagnostics

* fix: preserve service home during self-update

* fix(cli): tee self-update helper output to a log file

* fix(cli): avoid launchd bootstrap race when reloading the macOS service

* fix(backend): run macOS self-update helper once via transient launchd plist

* fix(web): show self-update progress and lock the apply button until the update lands

* fix(cli): reinstall via the kandev wrapper on Homebrew self-update

* chore: remove temp self-update test scripts

* fix(web): drop unused apply path from useUpdates hook

* fix(backend): snapshot install state once in apply preflight

* docs: align self-update docs with transient LaunchAgent flow

* chore(release): run cli bundle from apps workspace root

* fix(backend): guard against concurrent self-update applies

* fix(backend): check scheme and helper paths in self-update apply

* fix(backend): drop duplicate binary name from npx manual command

* fix(cli): allow --help for service self-update without --intent

* fix(cli): harden self-update dir perms and darwin uid resolution

* fix(web): set required POST fields last in applyUpdate

* docs: use JSON body notation for the apply endpoint

* fix(backend): expire self-update guard so a stuck helper cannot wedge apply

* refactor(cli): collapse redundant writeSync branch in self-update log
2026-06-01 17:49:15 +01:00

77 lines
2.3 KiB
TypeScript

import fs from "node:fs";
import path from "node:path";
import type { LauncherInfo } from "./paths";
export type ServiceManager = "systemd" | "launchd";
export type ServiceMode = "user" | "system";
export type ServiceInstallMetadata = {
version: 1;
manager: ServiceManager;
mode: ServiceMode;
kind: LauncherInfo["kind"];
home_dir: string;
log_dir: string;
service_path: string;
node_path: string;
cli_entry: string;
bundle_dir?: string;
launcher_version?: string;
port?: number;
system_user?: string;
installed_at: string;
};
export type BuildServiceInstallMetadataInput = {
manager: ServiceManager;
mode: ServiceMode;
launcher: LauncherInfo;
homeDir: string;
logDir: string;
servicePath: string;
port?: number;
systemUser?: string;
now?: Date;
};
export function serviceMetadataPath(homeDir: string): string {
return path.join(homeDir, "service", "install.json");
}
export function buildServiceInstallMetadata(
input: BuildServiceInstallMetadataInput,
): ServiceInstallMetadata {
const out: ServiceInstallMetadata = {
version: 1,
manager: input.manager,
mode: input.mode,
kind: input.launcher.kind,
home_dir: input.homeDir,
log_dir: input.logDir,
service_path: input.servicePath,
node_path: input.launcher.nodePath,
cli_entry: input.launcher.cliEntry,
installed_at: (input.now ?? new Date()).toISOString(),
};
if (input.launcher.bundleDir) out.bundle_dir = input.launcher.bundleDir;
if (input.launcher.version) out.launcher_version = input.launcher.version;
if (input.port !== undefined) out.port = input.port;
if (input.systemUser) out.system_user = input.systemUser;
return out;
}
export function writeServiceInstallMetadata(
metadataPath: string,
metadata: ServiceInstallMetadata,
): void {
const dir = path.dirname(metadataPath);
// `mode` on mkdir/writeFile only applies when the path is created. chmod after
// so a pre-existing service dir / install.json is tightened to owner-only too
// (it can hold launcher paths and the metadata that gates self-update).
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
fs.chmodSync(dir, 0o700);
fs.writeFileSync(metadataPath, `${JSON.stringify(metadata, null, 2)}\n`, { mode: 0o600 });
fs.chmodSync(metadataPath, 0o600);
}