easy-devops 1.2.1 → 1.3.1

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.
Files changed (126) hide show
  1. package/README.md +97 -6
  2. package/dist/ui/assets/index-CMRzkIKj.css +1 -0
  3. package/dist/ui/assets/index-DEouL3IK.js +452 -0
  4. package/dist/ui/assets/socket-DwLPrgsr.js +1 -0
  5. package/dist/ui/assets/socket-K5ttLD2q.js +65 -0
  6. package/dist/ui/brand/apple-touch-icon.png +0 -0
  7. package/dist/ui/brand/favicon-16.png +0 -0
  8. package/dist/ui/brand/favicon-32.png +0 -0
  9. package/dist/ui/brand/favicon-light-16.png +0 -0
  10. package/dist/ui/brand/favicon-light-32.png +0 -0
  11. package/dist/ui/brand/favicon.ico +0 -0
  12. package/dist/ui/brand/icon-192.png +0 -0
  13. package/dist/ui/brand/icon-48.png +0 -0
  14. package/dist/ui/brand/icon-512.png +0 -0
  15. package/dist/ui/brand/icon-64-light.png +0 -0
  16. package/dist/ui/brand/icon-64.png +0 -0
  17. package/dist/ui/brand/logo-dark.png +0 -0
  18. package/dist/ui/brand/logo-light.png +0 -0
  19. package/dist/ui/brand/mark-dark.png +0 -0
  20. package/dist/ui/brand/mark-light.png +0 -0
  21. package/dist/ui/index.html +71 -0
  22. package/install.ps1 +188 -1
  23. package/install.sh +254 -29
  24. package/package.json +15 -4
  25. package/src/cli/dash.js +110 -0
  26. package/src/cli/index.js +237 -106
  27. package/src/cli/managers/domain-manager.js +60 -40
  28. package/src/cli/managers/ssl-manager.js +47 -330
  29. package/src/cli/menu.js +156 -0
  30. package/src/cli/menus/cleanup.js +205 -0
  31. package/src/cli/menus/dashboard.js +92 -1
  32. package/src/cli/menus/runtime.js +236 -0
  33. package/src/cli/menus/service.js +764 -0
  34. package/src/cli/menus/settings.js +176 -8
  35. package/src/cli/menus/uninstall.js +167 -0
  36. package/src/cli/menus/update.js +222 -47
  37. package/src/core/acme-challenge.js +273 -0
  38. package/src/core/cert-operations.js +189 -0
  39. package/src/{dashboard/lib → core}/cert-reader.js +2 -2
  40. package/src/core/cert-renewal.js +494 -0
  41. package/src/core/cleanup.js +600 -0
  42. package/src/core/config.js +169 -2
  43. package/src/core/daemon/ipc.js +292 -0
  44. package/src/core/daemon/lock.js +294 -0
  45. package/src/core/daemon/run.js +241 -0
  46. package/src/core/daemon/stopRequest.js +162 -0
  47. package/src/core/db.js +37 -8
  48. package/src/core/deploy/apps.js +491 -0
  49. package/src/core/deploy/domainLink.js +200 -0
  50. package/src/core/deploy/engine.js +973 -0
  51. package/src/core/deploy/ezfile.js +361 -0
  52. package/src/core/deploy/github.js +239 -0
  53. package/src/core/deploy/githubApi.js +261 -0
  54. package/src/core/deploy/hookServer.js +325 -0
  55. package/src/core/deploy/hubCommands.js +267 -0
  56. package/src/core/deploy/launcher.js +57 -0
  57. package/src/core/deploy/status.js +91 -0
  58. package/src/core/deploy/store.js +398 -0
  59. package/src/core/domainNotifier.js +94 -28
  60. package/src/core/domains-db.js +234 -0
  61. package/src/core/events.js +94 -2
  62. package/src/core/hub/agent.js +842 -0
  63. package/src/core/hubLink.js +306 -0
  64. package/src/core/hubPermissions.js +430 -0
  65. package/src/core/installSource.js +162 -0
  66. package/src/core/jobs/bus.js +120 -0
  67. package/src/core/jobs/index.js +371 -0
  68. package/src/core/jobs/monitors.js +368 -0
  69. package/src/core/jobs/owner.js +217 -0
  70. package/src/core/jobs/revisionWatcher.js +106 -0
  71. package/src/core/nginx-apply.js +213 -0
  72. package/src/core/nginx-conf-generator.js +253 -17
  73. package/src/{dashboard/lib → core}/nginx-service.js +18 -9
  74. package/src/core/notifications/claim.js +214 -0
  75. package/src/core/notifications/dispatch.js +154 -0
  76. package/src/core/notifier.js +31 -1
  77. package/src/core/passwords.js +172 -0
  78. package/src/core/permissions.js +8 -4
  79. package/src/core/platform.js +709 -2
  80. package/src/core/probeCache.js +258 -0
  81. package/src/core/purge.js +372 -0
  82. package/src/core/runner.js +488 -0
  83. package/src/core/service/acl.js +226 -0
  84. package/src/core/service/dataDir.js +345 -0
  85. package/src/core/service/index.js +1128 -0
  86. package/src/core/service/legacy.js +218 -0
  87. package/src/core/service/runtime.js +446 -0
  88. package/src/core/service/runtimeUpdate.js +800 -0
  89. package/src/core/service/systemd.js +458 -0
  90. package/src/core/service/windowsPersonal.js +530 -0
  91. package/src/core/service/windowsService.js +576 -0
  92. package/src/core/service/winsw.js +260 -0
  93. package/src/core/setupCode.js +331 -0
  94. package/src/core/shell.js +70 -7
  95. package/src/core/ssl-issuer.js +627 -0
  96. package/src/core/updater.js +295 -0
  97. package/src/core/validators.js +99 -0
  98. package/src/dashboard/controllers/authController.js +302 -15
  99. package/src/dashboard/controllers/deploymentsController.js +479 -0
  100. package/src/dashboard/controllers/domainsController.js +80 -61
  101. package/src/dashboard/controllers/hubController.js +135 -0
  102. package/src/dashboard/controllers/nginxController.js +21 -3
  103. package/src/dashboard/controllers/processesController.js +368 -0
  104. package/src/dashboard/controllers/settingsController.js +180 -12
  105. package/src/dashboard/controllers/sslController.js +288 -10
  106. package/src/dashboard/lib/daemonBridge.js +163 -0
  107. package/src/dashboard/lib/localRequest.js +235 -0
  108. package/src/dashboard/lib/loginRateLimit.js +119 -0
  109. package/src/dashboard/lib/requireAuth.js +80 -0
  110. package/src/dashboard/public/js/app.js +489 -19
  111. package/src/dashboard/routes/auth.js +10 -10
  112. package/src/dashboard/routes/deployments.js +46 -0
  113. package/src/dashboard/routes/domains.js +9 -0
  114. package/src/dashboard/routes/hub.js +22 -0
  115. package/src/dashboard/routes/nginx.js +9 -0
  116. package/src/dashboard/routes/processes.js +29 -0
  117. package/src/dashboard/routes/settings.js +9 -0
  118. package/src/dashboard/routes/ssl.js +10 -0
  119. package/src/dashboard/server.js +288 -21
  120. package/src/dashboard/socketManager.js +171 -207
  121. package/src/dashboard/views/index.ejs +1 -0
  122. package/src/dashboard/views/partials/login.ejs +68 -7
  123. package/src/dashboard/views/partials/processes-panel.ejs +261 -0
  124. package/src/dashboard/views/partials/settings-panel.ejs +287 -0
  125. package/src/dashboard/views/partials/sidebar.ejs +2 -0
  126. package/src/dashboard/lib/domains-db.js +0 -51
