centralu 0.1.0-beta.7 → 0.1.0-beta.71
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/centralu.mjs +434 -61
- package/bin/node-floor.mjs +42 -0
- package/bin/platform.mjs +380 -0
- package/bin/semver.mjs +34 -1
- package/bin/serve-args.mjs +181 -0
- package/bin/serve-autostart.mjs +490 -0
- package/bin/serve-detach.mjs +377 -0
- package/bin/serve-files.mjs +773 -0
- package/bin/serve-handover.mjs +192 -0
- package/bin/serve-host.mjs +467 -0
- package/bin/serve-install.mjs +218 -0
- package/bin/serve-keys.mjs +126 -0
- package/bin/serve-port.mjs +79 -0
- package/bin/serve-run.mjs +290 -0
- package/bin/serve-switch.mjs +635 -0
- package/bin/serve-util.mjs +144 -0
- package/bin/serve-window.mjs +147 -0
- package/bin/serve-windows.mjs +181 -0
- package/bin/serve.mjs +39 -0
- package/bin/swap.mjs +48 -0
- package/package.json +10 -6
package/bin/centralu.mjs
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* attaches it, npm does not. So an npm install just opens with no warning even with an ad hoc
|
|
9
9
|
* signature (docs/plans/beta-release-checklist.md §2).
|
|
10
10
|
*
|
|
11
|
-
* The app itself lives inside an architecture-specific package (
|
|
11
|
+
* The app itself lives inside an architecture-specific package (`@centralu/darwin-arm64`). This
|
|
12
12
|
* package is a thin shell that finds it and launches it — the same structure esbuild and swc
|
|
13
13
|
* use.
|
|
14
14
|
*
|
|
@@ -16,10 +16,54 @@
|
|
|
16
16
|
* "the app" even is — macOS hands a `.app` directory to LaunchServices, Linux runs a
|
|
17
17
|
* self-contained AppImage itself — so that difference lives in one table (`TARGETS`)
|
|
18
18
|
* instead of being rediscovered in every function.
|
|
19
|
+
*
|
|
20
|
+
* Windows joined in W3 of #14. Its decisions — the exe inside the folder, a detached start,
|
|
21
|
+
* where `install` copies to, the WebView2 check — live in `platform.mjs` as functions of the
|
|
22
|
+
* platform, so they are tested on macOS CI (tooling/launcher-platform.test.ts) instead of
|
|
23
|
+
* being first exercised on someone's laptop.
|
|
19
24
|
*/
|
|
20
25
|
import { execFileSync, spawn } from 'node:child_process'
|
|
21
|
-
import { copyDiffers, isNewer } from './semver.mjs'
|
|
22
|
-
import {
|
|
26
|
+
import { copyDiffers, isNewer, keepsInstalledCopy } from './semver.mjs'
|
|
27
|
+
import {
|
|
28
|
+
asideCopies,
|
|
29
|
+
bundleVersionCommand,
|
|
30
|
+
busyMessage,
|
|
31
|
+
earlyExitMessage,
|
|
32
|
+
executableIn,
|
|
33
|
+
findHostEntry,
|
|
34
|
+
installedPaths,
|
|
35
|
+
isBusyError,
|
|
36
|
+
launchPlan,
|
|
37
|
+
npmCommand,
|
|
38
|
+
regExe,
|
|
39
|
+
shortcutCommand,
|
|
40
|
+
targetFor,
|
|
41
|
+
TARGETS,
|
|
42
|
+
webview2MissingMessage,
|
|
43
|
+
webview2Status,
|
|
44
|
+
windowsInstall,
|
|
45
|
+
} from './platform.mjs'
|
|
46
|
+
import {
|
|
47
|
+
decodeChildSpec,
|
|
48
|
+
gateHostStart,
|
|
49
|
+
parseServeArgs,
|
|
50
|
+
printConnection,
|
|
51
|
+
rotateToken,
|
|
52
|
+
revokeCredential,
|
|
53
|
+
runAutostart,
|
|
54
|
+
runDetach,
|
|
55
|
+
runDetachedChild,
|
|
56
|
+
runServe,
|
|
57
|
+
runStop,
|
|
58
|
+
runSwitch,
|
|
59
|
+
runSwitchRun,
|
|
60
|
+
runSwitchStatus,
|
|
61
|
+
runWindow,
|
|
62
|
+
SERVE_HELP,
|
|
63
|
+
} from './serve.mjs'
|
|
64
|
+
import { swapInto } from './swap.mjs'
|
|
65
|
+
import { belowFloor, floorOf, oldNodeSentence } from './node-floor.mjs'
|
|
66
|
+
import { chmodSync, cpSync, existsSync, mkdirSync, readdirSync, readFileSync, realpathSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
|
23
67
|
import { createRequire } from 'node:module'
|
|
24
68
|
import { homedir } from 'node:os'
|
|
25
69
|
import { dirname, join } from 'node:path'
|
|
@@ -28,31 +72,27 @@ const require = createRequire(import.meta.url)
|
|
|
28
72
|
const pkg = require('../package.json')
|
|
29
73
|
|
|
30
74
|
const APP_NAME = 'Centralu'
|
|
75
|
+
/** The oldest Node the host runs on: this package's own `engines.node` (node-floor.mjs) */
|
|
76
|
+
const NODE_FLOOR = floorOf(pkg.engines.node)
|
|
31
77
|
const BUNDLE = `${APP_NAME}.app`
|
|
32
|
-
|
|
78
|
+
/*
|
|
79
|
+
* `CENTRALU_INSTALLED_COPY` points `install` and the copy checks at another bundle path. It exists
|
|
80
|
+
* so the launcher's own tests can run `centralu install` against a temporary folder and never
|
|
81
|
+
* against the real /Applications (tooling/launcher-install.test.ts).
|
|
82
|
+
*/
|
|
83
|
+
const INSTALLED = process.env.CENTRALU_INSTALLED_COPY || `/Applications/${BUNDLE}`
|
|
84
|
+
|
|
85
|
+
const PLATFORM = process.platform
|
|
86
|
+
const HOME = homedir()
|
|
33
87
|
|
|
34
88
|
/** Where a menu entry for the app goes on freedesktop desktops */
|
|
35
|
-
const DESKTOP_ENTRY = join(
|
|
89
|
+
const DESKTOP_ENTRY = join(HOME, '.local/share/applications/centralu.desktop')
|
|
36
90
|
|
|
37
|
-
/**
|
|
38
|
-
|
|
39
|
-
*
|
|
40
|
-
* `artifact` must match `scripts/release-npm.mts` exactly — that script renames what
|
|
41
|
-
* Tauri produced (`Centralu_0.1.0-beta.2_amd64.AppImage`) to a fixed name precisely so
|
|
42
|
-
* this file does not have to know version or architecture. If the two ever disagree the
|
|
43
|
-
* symptom is "installed fine, does nothing", so keep them in one place mentally.
|
|
44
|
-
*
|
|
45
|
-
* Only combinations that are actually published belong here. Listing a package that was
|
|
46
|
-
* never released would turn "not supported yet" into "your install is broken", which
|
|
47
|
-
* sends the user off to reinstall something that can never appear.
|
|
48
|
-
*/
|
|
49
|
-
const TARGETS = {
|
|
50
|
-
'darwin-arm64': { pkg: 'centralu-darwin-arm64', artifact: BUNDLE },
|
|
51
|
-
'linux-arm64': { pkg: 'centralu-linux-arm64', artifact: `${APP_NAME}.AppImage` },
|
|
52
|
-
'linux-x64': { pkg: 'centralu-linux-x64', artifact: `${APP_NAME}.AppImage` },
|
|
53
|
-
}
|
|
91
|
+
/** Where `centralu install` copies the app on Windows (see `windowsInstall`) */
|
|
92
|
+
const WIN = PLATFORM === 'win32' ? windowsInstall(process.env, HOME) : null
|
|
54
93
|
|
|
55
|
-
|
|
94
|
+
/** The arch package for this machine (the table lives in platform.mjs) */
|
|
95
|
+
const TARGET = targetFor(PLATFORM, process.arch)
|
|
56
96
|
|
|
57
97
|
/** The app inside the architecture-specific package. null if it cannot be found. */
|
|
58
98
|
function bundledApp() {
|
|
@@ -64,7 +104,7 @@ function bundledApp() {
|
|
|
64
104
|
return null // The package for this architecture is not installed — the reason is explained below.
|
|
65
105
|
}
|
|
66
106
|
const app = join(root, TARGET.artifact)
|
|
67
|
-
return existsSync(app) ? app : null
|
|
107
|
+
return existsSync(executableIn(PLATFORM, app)) ? app : null
|
|
68
108
|
}
|
|
69
109
|
|
|
70
110
|
/**
|
|
@@ -75,7 +115,7 @@ function explainMissing() {
|
|
|
75
115
|
if (!TARGET) {
|
|
76
116
|
// Kept as its own case: "not published for your machine" and "not published for
|
|
77
117
|
// Intel Macs, and here is the reason" send the user to different places.
|
|
78
|
-
if (
|
|
118
|
+
if (PLATFORM === 'darwin') {
|
|
79
119
|
return (
|
|
80
120
|
`${APP_NAME} is Apple Silicon only for now (current architecture: ${process.arch}).\n` +
|
|
81
121
|
'Intel Mac support needs native addons bundled in as well, and is tracked as separate work.'
|
|
@@ -83,7 +123,7 @@ function explainMissing() {
|
|
|
83
123
|
}
|
|
84
124
|
const supported = Object.keys(TARGETS).join(', ')
|
|
85
125
|
return (
|
|
86
|
-
`${APP_NAME} does not support ${
|
|
126
|
+
`${APP_NAME} does not support ${PLATFORM}/${process.arch} yet.\n` +
|
|
87
127
|
`Currently supported combinations: ${supported}\n` +
|
|
88
128
|
'Progress is tracked at https://github.com/ijun17/centralu/issues/14.'
|
|
89
129
|
)
|
|
@@ -94,6 +134,43 @@ function explainMissing() {
|
|
|
94
134
|
)
|
|
95
135
|
}
|
|
96
136
|
|
|
137
|
+
/**
|
|
138
|
+
* The bundled host's entry (`resources/host/main.mjs`) for `centralu serve`, or exit with why not:
|
|
139
|
+
* from the platform package, or from the host-only package where a hub installed this (`findHostEntry`).
|
|
140
|
+
*
|
|
141
|
+
* `CENTRALU_HOST_ENTRY` points it at another host instead: a source checkout's
|
|
142
|
+
* `packages/agent-host/src/main.ts` while developing, or a freshly bundled `resources/host/main.mjs`.
|
|
143
|
+
* The tests start `serve` that way (tooling/launcher-serve.test.ts).
|
|
144
|
+
*/
|
|
145
|
+
function requireHostEntry() {
|
|
146
|
+
if (process.env.CENTRALU_HOST_ENTRY) return process.env.CENTRALU_HOST_ENTRY
|
|
147
|
+
if (!TARGET) {
|
|
148
|
+
console.error(explainMissing())
|
|
149
|
+
process.exit(1)
|
|
150
|
+
}
|
|
151
|
+
// The platform package of an npm install, else the host-only package a hub installed (platform.mjs)
|
|
152
|
+
const found = findHostEntry(PLATFORM, process.arch, {
|
|
153
|
+
resolveRoot: (name) => {
|
|
154
|
+
try {
|
|
155
|
+
return dirname(require.resolve(`${name}/package.json`))
|
|
156
|
+
} catch {
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
exists: existsSync,
|
|
161
|
+
})
|
|
162
|
+
if (found.entry) return found.entry
|
|
163
|
+
if (found.missingHost) {
|
|
164
|
+
console.error(
|
|
165
|
+
`The host is missing from ${found.missingHost.pkg} (looked for ${found.missingHost.entry}).\n` +
|
|
166
|
+
'Linux packages published before `centralu serve` existed do not carry it. Update with `npm i -g centralu`.',
|
|
167
|
+
)
|
|
168
|
+
process.exit(1)
|
|
169
|
+
}
|
|
170
|
+
console.error(explainMissing())
|
|
171
|
+
process.exit(1)
|
|
172
|
+
}
|
|
173
|
+
|
|
97
174
|
function requireApp() {
|
|
98
175
|
const app = bundledApp()
|
|
99
176
|
if (!app) {
|
|
@@ -104,21 +181,27 @@ function requireApp() {
|
|
|
104
181
|
}
|
|
105
182
|
|
|
106
183
|
/**
|
|
107
|
-
* Launches the app. If it is installed in `/Applications
|
|
184
|
+
* Launches the app. If it is installed in `/Applications` (macOS) or
|
|
185
|
+
* `%LOCALAPPDATA%\Programs\Centralu` (Windows), that copy is used first.
|
|
108
186
|
*
|
|
109
|
-
*
|
|
187
|
+
* Linux has no such preference, and not for lack of an equivalent: `centralu install`
|
|
110
188
|
* on Linux writes a launcher that points back at this same package, so there is never a
|
|
111
189
|
* second copy to prefer.
|
|
190
|
+
*
|
|
191
|
+
* Resolves once the launcher has nothing more to wait for. Only Windows waits at all —
|
|
192
|
+
* briefly, to report an exe that dies on start (see runWindows).
|
|
112
193
|
*/
|
|
113
|
-
function run(args) {
|
|
114
|
-
if (
|
|
194
|
+
async function run(args) {
|
|
195
|
+
if (PLATFORM === 'darwin') {
|
|
115
196
|
const app = existsSync(INSTALLED) ? INSTALLED : requireApp()
|
|
116
197
|
// `open` goes through LaunchServices — the dock icon and single-instance behavior only
|
|
117
198
|
// work correctly that way.
|
|
118
|
-
const
|
|
199
|
+
const plan = launchPlan(PLATFORM, app, args, HOME)
|
|
200
|
+
const r = spawn(plan.command, plan.args, plan.options)
|
|
119
201
|
r.on('exit', (code) => process.exit(code ?? 0))
|
|
120
202
|
return
|
|
121
203
|
}
|
|
204
|
+
if (PLATFORM === 'win32') return runWindows(args)
|
|
122
205
|
// Linux has no LaunchServices — the AppImage is its own launcher, so run it directly.
|
|
123
206
|
//
|
|
124
207
|
// Staying attached to the terminal is on purpose. The two usual first failures on
|
|
@@ -127,7 +210,8 @@ function run(args) {
|
|
|
127
210
|
// missing system library from the loader. Detaching would hand back the shell prompt
|
|
128
211
|
// and throw away the one line that says what to do.
|
|
129
212
|
const app = requireApp()
|
|
130
|
-
const
|
|
213
|
+
const plan = launchPlan(PLATFORM, app, args, HOME)
|
|
214
|
+
const r = spawn(plan.command, plan.args, plan.options)
|
|
131
215
|
r.on('error', (e) => {
|
|
132
216
|
console.error(`Failed to run ${app}: ${e.message}`)
|
|
133
217
|
// EACCES here means the executable bit did not survive the trip through npm, which
|
|
@@ -138,23 +222,101 @@ function run(args) {
|
|
|
138
222
|
r.on('exit', (code) => process.exit(code ?? 0))
|
|
139
223
|
}
|
|
140
224
|
|
|
225
|
+
/** How long to watch a freshly started exe for an immediate exit */
|
|
226
|
+
const EARLY_EXIT_MS = 3000
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Windows: check WebView2, start `centralu.exe` detached, and watch it for a few seconds.
|
|
230
|
+
*
|
|
231
|
+
* The app is a GUI program, so a failure to start has nowhere to print. Without WebView2
|
|
232
|
+
* the exe ends before it has a window, and the person sees nothing happen at all — the
|
|
233
|
+
* registry check turns that into a message with the download link before anything starts.
|
|
234
|
+
* The short watch afterwards catches whatever the registry cannot see (a broken runtime, a
|
|
235
|
+
* blocked exe), then lets go so the app outlives this launcher.
|
|
236
|
+
*/
|
|
237
|
+
async function runWindows(args) {
|
|
238
|
+
const webview = webview2Status(process.env, queryRegistry)
|
|
239
|
+
if (webview.state === 'missing') {
|
|
240
|
+
console.error(webview2MissingMessage())
|
|
241
|
+
process.exit(1)
|
|
242
|
+
}
|
|
243
|
+
const app = existsSync(WIN.exe) ? WIN.dir : requireApp()
|
|
244
|
+
const plan = launchPlan(PLATFORM, app, args, HOME)
|
|
245
|
+
const child = spawn(plan.command, plan.args, plan.options)
|
|
246
|
+
await new Promise((resolve) => {
|
|
247
|
+
const done = () => {
|
|
248
|
+
clearTimeout(timer)
|
|
249
|
+
child.removeAllListeners()
|
|
250
|
+
child.unref()
|
|
251
|
+
resolve()
|
|
252
|
+
}
|
|
253
|
+
const timer = setTimeout(done, EARLY_EXIT_MS)
|
|
254
|
+
child.on('error', (e) => {
|
|
255
|
+
console.error(`Failed to start ${plan.command}: ${e.message}`)
|
|
256
|
+
process.exitCode = 1
|
|
257
|
+
done()
|
|
258
|
+
})
|
|
259
|
+
child.on('exit', (code) => {
|
|
260
|
+
// 0 is a normal answer this early: a second start hands over to the window that is
|
|
261
|
+
// already open and leaves.
|
|
262
|
+
if (code !== 0 && code !== null) {
|
|
263
|
+
console.error(earlyExitMessage(code, HOME))
|
|
264
|
+
process.exitCode = 1
|
|
265
|
+
}
|
|
266
|
+
done()
|
|
267
|
+
})
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* `reg query <key> /v pv`: stdout, null when the key or value does not exist (reg exits 1),
|
|
273
|
+
* undefined when reg itself could not run.
|
|
274
|
+
*/
|
|
275
|
+
function queryRegistry(key) {
|
|
276
|
+
try {
|
|
277
|
+
return execFileSync(regExe(process.env), ['query', key, '/v', 'pv'], {
|
|
278
|
+
encoding: 'utf8',
|
|
279
|
+
stdio: ['ignore', 'pipe', 'ignore'],
|
|
280
|
+
windowsHide: true,
|
|
281
|
+
})
|
|
282
|
+
} catch (e) {
|
|
283
|
+
return typeof e?.status === 'number' ? null : undefined
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
141
287
|
/**
|
|
142
288
|
* Registers the app in the system menu.
|
|
143
289
|
*
|
|
144
290
|
* **Never done quietly through postinstall.** Writing silently into someone's `/Applications`
|
|
145
291
|
* erodes trust, and pnpm blocks postinstall by default anyway. This is only run when the
|
|
146
292
|
* person explicitly asks for it.
|
|
293
|
+
*
|
|
294
|
+
* **Never over a newer local build** (#645), unless `--force`. The in-app update runs this after
|
|
295
|
+
* `npm i -g` with the same rule (`keepsInstalledCopy`): a maintainer's local build
|
|
296
|
+
* (`0.2.0-beta.21.local.1`) waiting for "Apply now" would otherwise be replaced by npm's beta.21.
|
|
297
|
+
* Any released version is replaced, even a higher one: npm is the source of truth for releases,
|
|
298
|
+
* and a release pulled from `latest` must not survive here. The check comes before finding the app
|
|
299
|
+
* package, since keeping the copy needs nothing from it. `--force` replaces a local build on purpose.
|
|
147
300
|
*/
|
|
148
|
-
function install() {
|
|
149
|
-
const
|
|
150
|
-
if (
|
|
151
|
-
|
|
152
|
-
console.log(
|
|
153
|
-
|
|
301
|
+
function install(force) {
|
|
302
|
+
const copy = installedCopyVersion()
|
|
303
|
+
if (!force && keepsInstalledCopy(pkg.version, copy)) {
|
|
304
|
+
const where = PLATFORM === 'win32' ? WIN.dir : INSTALLED
|
|
305
|
+
console.log(`${where} holds the local build ${copy}, newer than this package (${pkg.version}), and was left as it is.`)
|
|
306
|
+
console.log('To replace it anyway: centralu install --force')
|
|
307
|
+
return
|
|
154
308
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
309
|
+
const app = requireApp()
|
|
310
|
+
if (PLATFORM === 'win32') return installWindows(app)
|
|
311
|
+
if (PLATFORM !== 'darwin') return installDesktopEntry(app)
|
|
312
|
+
if (existsSync(INSTALLED)) console.log(`Replacing the existing ${INSTALLED} with the new version.`)
|
|
313
|
+
// Copied beside the old one and swapped in by rename (swap.mjs): a copy that fails leaves the
|
|
314
|
+
// old app in place rather than none. ditto, not cp — carries over the bundle's permissions and
|
|
315
|
+
// extended attributes unchanged (so the signature does not break).
|
|
316
|
+
const { leftAt } = swapInto(INSTALLED, (staging) => {
|
|
317
|
+
execFileSync('/usr/bin/ditto', [app, staging], { stdio: 'inherit' })
|
|
318
|
+
})
|
|
319
|
+
if (leftAt) console.log(`The previous copy could not be removed and was left at ${leftAt}. Delete it yourself.`)
|
|
158
320
|
console.log(`Installed: ${INSTALLED}`)
|
|
159
321
|
console.log('It can now be found in Launchpad and Spotlight too.')
|
|
160
322
|
}
|
|
@@ -193,16 +355,105 @@ function installDesktopEntry(app) {
|
|
|
193
355
|
console.log('It can now be found in the app list too (some desktops require logging back in before it shows up).')
|
|
194
356
|
}
|
|
195
357
|
|
|
358
|
+
/**
|
|
359
|
+
* The Windows answer to "copy it into /Applications": copy the folder into
|
|
360
|
+
* `%LOCALAPPDATA%\Programs\Centralu` and put a shortcut in the Start menu.
|
|
361
|
+
*
|
|
362
|
+
* A copy rather than a shortcut into the npm package (the Linux shape), because Windows
|
|
363
|
+
* will not replace a running program's files: with the app running from the package,
|
|
364
|
+
* `npm i -g centralu@newer` fails. Running from the copy leaves the package free to update.
|
|
365
|
+
*
|
|
366
|
+
* The old copy is never deleted before the new one is complete. The new folder is
|
|
367
|
+
* assembled beside it, the old one is renamed aside, and only then is the new one moved in
|
|
368
|
+
* — so a copy that fails halfway, or an app that is still running, leaves the working
|
|
369
|
+
* install untouched rather than a folder with half its files.
|
|
370
|
+
*/
|
|
371
|
+
function installWindows(app) {
|
|
372
|
+
const staging = `${WIN.dir}.new`
|
|
373
|
+
const aside = `${WIN.dir}.old-${Date.now()}`
|
|
374
|
+
sweepAsideCopies()
|
|
375
|
+
rmSync(staging, { recursive: true, force: true })
|
|
376
|
+
mkdirSync(dirname(WIN.dir), { recursive: true })
|
|
377
|
+
cpSync(app, staging, { recursive: true })
|
|
378
|
+
writeFileSync(join(staging, 'centralu-version.txt'), `${pkg.version}\n`)
|
|
379
|
+
if (existsSync(WIN.dir)) {
|
|
380
|
+
console.log(`Replacing the existing ${WIN.dir} with the new version.`)
|
|
381
|
+
try {
|
|
382
|
+
renameSync(WIN.dir, aside)
|
|
383
|
+
} catch (e) {
|
|
384
|
+
rmSync(staging, { recursive: true, force: true })
|
|
385
|
+
if (!isBusyError(e)) throw e
|
|
386
|
+
console.error(busyMessage(WIN.dir))
|
|
387
|
+
process.exit(1)
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
renameSync(staging, WIN.dir)
|
|
391
|
+
try {
|
|
392
|
+
rmSync(aside, { recursive: true, force: true })
|
|
393
|
+
} catch {
|
|
394
|
+
// A rename can succeed while the old copy is running; its files then stay locked until
|
|
395
|
+
// it quits. Harmless — it is no longer the one anything starts — but worth a line.
|
|
396
|
+
console.log(`The previous copy is still in use and was left at ${aside}. Delete it after quitting ${APP_NAME}.`)
|
|
397
|
+
}
|
|
398
|
+
const sc = shortcutCommand(process.env, { shortcut: WIN.shortcut, target: WIN.exe, workdir: HOME })
|
|
399
|
+
try {
|
|
400
|
+
execFileSync(sc.command, sc.args, { env: sc.env, stdio: ['ignore', 'ignore', 'inherit'], windowsHide: true })
|
|
401
|
+
} catch (e) {
|
|
402
|
+
console.log(`Installed: ${WIN.dir}`)
|
|
403
|
+
console.error(`Could not create the Start menu shortcut (${e.message}). Start ${WIN.exe} directly, or run \`centralu\`.`)
|
|
404
|
+
process.exit(1)
|
|
405
|
+
}
|
|
406
|
+
console.log(`Installed: ${WIN.dir}`)
|
|
407
|
+
console.log(`Start menu: ${WIN.shortcut}`)
|
|
408
|
+
console.log('It can now be found in Start and Windows search, and `centralu` starts this copy too.')
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Removes the copies earlier installs renamed aside (`Centralu.old-<time>`), whichever can go.
|
|
413
|
+
*
|
|
414
|
+
* An update from inside the app renames the running copy aside, and Windows will not delete a
|
|
415
|
+
* running exe, so that copy stays until the app quits. Nothing else would ever remove it, and
|
|
416
|
+
* each in-app update would leave one more (about 70 files each, measured 2026-10-08). One still
|
|
417
|
+
* in use is left for the next install; deleting what can be deleted from it does not disturb the
|
|
418
|
+
* app running from it, which reaches its files by the folder's original path, now the new copy.
|
|
419
|
+
*/
|
|
420
|
+
function sweepAsideCopies() {
|
|
421
|
+
const parent = dirname(WIN.dir)
|
|
422
|
+
let names
|
|
423
|
+
try {
|
|
424
|
+
names = readdirSync(parent)
|
|
425
|
+
} catch {
|
|
426
|
+
return
|
|
427
|
+
}
|
|
428
|
+
for (const name of asideCopies(names)) {
|
|
429
|
+
try {
|
|
430
|
+
rmSync(join(parent, name), { recursive: true, force: true })
|
|
431
|
+
} catch {
|
|
432
|
+
// Still in use: the next install tries again
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
196
437
|
function uninstall() {
|
|
197
|
-
const
|
|
198
|
-
|
|
199
|
-
|
|
438
|
+
const paths = installedPaths(PLATFORM, process.env, HOME)
|
|
439
|
+
const present = paths.filter((p) => existsSync(p))
|
|
440
|
+
if (present.length === 0) {
|
|
441
|
+
console.log(`${paths[0]} does not exist — nothing to remove.`)
|
|
200
442
|
return
|
|
201
443
|
}
|
|
202
|
-
|
|
203
|
-
|
|
444
|
+
for (const installed of present) {
|
|
445
|
+
try {
|
|
446
|
+
rmSync(installed, { recursive: true, force: true })
|
|
447
|
+
} catch (e) {
|
|
448
|
+
if (PLATFORM !== 'win32' || !isBusyError(e)) throw e
|
|
449
|
+
console.error(busyMessage(installed))
|
|
450
|
+
process.exit(1)
|
|
451
|
+
}
|
|
452
|
+
console.log(`Removed: ${installed}`)
|
|
453
|
+
}
|
|
204
454
|
console.log('To remove the package itself: npm uninstall -g centralu')
|
|
205
|
-
|
|
455
|
+
const data = PLATFORM === 'win32' ? '%USERPROFILE%\\.centralu' : '~/.centralu'
|
|
456
|
+
console.log(`Conversation history is left in place (${data}). Remove it yourself if you want to.`)
|
|
206
457
|
}
|
|
207
458
|
|
|
208
459
|
/**
|
|
@@ -250,31 +501,54 @@ async function update() {
|
|
|
250
501
|
return
|
|
251
502
|
}
|
|
252
503
|
console.log(`Upgrading ${pkg.version} → ${latest}.`)
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
504
|
+
const npm = npmCommand(PLATFORM, ['i', '-g', `${pkg.name}@${latest}`])
|
|
505
|
+
try {
|
|
506
|
+
execFileSync(npm.command, npm.args, npm.options)
|
|
507
|
+
} catch (e) {
|
|
508
|
+
// On Windows the usual cause is the app running from inside the package: its files are
|
|
509
|
+
// locked, and npm cannot replace them. npm's own output (above) has the details.
|
|
510
|
+
if (PLATFORM === 'win32') console.error(`\n${busyMessage('the npm package')}`)
|
|
511
|
+
process.exit(typeof e?.status === 'number' ? e.status : 1)
|
|
512
|
+
}
|
|
513
|
+
// Someone with a copy in /Applications (or %LOCALAPPDATA%\Programs) needs that updated
|
|
514
|
+
// too, or the old version stays behind.
|
|
256
515
|
// On Linux the menu entry points at the package instead of a copy, so rewriting it is
|
|
257
516
|
// cheap — but it is still worth doing, because the Exec path is what would go stale.
|
|
258
|
-
|
|
517
|
+
//
|
|
518
|
+
// The *new* launcher does the refresh: this file has just been replaced on disk. It is
|
|
519
|
+
// run through `node` rather than as a script, because Windows cannot execute a `.mjs`.
|
|
520
|
+
const installed = installedPaths(PLATFORM, process.env, HOME)[0]
|
|
259
521
|
if (existsSync(installed)) {
|
|
260
522
|
console.log(`Updating ${installed} too.`)
|
|
261
|
-
execFileSync(process.argv[1],
|
|
523
|
+
execFileSync(process.execPath, [process.argv[1], 'install'], { stdio: 'inherit' })
|
|
262
524
|
}
|
|
263
525
|
console.log('Done. If the app is open, please restart it.')
|
|
264
526
|
}
|
|
265
527
|
|
|
266
528
|
/**
|
|
267
529
|
* The version of the copy placed in /Applications. null if there is nothing to compare.
|
|
530
|
+
* (`bundleVersionCommand`: the in-app update reads it with the same command.)
|
|
268
531
|
*
|
|
269
532
|
* `defaults` is used because there is no guarantee Info.plist stays XML — Tauri just happens
|
|
270
533
|
* to write it as XML today, and if it ever switches to a binary plist, anything reading it
|
|
271
534
|
* with a regex would silently stop working. `defaults` is always present on a Mac and reads
|
|
272
535
|
* both formats.
|
|
536
|
+
*
|
|
537
|
+
* On Windows it is the file `centralu install` wrote beside the copy (see `windowsInstall`
|
|
538
|
+
* for why not the exe's own version).
|
|
273
539
|
*/
|
|
274
540
|
function installedCopyVersion() {
|
|
275
|
-
if (
|
|
541
|
+
if (PLATFORM === 'win32') {
|
|
542
|
+
try {
|
|
543
|
+
return readFileSync(WIN.versionFile, 'utf8').trim()
|
|
544
|
+
} catch {
|
|
545
|
+
return null
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
if (PLATFORM !== 'darwin' || !existsSync(INSTALLED)) return null
|
|
276
549
|
try {
|
|
277
|
-
const
|
|
550
|
+
const read = bundleVersionCommand(INSTALLED)
|
|
551
|
+
const out = execFileSync(read.command, read.args, {
|
|
278
552
|
encoding: 'utf8',
|
|
279
553
|
stdio: ['ignore', 'pipe', 'ignore'],
|
|
280
554
|
})
|
|
@@ -300,7 +574,8 @@ function installedCopyVersion() {
|
|
|
300
574
|
function notifyIfCopyStale() {
|
|
301
575
|
const version = installedCopyVersion()
|
|
302
576
|
if (!copyDiffers(pkg.version, version)) return
|
|
303
|
-
|
|
577
|
+
const where = PLATFORM === 'win32' ? WIN.dir : '/Applications'
|
|
578
|
+
console.log(`\nThe ${where} copy is ${version} (this package is ${pkg.version}).\n centralu install`)
|
|
304
579
|
}
|
|
305
580
|
|
|
306
581
|
/** Only announced after the app has launched — checking for updates must never delay startup. */
|
|
@@ -316,17 +591,21 @@ async function notifyIfOutdated() {
|
|
|
316
591
|
const HELP = `${APP_NAME} ${pkg.version}
|
|
317
592
|
|
|
318
593
|
centralu Launches the app
|
|
319
|
-
centralu install Registers it in the app list (macOS: /Applications, Linux: a menu entry
|
|
594
|
+
centralu install Registers it in the app list (macOS: /Applications, Linux: a menu entry,
|
|
595
|
+
Windows: %LOCALAPPDATA%\\Programs\\Centralu and a Start menu shortcut).
|
|
596
|
+
A newer local build there is kept; --force replaces it anyway
|
|
320
597
|
centralu uninstall Removes that registration (conversation history is kept)
|
|
321
598
|
centralu update Upgrades if a new version is available
|
|
599
|
+
centralu serve Runs the host alone on this machine, for the app on another computer
|
|
600
|
+
to reach over SSH (\`centralu serve --help\`)
|
|
322
601
|
centralu --version Prints the version
|
|
323
602
|
|
|
324
|
-
Requires: Node
|
|
603
|
+
Requires: Node ${NODE_FLOOR}+, and the claude or codex CLI (the app's first screen reports the status)`
|
|
325
604
|
|
|
326
605
|
const [cmd, ...rest] = process.argv.slice(2)
|
|
327
606
|
switch (cmd) {
|
|
328
607
|
case 'install':
|
|
329
|
-
install()
|
|
608
|
+
install(rest.includes('--force'))
|
|
330
609
|
break
|
|
331
610
|
case 'uninstall':
|
|
332
611
|
uninstall()
|
|
@@ -334,6 +613,98 @@ switch (cmd) {
|
|
|
334
613
|
case 'update':
|
|
335
614
|
await update()
|
|
336
615
|
break
|
|
616
|
+
case 'serve': {
|
|
617
|
+
// No update notice here: `--connection`'s stdout is read by a program, and a foreground
|
|
618
|
+
// server's terminal is a log.
|
|
619
|
+
const args = parseServeArgs(rest)
|
|
620
|
+
if (args.error) {
|
|
621
|
+
console.error(args.error)
|
|
622
|
+
process.exit(2)
|
|
623
|
+
}
|
|
624
|
+
if (args.mode === 'help') {
|
|
625
|
+
console.log(SERVE_HELP)
|
|
626
|
+
break
|
|
627
|
+
}
|
|
628
|
+
try {
|
|
629
|
+
// One full credential withdrawn (#1405): like --rotate-token, nothing to start or describe
|
|
630
|
+
if (args.mode === 'revoke') {
|
|
631
|
+
process.exitCode = revokeCredential({ env: process.env, home: HOME, id: args.revoke })
|
|
632
|
+
break
|
|
633
|
+
}
|
|
634
|
+
if (args.mode === 'rotate') {
|
|
635
|
+
process.exitCode = rotateToken({ env: process.env, home: HOME })
|
|
636
|
+
break
|
|
637
|
+
}
|
|
638
|
+
// The hub's switch of the install it manages (serve-switch.mjs): it starts and checks hosts only through serve's CLI
|
|
639
|
+
if (args.mode === 'switch') {
|
|
640
|
+
process.exitCode = await runSwitch({ env: process.env, home: HOME, raw: args.switchArg, cliPath: realpathSync(process.argv[1]) })
|
|
641
|
+
break
|
|
642
|
+
}
|
|
643
|
+
if (args.mode === 'switch-run') {
|
|
644
|
+
process.exitCode = await runSwitchRun({ env: process.env, home: HOME, raw: args.switchArg, dataDir: args.dataDir })
|
|
645
|
+
break
|
|
646
|
+
}
|
|
647
|
+
if (args.mode === 'switch-status') {
|
|
648
|
+
process.exitCode = await runSwitchStatus({ env: process.env, home: HOME, seen: args.seen })
|
|
649
|
+
break
|
|
650
|
+
}
|
|
651
|
+
/*
|
|
652
|
+
* Before anything spawns a host: under a Node below the floor the host crashes on its first
|
|
653
|
+
* database call, with nothing in its log (issue #699). `--detach` also answers in the line the
|
|
654
|
+
* hub reads, so it can offer its managed install instead of quoting a crash. `--autostart on`
|
|
655
|
+
* writes a boot entry that would start that host. `--connection`, `--stop`, `--autostart
|
|
656
|
+
* off|status` and `--rotate-token` start no host and keep working, so the hub can still ask this
|
|
657
|
+
* machine what Node it has.
|
|
658
|
+
*/
|
|
659
|
+
const startsHost = args.mode === 'serve' || args.mode === 'detach' || args.mode === 'window' || (args.mode === 'autostart' && args.autostart === 'on')
|
|
660
|
+
/*
|
|
661
|
+
* A switch of the managed install under way there (§10.5a): no host starts between its stop and its start, and one
|
|
662
|
+
* whose switcher died is finished first, before this version refuses for a reason of its own (a host already
|
|
663
|
+
* answering, a missing host entry, a Node below the floor), so a reboot or a sign-in finishes it with no hub (#1907).
|
|
664
|
+
* Not for the launcher `--detach` started: that one asked already
|
|
665
|
+
*/
|
|
666
|
+
if ((args.mode === 'serve' || args.mode === 'detach' || args.mode === 'window') && args.child === undefined) {
|
|
667
|
+
const refused = await gateHostStart({ env: process.env, home: HOME, version: pkg.version, detach: args.mode === 'detach' })
|
|
668
|
+
if (refused !== null) {
|
|
669
|
+
process.exitCode = refused
|
|
670
|
+
break
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
if (startsHost && belowFloor(process.versions.node, NODE_FLOOR)) {
|
|
674
|
+
const message = oldNodeSentence(process.versions.node, NODE_FLOOR)
|
|
675
|
+
console.error(`centralu serve: ${message}`)
|
|
676
|
+
if (args.mode === 'detach') {
|
|
677
|
+
const line = { v: 1, detach: { ok: false, how: null, reason: 'node_too_old', message, node: process.versions.node, floor: NODE_FLOOR } }
|
|
678
|
+
process.stdout.write(`${JSON.stringify(line)}\n`)
|
|
679
|
+
}
|
|
680
|
+
// Not process.exit: the line above is on a pipe, and exiting at once may cut it
|
|
681
|
+
process.exitCode = 1
|
|
682
|
+
break
|
|
683
|
+
}
|
|
684
|
+
// A launcher `--detach` started: what it carried (the data folder, a host entry) applies before
|
|
685
|
+
// anything below reads the environment
|
|
686
|
+
const spec = args.child !== undefined ? decodeChildSpec(args.child) : null
|
|
687
|
+
if (args.child !== undefined && !spec) {
|
|
688
|
+
console.error('centralu serve: --detached-child is how --detach starts serve; this one could not be read')
|
|
689
|
+
process.exit(2)
|
|
690
|
+
}
|
|
691
|
+
if (spec) Object.assign(process.env, spec.env)
|
|
692
|
+
// realpath: npm's bin is a symlink to this file, and the launcher must not depend on the link
|
|
693
|
+
const opts = { env: process.env, home: HOME, entry: requireHostEntry(), version: pkg.version, cliPath: realpathSync(process.argv[1]) }
|
|
694
|
+
if (args.mode === 'connection') process.exitCode = await printConnection({ ...opts, pair: args.pair ?? null, pairKey: args.pairKey ?? null, keyPaired: args.keyPaired === true })
|
|
695
|
+
else if (args.mode === 'detach') process.exitCode = await runDetach({ ...opts, port: args.port, from: args.from })
|
|
696
|
+
else if (args.mode === 'stop') process.exitCode = await runStop(opts)
|
|
697
|
+
else if (args.mode === 'window') process.exitCode = await runWindow(opts)
|
|
698
|
+
else if (args.mode === 'autostart') process.exitCode = await runAutostart({ ...opts, action: args.autostart })
|
|
699
|
+
else if (spec) process.exitCode = await runDetachedChild({ ...opts, spec, port: args.port })
|
|
700
|
+
else process.exitCode = await runServe({ ...opts, port: args.port, from: args.from })
|
|
701
|
+
} catch (e) {
|
|
702
|
+
// A state file that cannot be read or written: one sentence, not a stack, and nothing on stdout
|
|
703
|
+
console.error(`centralu serve: ${e?.message ?? e}`)
|
|
704
|
+
process.exitCode = 1
|
|
705
|
+
}
|
|
706
|
+
break
|
|
707
|
+
}
|
|
337
708
|
case '--version':
|
|
338
709
|
case '-v':
|
|
339
710
|
console.log(pkg.version)
|
|
@@ -343,8 +714,10 @@ switch (cmd) {
|
|
|
343
714
|
case 'help':
|
|
344
715
|
console.log(HELP)
|
|
345
716
|
break
|
|
346
|
-
default:
|
|
347
|
-
|
|
717
|
+
default: {
|
|
718
|
+
// Started before the update check, never after it: the check must not delay startup.
|
|
719
|
+
const launched = run(cmd ? [cmd, ...rest] : [])
|
|
348
720
|
notifyIfCopyStale()
|
|
349
|
-
await notifyIfOutdated()
|
|
721
|
+
await Promise.all([launched, notifyIfOutdated()])
|
|
722
|
+
}
|
|
350
723
|
}
|