@cocorograph/hub-agent 0.5.12 → 0.5.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/bin/hub-agent.mjs CHANGED
@@ -34,7 +34,11 @@ import {
34
34
  uninstallPlugin,
35
35
  } from "../src/plugin-install.mjs"
36
36
  import { loadPlugins } from "../src/plugin-loader.mjs"
37
- import { installService, uninstallService } from "../src/service-install.mjs"
37
+ import {
38
+ installService,
39
+ restartService,
40
+ uninstallService,
41
+ } from "../src/service-install.mjs"
38
42
 
39
43
  const here = path.dirname(fileURLToPath(import.meta.url))
40
44
  const pkg = JSON.parse(readFileSync(path.join(here, "..", "package.json"), "utf-8"))
@@ -164,6 +168,24 @@ program
164
168
  }
165
169
  })
166
170
 
171
+ program
172
+ .command("restart")
173
+ .description("install-service 済みの daemon を再起動 (macOS=launchctl kickstart / Linux=systemctl --user restart)")
174
+ .action(async () => {
175
+ try {
176
+ const r = await restartService()
177
+ console.log(`restarted: ${r.platform} ${r.path}`)
178
+ if (r.platform === "darwin") {
179
+ console.log(`label: ${r.label}`)
180
+ } else {
181
+ console.log(`unit: ${r.unit}`)
182
+ }
183
+ } catch (err) {
184
+ console.error(`restart failed: ${err.message}`)
185
+ process.exit(1)
186
+ }
187
+ })
188
+
167
189
  program
168
190
  .command("uninstall-service")
169
191
  .description("OS サービス登録を解除")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocorograph/hub-agent",
3
- "version": "0.5.12",
3
+ "version": "0.5.13",
4
4
  "description": "Hub Hosted Cockpit のローカル常駐 agent。Hub と outbound WSS で接続し、ローカルの tmux/pty を中継する。",
5
5
  "type": "module",
6
6
  "license": "UNLICENSED",
@@ -30,14 +30,54 @@ present() { command -v "$1" >/dev/null 2>&1; }
30
30
 
31
31
  ensure_brew() {
32
32
  if [[ "$(uname)" != "Darwin" ]]; then return 0; fi
33
- if present brew; then color_ok "brew already installed"; return 0; fi
34
- color_step "Homebrew をインストール"
35
- /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
36
- # Apple Silicon の brew はデフォルト PATH に入らないので追加
37
- if [[ -d /opt/homebrew/bin ]]; then
38
- export PATH="/opt/homebrew/bin:$PATH"
33
+ if present brew; then
34
+ color_ok "brew already installed"
35
+ else
36
+ color_step "Homebrew をインストール"
37
+ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
38
+ # Apple Silicon の brew はデフォルト PATH に入らないので追加
39
+ if [[ -d /opt/homebrew/bin ]]; then
40
+ export PATH="/opt/homebrew/bin:$PATH"
41
+ fi
42
+ present brew || { color_err "brew install failed"; exit 1; }
39
43
  fi
40
- present brew || { color_err "brew install failed"; exit 1; }
44
+ # Apple Silicon Mac `/opt/homebrew/bin` がデフォルト PATH に入らないので、
45
+ # `~/.zprofile` / `~/.bash_profile` に `brew shellenv` の eval を **永続化** する。
46
+ # これを入れないと install.sh 終了後の新規ターミナルから `npm` / `node` /
47
+ # `hub-agent` が `command not found` になる (葛西氏 Mac で 0.5.11 リリース直後に踏んだ)。
48
+ persist_brew_shellenv
49
+ }
50
+
51
+ # Apple Silicon Mac で brew のパスを zsh / bash 永続化する。既に追記済みなら no-op。
52
+ # `eval "$(/opt/homebrew/bin/brew shellenv)"` を書くことで、Homebrew が用意する
53
+ # HOMEBREW_PREFIX / PATH / MANPATH / INFOPATH すべてを新規シェルで自動セットする。
54
+ persist_brew_shellenv() {
55
+ local brew_bin=""
56
+ if [[ -x /opt/homebrew/bin/brew ]]; then
57
+ brew_bin="/opt/homebrew/bin/brew"
58
+ elif [[ -x /usr/local/bin/brew ]]; then
59
+ # Intel Mac は `/usr/local/bin` がデフォルト PATH に入るので追記不要
60
+ return 0
61
+ else
62
+ return 0
63
+ fi
64
+ local snippet="eval \"\$(${brew_bin} shellenv)\""
65
+ local marker="# >>> hub-agent: brew shellenv (Apple Silicon PATH) >>>"
66
+ local end_marker="# <<< hub-agent: brew shellenv <<<"
67
+ local target
68
+ for target in "$HOME/.zprofile" "$HOME/.bash_profile"; do
69
+ if [[ -f "$target" ]] && grep -Fq "$snippet" "$target"; then
70
+ color_ok "brew shellenv は既に $target にあります"
71
+ continue
72
+ fi
73
+ color_step "$target に brew shellenv を追記"
74
+ {
75
+ printf '\n%s\n' "$marker"
76
+ printf '%s\n' "$snippet"
77
+ printf '%s\n' "$end_marker"
78
+ } >> "$target"
79
+ color_ok "$target に追記しました (新規シェルから有効)"
80
+ done
41
81
  }
42
82
 
43
83
  ensure_pkg() {
@@ -105,6 +105,47 @@ export async function installService({ bin } = {}) {
105
105
  throw new Error(`unsupported platform: ${process.platform}`)
106
106
  }
107
107
 
108
+ /**
109
+ * install-service 済みの hub-agent を再起動する。
110
+ *
111
+ * - macOS: `launchctl kickstart -k gui/<uid>/<label>` で SIGTERM + 再起動
112
+ * - Linux: `systemctl --user restart hub-agent.service`
113
+ *
114
+ * service が install されていない場合は分かりやすい error を投げる。
115
+ * (cockpit web UI から `hub-agent restart` 案内が出ても、enroll 直後の
116
+ * ユーザーが叩くケースを想定)
117
+ */
118
+ export async function restartService() {
119
+ if (process.platform === "darwin") {
120
+ const dest = macPlistPath()
121
+ try {
122
+ await fs.access(dest)
123
+ } catch {
124
+ throw new Error(
125
+ `service not installed (${dest} not found). run \`hub-agent install-service\` first`,
126
+ )
127
+ }
128
+ const uid = ensureUnixUid()
129
+ run("launchctl", ["kickstart", "-k", `gui/${uid}/${PLIST_LABEL}`])
130
+ return { platform: "darwin", path: dest, label: PLIST_LABEL }
131
+ }
132
+
133
+ if (process.platform === "linux") {
134
+ const dest = linuxUnitPath()
135
+ try {
136
+ await fs.access(dest)
137
+ } catch {
138
+ throw new Error(
139
+ `service not installed (${dest} not found). run \`hub-agent install-service\` first`,
140
+ )
141
+ }
142
+ run("systemctl", ["--user", "restart", SYSTEMD_UNIT_NAME])
143
+ return { platform: "linux", path: dest, unit: SYSTEMD_UNIT_NAME }
144
+ }
145
+
146
+ throw new Error(`unsupported platform: ${process.platform}`)
147
+ }
148
+
108
149
  export async function uninstallService() {
109
150
  if (process.platform === "darwin") {
110
151
  const uid = ensureUnixUid()