@@ -0,0 +1,764 @@
1
+ /**
2
+ * cli/menus/service.js
3
+ *
4
+ * `ezz service install | uninstall | status | start | stop | restart | logs | repair`
5
+ *
6
+ * The CLI owns every question. `core/service/` sequences the work but never
7
+ * prompts, so the dashboard cannot trigger anything interactive and the core
8
+ * stays callable from a test with no terminal.
9
+ *
10
+ * Installing a Windows service in Server mode needs an Administrator terminal,
11
+ * which is why that path is CLI-only and says so. **No password is ever
12
+ * requested here or anywhere below it** — on Linux sudo does its own asking,
13
+ * and the Windows service runs as a virtual account that has no password.
14
+ */
15
+
16
+ import chalk from 'chalk';
17
+ import inquirer from 'inquirer';
18
+ import ora from 'ora';
19
+
20
+ import { isWindows, isDevDataDir, getDataDir } from '../../core/platform.js';
21
+ import * as service from '../../core/service/index.js';
22
+ import { describeLegacy } from '../../core/service/legacy.js';
23
+
24
+ const ACTIONS = ['install', 'uninstall', 'status', 'start', 'stop', 'restart', 'logs', 'repair'];
25
+
26
+ /**
27
+ * Entry point for the `service` command.
28
+ * @param {string} action
29
+ * @returns {Promise<number>} Process exit code
30
+ */
31
+ export async function serviceCommand(action) {
32
+ if (!ACTIONS.includes(action)) {
33
+ process.stderr.write(
34
+ `Unknown service action: ${action}\n` +
35
+ `Available: ${ACTIONS.join(', ')}\n`
36
+ );
37
+ return 1;
38
+ }
39
+
40
+ switch (action) {
41
+ case 'status': return showStatus();
42
+ case 'install': return doInstall();
43
+ case 'uninstall': return doUninstall();
44
+ case 'start': return doStart();
45
+ case 'stop': return doStop();
46
+ case 'restart': return doRestart();
47
+ case 'logs': return showLogs();
48
+ case 'repair': return doRepair();
49
+ default: return 1;
50
+ }
51
+ }
52
+
53
+ // ─── Shared rendering ─────────────────────────────────────────────────────────
54
+
55
+ const dot = (ok) => (ok ? chalk.green('●') : chalk.red('●'));
56
+ /** Unknown is its own colour: it is a missing answer, not a failure. */
57
+ const maybeDot = (v) => (v === null || v === undefined ? chalk.yellow('●') : dot(v));
58
+
59
+ const MECHANISM_LABELS = {
60
+ systemd: 'systemd unit',
61
+ winsw: 'Windows Service (WinSW)',
62
+ 'startup-supervisor': 'Startup-folder supervisor',
63
+ none: 'none',
64
+ };
65
+
66
+ /**
67
+ * A timestamp a person can read, in their own timezone.
68
+ *
69
+ * ISO-8601 UTC ("2026-11-14T23:21:18.682Z") was being printed straight from
70
+ * the stored value, which is both the wrong clock for the reader and harder to
71
+ * scan than it needs to be. The stored value stays ISO — it is a record — and
72
+ * only the display is converted.
73
+ *
74
+ * @param {string|number|null} value ISO string or epoch milliseconds
75
+ * @returns {string}
76
+ */
77
+ function localTime(value) {
78
+ if (value === null || value === undefined) return '—';
79
+
80
+ const date = value instanceof Date ? value : new Date(value);
81
+ if (Number.isNaN(date.getTime())) return String(value);
82
+
83
+ const pad = (n) => String(n).padStart(2, '0');
84
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} `
85
+ + `${pad(date.getHours())}:${pad(date.getMinutes())}`;
86
+ }
87
+
88
+ function humanDuration(ms) {
89
+ if (ms === null || ms === undefined) return '—';
90
+ const s = Math.floor(ms / 1000);
91
+ if (s < 60) return `${s}s`;
92
+ const m = Math.floor(s / 60);
93
+ if (m < 60) return `${m}m`;
94
+ const h = Math.floor(m / 60);
95
+ if (h < 48) return `${h}h ${m % 60}m`;
96
+ return `${Math.floor(h / 24)}d ${h % 24}h`;
97
+ }
98
+
99
+ function printWarnings(warnings) {
100
+ if (!warnings?.length) return;
101
+ console.log();
102
+ for (const warning of warnings) {
103
+ const [first, ...rest] = String(warning).split('\n');
104
+ console.log(' ' + chalk.yellow('⚠ ') + chalk.yellow(first));
105
+ for (const line of rest) console.log(' ' + chalk.gray(line));
106
+ }
107
+ }
108
+
109
+ // ─── status ───────────────────────────────────────────────────────────────────
110
+
111
+ /**
112
+ * Renders the service state. Also used after install/uninstall, so the user
113
+ * always sees the resulting state rather than only a success line.
114
+ * @returns {Promise<number>}
115
+ */
116
+ export async function showStatus() {
117
+ const spinner = ora('Checking the service…').start();
118
+ let s;
119
+ try {
120
+ s = await service.status();
121
+ } catch (err) {
122
+ spinner.fail(`Could not read the service state: ${err.message}`);
123
+ return 1;
124
+ }
125
+ spinner.stop();
126
+
127
+ console.log();
128
+ console.log(' ' + chalk.bold('Easy DevOps background service'));
129
+ console.log(' ' + chalk.gray('─'.repeat(58)));
130
+
131
+ if (isDevDataDir()) {
132
+ console.log(' ' + chalk.bgYellow.black(' DEV MODE ') + chalk.yellow(` data ${getDataDir()}`));
133
+ console.log();
134
+ }
135
+
136
+ const modeLabel = s.mode ? ` (${s.mode})` : '';
137
+ console.log(` Mechanism ${MECHANISM_LABELS[s.mechanism] ?? s.mechanism}${chalk.gray(modeLabel)}`);
138
+ console.log(` Installed ${dot(s.service.installed)} ${s.service.installed ? 'yes' : 'no'}`);
139
+
140
+ if (s.mode === service.MODES.PERSONAL) {
141
+ console.log(` Starts ${chalk.gray('at logon (nothing runs before you sign in)')}`);
142
+ } else {
143
+ console.log(` Running ${dot(s.service.running)} ${s.service.running ? 'yes' : `no${s.service.state ? ` (${s.service.state})` : ''}`}`);
144
+ console.log(` Start on boot ${maybeDot(s.service.enabledOnBoot)} ${describeBoot(s.service.enabledOnBoot)}`);
145
+ }
146
+
147
+ // The daemon's own heartbeat, which is the authority on whether background
148
+ // work is actually happening. A service manager reporting "running" is not
149
+ // the same thing.
150
+ //
151
+ // "stopped by you" is its own state, in its own colour: a user who stopped
152
+ // it deliberately must not be told their service crashed.
153
+ const d = s.daemon;
154
+ const daemonLine = d.running
155
+ ? `running (pid ${d.pid}, since ${localTime(d.startedAt)}, up ${humanDuration(d.startedAt ? Date.now() - d.startedAt : null)})`
156
+ : s.stoppedByUser
157
+ ? chalk.yellow('stopped by you') + chalk.gray(s.stoppedAt ? ` at ${localTime(s.stoppedAt)}` : '')
158
+ : `not running ${chalk.gray(`— ${d.detail}`)}`;
159
+
160
+ const daemonDot = d.running ? dot(true) : s.stoppedByUser ? chalk.yellow('●') : dot(false);
161
+ console.log(` Daemon ${daemonDot} ${daemonLine}`);
162
+
163
+ if (s.service.restarts) {
164
+ console.log(` Restarts ${s.service.crashLooping ? chalk.red(s.service.restarts) : s.service.restarts}`);
165
+ }
166
+
167
+ console.log(` Data directory ${chalk.gray(s.dataDir)}${s.serverMode ? chalk.gray(' (server mode)') : ''}`);
168
+ console.log(` Dashboard UI ${chalk.gray(s.dashboardUi ?? 'unknown')}`);
169
+
170
+ // Runtime: the service's pinned Node next to the one in this shell.
171
+ const rt = s.runtime;
172
+ console.log();
173
+ console.log(` System Node ${rt.systemNode.version} ${chalk.gray(rt.systemNode.path)}`);
174
+
175
+ if (rt.pinned && rt.pinned.kind !== 'none') {
176
+ const label = rt.pinned.version ?? chalk.red('not runnable');
177
+ const kind = { symlink: 'link', copy: 'copy', recorded: 'recorded path', 'real file': 'file' }[rt.pinned.kind] ?? rt.pinned.kind;
178
+ console.log(` Service Node ${label} ${chalk.gray(`(${kind}: ${rt.pinned.path})`)}`);
179
+ if (rt.pinned.target && rt.pinned.target !== rt.pinned.path) {
180
+ console.log(` ${chalk.gray(`→ ${rt.pinned.target}`)}`);
181
+ }
182
+ } else {
183
+ console.log(` Service Node ${chalk.gray('not pinned yet')}`);
184
+ }
185
+
186
+ if (rt.manager?.managed) {
187
+ console.log(` Version manager${chalk.gray(` ${rt.manager.manager} — the service is pinned so switching versions cannot break it`)}`);
188
+ }
189
+
190
+ printWarnings(s.warnings);
191
+
192
+ if (!s.service.installed) {
193
+ console.log();
194
+ console.log(' ' + chalk.gray('Install it with: ') + chalk.white('ezz service install'));
195
+ }
196
+
197
+ console.log();
198
+ return 0;
199
+ }
200
+
201
+ function describeBoot(enabled) {
202
+ if (enabled === null || enabled === undefined) return 'unknown';
203
+ return enabled ? 'yes' : 'no';
204
+ }
205
+
206
+ // ─── install ──────────────────────────────────────────────────────────────────
207
+
208
+ async function doInstall() {
209
+ console.log();
210
+ console.log(' ' + chalk.bold('Install the Easy DevOps background service'));
211
+ console.log(' ' + chalk.gray('─'.repeat(58)));
212
+
213
+ if (isDevDataDir()) {
214
+ console.log(' ' + chalk.bgYellow.black(' DEV MODE ') + chalk.yellow(` data ${getDataDir()}`));
215
+ }
216
+ console.log();
217
+
218
+ // ── Windows: Server or Personal ──────────────────────────────────────────
219
+ let mode = null;
220
+
221
+ if (isWindows) {
222
+ const suggested = await service.suggestWindowsMode();
223
+
224
+ console.log(' ' + chalk.gray(`Detected: ${suggested.detail}`));
225
+ console.log();
226
+
227
+ if (suggested.isServerEdition) {
228
+ // On a server the answer is clear, so it is stated rather than asked:
229
+ // the machine exists to serve and nobody is logged in most of the time.
230
+ console.log(' ' + chalk.white('This is Windows Server, so Server mode is the right choice.'));
231
+ console.log(' ' + chalk.gray('It runs as a real Windows Service, starts before anyone logs in,'));
232
+ console.log(' ' + chalk.gray('and needs an Administrator terminal once. You will NOT be asked'));
233
+ console.log(' ' + chalk.gray('for a password: the service uses a Windows-managed account.'));
234
+ console.log();
235
+
236
+ const { confirmServer } = await inquirer.prompt([{
237
+ type: 'confirm',
238
+ name: 'confirmServer',
239
+ message: 'Install in Server mode?',
240
+ default: true,
241
+ }]);
242
+ mode = confirmServer ? service.MODES.SERVER : service.MODES.PERSONAL;
243
+ } else {
244
+ const { chosen } = await inquirer.prompt([{
245
+ type: 'list',
246
+ name: 'chosen',
247
+ message: 'How should the background service run?',
248
+ default: service.MODES.PERSONAL,
249
+ choices: [
250
+ {
251
+ name: 'Personal — starts when you log in. No administrator rights needed.',
252
+ value: service.MODES.PERSONAL,
253
+ },
254
+ {
255
+ name: 'Server — a real Windows Service, starts at boot. Needs an Administrator terminal once.',
256
+ value: service.MODES.SERVER,
257
+ },
258
+ ],
259
+ }]);
260
+ mode = chosen;
261
+ }
262
+
263
+ if (mode === service.MODES.SERVER) {
264
+ console.log();
265
+ console.log(' ' + chalk.yellow('Server mode will move your data to C:\\ProgramData\\easy-devops'));
266
+ console.log(' ' + chalk.gray('so the service account and your CLI can share one database.'));
267
+ console.log(' ' + chalk.gray('It is copied, verified, and the original is kept as a backup.'));
268
+ console.log();
269
+
270
+ const { confirmMove } = await inquirer.prompt([{
271
+ type: 'confirm',
272
+ name: 'confirmMove',
273
+ message: 'Continue?',
274
+ default: true,
275
+ }]);
276
+ if (!confirmMove) {
277
+ console.log(' ' + chalk.gray('Nothing was changed.'));
278
+ return 0;
279
+ }
280
+ }
281
+ }
282
+
283
+ // ── The old PM2 setup ────────────────────────────────────────────────────
284
+ let removeLegacy = false;
285
+ const found = await service.legacy.detectLegacy();
286
+
287
+ if (found.found) {
288
+ console.log();
289
+ console.log(describeLegacy(found.items).split('\n').map((l) => ` ${chalk.yellow(l)}`).join('\n'));
290
+ console.log();
291
+
292
+ const { cleanup } = await inquirer.prompt([{
293
+ type: 'confirm',
294
+ name: 'cleanup',
295
+ message: 'Remove the old PM2-based setup?',
296
+ default: true,
297
+ }]);
298
+ removeLegacy = cleanup;
299
+ }
300
+
301
+ // ── Install ──────────────────────────────────────────────────────────────
302
+ console.log();
303
+ const spinner = ora('Installing…').start();
304
+ const lines = [];
305
+
306
+ let result;
307
+ try {
308
+ result = await service.install({
309
+ mode,
310
+ removeLegacy,
311
+ log: (line) => { lines.push(line); spinner.text = line; },
312
+ });
313
+ } catch (err) {
314
+ spinner.fail(`Install failed: ${err.message}`);
315
+ return 1;
316
+ }
317
+
318
+ spinner.stop();
319
+ for (const line of lines) console.log(' ' + chalk.gray(line));
320
+
321
+ if (!result.ok) {
322
+ console.log();
323
+
324
+ if (result.manual) {
325
+ // No systemd: not a failure, just a different answer.
326
+ console.log(' ' + chalk.yellow(result.reason));
327
+ console.log();
328
+ console.log(result.manual.split('\n').map((l) => ` ${l}`).join('\n'));
329
+ console.log();
330
+ return 0;
331
+ }
332
+
333
+ console.log(' ' + chalk.red('Install failed.'));
334
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
335
+
336
+ if (result.needsElevation) {
337
+ console.log();
338
+ console.log(' ' + chalk.gray('No password is needed — only elevation.'));
339
+ }
340
+ console.log();
341
+ return 1;
342
+ }
343
+
344
+ console.log();
345
+ console.log(' ' + chalk.green('✓ Installed.'));
346
+
347
+ if (result.migrated) {
348
+ console.log(' ' + chalk.gray(`Data moved to ${result.dataDir}`));
349
+ if (result.backup) console.log(' ' + chalk.gray(`Backup kept at ${result.backup}`));
350
+ }
351
+
352
+ if (result.legacy?.removed?.length) {
353
+ console.log(' ' + chalk.gray(`Removed from the old setup: ${result.legacy.removed.join(', ')}`));
354
+ }
355
+ for (const failure of result.legacy?.failed ?? []) {
356
+ console.log(' ' + chalk.yellow(`⚠ could not remove ${failure.name}: ${failure.reason}`));
357
+ }
358
+ for (const failure of result.aclFailures ?? []) {
359
+ console.log(' ' + chalk.yellow(`⚠ ${failure.path}: ${failure.reason}`));
360
+ }
361
+
362
+ if (result.mode === service.MODES.PERSONAL) {
363
+ console.log();
364
+ console.log(' ' + chalk.white('It will start automatically the next time you log in.'));
365
+ console.log(' ' + chalk.gray(`Supervisor log: ${result.logFile}`));
366
+ console.log();
367
+
368
+ // Offer to start it now, through exactly the same path as
369
+ // `ezz service start`. The old message suggested `ezz daemon` instead,
370
+ // which is wrong for this purpose: that runs a daemon in the foreground
371
+ // tied to the terminal, so closing the window stops it. The supervisor is
372
+ // what should own it.
373
+ const started = await offerToStart();
374
+ if (started !== null) return started;
375
+
376
+ return 0;
377
+ }
378
+
379
+ if (result.daemon?.pid) {
380
+ console.log(' ' + chalk.gray(`Daemon reported in after ${Math.round(result.daemon.waitedMs / 1000)}s (pid ${result.daemon.pid})`));
381
+ }
382
+
383
+ console.log();
384
+ return showStatus();
385
+ }
386
+
387
+ /**
388
+ * Asks whether to start the service now, and does it the same way
389
+ * `ezz service start` does.
390
+ *
391
+ * @returns {Promise<number|null>} An exit code, or null when not started
392
+ */
393
+ async function offerToStart() {
394
+ // Already up — a Server-mode install starts the service itself, and so does a
395
+ // repair. Nothing to offer.
396
+ if (service.backgroundServiceState().running) return null;
397
+
398
+ let answer;
399
+ try {
400
+ ({ answer } = await inquirer.prompt([{
401
+ type: 'confirm',
402
+ name: 'answer',
403
+ message: 'Start it now?',
404
+ default: true,
405
+ }]));
406
+ } catch (err) {
407
+ // Ctrl+C at the prompt is not a failure: the install succeeded.
408
+ if (err.name === 'ExitPromptError') return null;
409
+ throw err;
410
+ }
411
+
412
+ if (!answer) {
413
+ console.log(' ' + chalk.gray('Not started. Start it any time with: ') + chalk.white('ezz service start'));
414
+ console.log();
415
+ return null;
416
+ }
417
+
418
+ return doStart();
419
+ }
420
+
421
+ // ─── uninstall ────────────────────────────────────────────────────────────────
422
+
423
+ async function doUninstall() {
424
+ const current = await service.status().catch(() => null);
425
+
426
+ console.log();
427
+ console.log(' ' + chalk.bold('Remove the Easy DevOps background service'));
428
+ console.log(' ' + chalk.gray('─'.repeat(58)));
429
+ console.log();
430
+
431
+ // Nothing to remove: say so and stop, rather than asking the user to confirm
432
+ // removing something that does not exist and then reporting success. The one
433
+ // thing still worth offering is cleaning up the OLD PM2-based setup, and only
434
+ // when some of it is actually there.
435
+ if (current && !current.service.installed) {
436
+ console.log(' ' + chalk.white('No background service is installed.'));
437
+ console.log(' ' + chalk.gray('Nothing to remove.'));
438
+
439
+ const leftovers = await service.legacy.detectLegacy();
440
+ if (!leftovers.found) {
441
+ console.log();
442
+ console.log(' ' + chalk.gray('Install one with: ') + chalk.white('ezz service install'));
443
+ console.log();
444
+ return 0;
445
+ }
446
+
447
+ console.log();
448
+ console.log(describeLegacy(leftovers.items).split('\n').map((l) => ` ${chalk.yellow(l)}`).join('\n'));
449
+ console.log();
450
+
451
+ const { cleanupOnly } = await inquirer.prompt([{
452
+ type: 'confirm',
453
+ name: 'cleanupOnly',
454
+ message: 'Remove the old PM2-based setup?',
455
+ default: true,
456
+ }]);
457
+
458
+ if (!cleanupOnly) {
459
+ console.log(' ' + chalk.gray('Nothing was changed.'));
460
+ return 0;
461
+ }
462
+
463
+ const spinner = ora('Removing the old setup...').start();
464
+ const cleaned = await service.legacy.removeLegacy({
465
+ items: leftovers.items,
466
+ log: (line) => { spinner.text = line; },
467
+ });
468
+ spinner.stop();
469
+
470
+ if (cleaned.removed.length) {
471
+ console.log(' ' + chalk.green(`✓ Removed: ${cleaned.removed.join(', ')}`));
472
+ }
473
+ for (const failure of cleaned.failed) {
474
+ console.log(' ' + chalk.yellow(`⚠ could not remove ${failure.name}: ${failure.reason}`));
475
+ }
476
+ console.log();
477
+ return cleaned.ok ? 0 : 1;
478
+ }
479
+
480
+ const { confirm } = await inquirer.prompt([{
481
+ type: 'confirm',
482
+ name: 'confirm',
483
+ message: 'Remove the background service? Monitoring will then only run while a dashboard is open.',
484
+ default: false,
485
+ }]);
486
+
487
+ if (!confirm) {
488
+ console.log(' ' + chalk.gray('Nothing was changed.'));
489
+ return 0;
490
+ }
491
+
492
+ // ── Decision 5: the data location is an explicit question ────────────────
493
+ //
494
+ // Uninstalling must never silently move the data back to %APPDATA%. While
495
+ // the marker exists, ProgramData holds the live data; relocating it without
496
+ // asking would be a surprise, and the per-user copy is the stale one.
497
+ let keepServerDataLocation = true;
498
+
499
+ if (current?.serverMode) {
500
+ console.log();
501
+ console.log(' ' + chalk.white(`Your data currently lives at ${current.dataDir}`));
502
+ console.log();
503
+
504
+ const { dataChoice } = await inquirer.prompt([{
505
+ type: 'list',
506
+ name: 'dataChoice',
507
+ message: 'What should happen to it?',
508
+ default: 'keep',
509
+ choices: [
510
+ {
511
+ name: `Keep it where it is (recommended) — the CLI and dashboard keep using ${current.dataDir}`,
512
+ value: 'keep',
513
+ },
514
+ {
515
+ name: 'Move it back to %APPDATA%\\easy-devops — copied, verified, with a backup kept',
516
+ value: 'move',
517
+ },
518
+ ],
519
+ }]);
520
+ keepServerDataLocation = dataChoice === 'keep';
521
+ }
522
+
523
+ // ── The old PM2 setup ────────────────────────────────────────────────────
524
+ let removeLegacy = false;
525
+ const found = await service.legacy.detectLegacy();
526
+
527
+ if (found.found) {
528
+ console.log();
529
+ console.log(describeLegacy(found.items).split('\n').map((l) => ` ${chalk.yellow(l)}`).join('\n'));
530
+ console.log();
531
+
532
+ const { cleanup } = await inquirer.prompt([{
533
+ type: 'confirm',
534
+ name: 'cleanup',
535
+ message: 'Remove the old PM2-based setup as well?',
536
+ default: true,
537
+ }]);
538
+ removeLegacy = cleanup;
539
+ }
540
+
541
+ console.log();
542
+ const spinner = ora('Removing…').start();
543
+ const lines = [];
544
+
545
+ let result;
546
+ try {
547
+ result = await service.uninstall({
548
+ keepServerDataLocation,
549
+ removeLegacy,
550
+ log: (line) => { lines.push(line); spinner.text = line; },
551
+ });
552
+ } catch (err) {
553
+ spinner.fail(`Uninstall failed: ${err.message}`);
554
+ return 1;
555
+ }
556
+
557
+ spinner.stop();
558
+ for (const line of lines) console.log(' ' + chalk.gray(line));
559
+
560
+ if (!result.ok) {
561
+ console.log();
562
+ console.log(' ' + chalk.red('Uninstall failed.'));
563
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
564
+ console.log();
565
+ return 1;
566
+ }
567
+
568
+ console.log();
569
+ console.log(' ' + chalk.green('✓ Removed.'));
570
+ for (const note of result.notes ?? []) {
571
+ console.log((' ' + chalk.gray(note)).replace(/\n/g, '\n '));
572
+ }
573
+ if (result.movedData) {
574
+ console.log(' ' + chalk.gray(`Data moved back to ${result.dataDir}`));
575
+ if (result.backup) console.log(' ' + chalk.gray(`Backup kept at ${result.backup}`));
576
+ }
577
+ if (result.legacy?.removed?.length) {
578
+ console.log(' ' + chalk.gray(`Removed from the old setup: ${result.legacy.removed.join(', ')}`));
579
+ }
580
+
581
+ console.log();
582
+ console.log(' ' + chalk.gray('Monitoring and notifications now run only while a dashboard is open.'));
583
+ console.log();
584
+ return 0;
585
+ }
586
+
587
+ // ─── stop / start ─────────────────────────────────────────────────────────────
588
+
589
+ async function doStop() {
590
+ const spinner = ora('Stopping the background service…').start();
591
+ const lines = [];
592
+
593
+ let result;
594
+ try {
595
+ result = await service.stop({ log: (line) => { lines.push(line); spinner.text = line; } });
596
+ } catch (err) {
597
+ spinner.fail(`Stop failed: ${err.message}`);
598
+ return 1;
599
+ }
600
+
601
+ spinner.stop();
602
+ for (const line of lines) console.log(' ' + chalk.gray(line));
603
+
604
+ if (!result.ok) {
605
+ console.log();
606
+ console.log(' ' + chalk.red('Could not stop it.'));
607
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
608
+ console.log();
609
+ return 1;
610
+ }
611
+
612
+ console.log();
613
+ console.log(' ' + chalk.green(result.alreadyStopped ? '✓ Already stopped.' : '✓ Stopped.'));
614
+ if (result.detail) console.log(' ' + chalk.gray(result.detail));
615
+
616
+ // The difference from uninstall is the thing a user most needs to hear.
617
+ console.log();
618
+ console.log(' ' + chalk.gray('This is temporary. It starts again '
619
+ + (result.mode === service.MODES.PERSONAL ? 'at your next logon' : 'at the next boot') + ','));
620
+ console.log(' ' + chalk.gray('or right now with: ') + chalk.white('ezz service start'));
621
+ console.log(' ' + chalk.gray('To remove it permanently: ') + chalk.white('ezz service uninstall'));
622
+ console.log();
623
+ console.log(' ' + chalk.yellow('Monitoring and notifications are not running while it is stopped.'));
624
+ console.log();
625
+ return 0;
626
+ }
627
+
628
+ async function doStart() {
629
+ const spinner = ora('Starting the background service…').start();
630
+ const lines = [];
631
+
632
+ let result;
633
+ try {
634
+ result = await service.start({ log: (line) => { lines.push(line); spinner.text = line; } });
635
+ } catch (err) {
636
+ spinner.fail(`Start failed: ${err.message}`);
637
+ return 1;
638
+ }
639
+
640
+ spinner.stop();
641
+ for (const line of lines) console.log(' ' + chalk.gray(line));
642
+
643
+ if (!result.ok) {
644
+ console.log();
645
+ if (result.alreadyRunning) {
646
+ console.log(' ' + chalk.yellow('Already running.'));
647
+ console.log(' ' + chalk.gray(result.reason));
648
+ console.log();
649
+ return 0;
650
+ }
651
+ console.log(' ' + chalk.red('Could not start it.'));
652
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
653
+ if (result.needsElevation) {
654
+ console.log();
655
+ console.log(' ' + chalk.gray('No password is needed — only elevation.'));
656
+ }
657
+ console.log();
658
+ return 1;
659
+ }
660
+
661
+ console.log();
662
+ console.log(' ' + chalk.green(`✓ Started (pid ${result.pid}, reported in after ${Math.round(result.waitedMs / 1000)}s).`));
663
+ console.log();
664
+ return 0;
665
+ }
666
+
667
+ // ─── restart ──────────────────────────────────────────────────────────────────
668
+
669
+ async function doRestart() {
670
+ const spinner = ora('Restarting the service…').start();
671
+
672
+ let result;
673
+ try {
674
+ result = await service.restart({ log: (line) => { spinner.text = line; } });
675
+ } catch (err) {
676
+ spinner.fail(`Restart failed: ${err.message}`);
677
+ return 1;
678
+ }
679
+
680
+ if (!result.ok) {
681
+ spinner.stop();
682
+ console.log();
683
+ console.log(' ' + chalk.red('Restart failed.'));
684
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
685
+ console.log();
686
+ return 1;
687
+ }
688
+
689
+ spinner.succeed(`Restarted (pid ${result.pid}, reported in after ${Math.round(result.waitedMs / 1000)}s)`);
690
+ console.log();
691
+ return 0;
692
+ }
693
+
694
+ // ─── logs ─────────────────────────────────────────────────────────────────────
695
+
696
+ async function showLogs() {
697
+ const spinner = ora('Reading the service log…').start();
698
+
699
+ let result;
700
+ try {
701
+ result = await service.logs({ lines: 80 });
702
+ } catch (err) {
703
+ spinner.fail(`Could not read the log: ${err.message}`);
704
+ return 1;
705
+ }
706
+ spinner.stop();
707
+
708
+ console.log();
709
+ console.log(' ' + chalk.bold('Service log') + chalk.gray(` ${result.source}`));
710
+ console.log(' ' + chalk.gray('─'.repeat(58)));
711
+
712
+ if (!result.output?.trim()) {
713
+ console.log(' ' + chalk.gray('(empty)'));
714
+ } else {
715
+ for (const line of result.output.split('\n')) console.log(' ' + line);
716
+ }
717
+
718
+ console.log();
719
+ return result.ok ? 0 : 1;
720
+ }
721
+
722
+ // ─── repair ───────────────────────────────────────────────────────────────────
723
+
724
+ async function doRepair() {
725
+ console.log();
726
+ console.log(' ' + chalk.bold('Repair the Easy DevOps service'));
727
+ console.log(' ' + chalk.gray('─'.repeat(58)));
728
+ console.log(' ' + chalk.gray('Re-pins the Node runtime, re-applies permissions, and restarts it.'));
729
+ console.log();
730
+
731
+ const spinner = ora('Repairing…').start();
732
+ const lines = [];
733
+
734
+ let result;
735
+ try {
736
+ result = await service.repair({ log: (line) => { lines.push(line); spinner.text = line; } });
737
+ } catch (err) {
738
+ spinner.fail(`Repair failed: ${err.message}`);
739
+ return 1;
740
+ }
741
+
742
+ spinner.stop();
743
+ for (const line of lines) console.log(' ' + chalk.gray(line));
744
+
745
+ if (!result.ok) {
746
+ console.log();
747
+ console.log(' ' + chalk.red('Repair failed.'));
748
+ console.log(String(result.reason).split('\n').map((l) => ` ${l}`).join('\n'));
749
+ if (result.needsElevation) {
750
+ console.log();
751
+ console.log(' ' + chalk.gray('No password is needed — only elevation.'));
752
+ }
753
+ console.log();
754
+ return 1;
755
+ }
756
+
757
+ console.log();
758
+ console.log(' ' + chalk.green('✓ Repaired.'));
759
+ for (const failure of result.aclFailures ?? []) {
760
+ console.log(' ' + chalk.yellow(`⚠ ${failure.path}: ${failure.reason}`));
761
+ }
762
+ console.log();
763
+ return showStatus();
764
+ }