Lines 1-48javascript
1import { execFile, execFileSync, spawn } from 'child_process';
2import { writeFileSync, mkdirSync, existsSync, cpSync } from 'fs';
HighChild Process
Package source references child process execution.
connection/upgrade.jsView on unpkg · L1 3import { join, dirname } from 'path';
4import { fileURLToPath } from 'url';
5import { platform, homedir } from 'os';
6import ctx from '../context.js';
7import { getConfigDir, getServiceName, getPm2AppName, getLaunchdPlistPath, DEFAULT_INSTANCE_ID } from '../service.js';
8import { sendToServer } from './buffer.js';
9import { stopAgentHeartbeat } from './heartbeat.js';
12 * Resolve the local service instance id for this running agent. Each agent
13 * process is pinned to one instance (set in index.js from
14 * YEAFT_AGENT_INSTANCE / config), so the upgrade flow must target THAT
15 * instance's service unit — not the bare `yeaft-agent` default. Without this,
16 * a named instance (e.g. `server-e7a9eb`) installs the new package but its
17 * systemd unit `yeaft-agent@server-e7a9eb` is never restarted, leaving the
18 * agent permanently offline after a UI-triggered upgrade.
20export function resolveInstanceId() {
21 return ctx.CONFIG?.instanceId || process.env.YEAFT_AGENT_INSTANCE || DEFAULT_INSTANCE_ID;
24// Derive absolute paths for npm/pm2 from current node executable.
25// In launchd/systemd environments, PATH may not include nvm/node dirs,
26// but process.execPath always points to the running node binary.
27const nodeBinDir = dirname(process.execPath);
28const isWin = platform() === 'win32';
29// Windows: use bare 'npm'/'pm2' + shell:true — cmd.exe finds them via PATH.
30// This was the working pattern before ce58bbc. Absolute paths break when
31// the path contains spaces (e.g., "C:\Program Files\nodejs\npm.cmd").
32// macOS/Linux: use absolute path — launchd/systemd have minimal PATH.
33const npmPath = isWin ? 'npm' : join(nodeBinDir, 'npm');
34const pm2Path = isWin ? 'pm2' : join(nodeBinDir, 'pm2');
35const shellOpt = isWin ? { shell: true, windowsHide: true } : {};
37// Ensure PATH includes nodeBinDir so that `#!/usr/bin/env node` shebangs
38// can locate node in launchd/systemd environments with minimal PATH.
39const currentPath = process.env.PATH || '/usr/bin:/bin:/usr/sbin:/sbin';
40const safePath = currentPath.includes(nodeBinDir) ? currentPath : `${nodeBinDir}:${currentPath}`;
41const safeEnv = { ...process.env, PATH: safePath };
42export const PUBLIC_NPM_REGISTRY = 'https://registry.npmjs.org/';
43
MediumInstall Persistence
Source writes installer persistence such as shell profile or service configuration.
connection/upgrade.jsView on unpkg · L1 45 * Build an npm metadata query that bypasses stale local metadata and registry
46 * mirrors. Upgrade decisions must use the package actually published to the
47 * public registry, not a cached `latest` value from a previous release.