easy-devops 0.2.2 → 0.2.4

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.
@@ -247,7 +247,7 @@ async function installNginx() {
247
247
  if (hasWinget) {
248
248
  spinner.text = 'Installing nginx via winget…';
249
249
  const result = await run(
250
- 'winget install -e --id Nginx.Nginx --accept-package-agreements --accept-source-agreements',
250
+ 'winget install -e --id nginxinc.nginx --accept-package-agreements --accept-source-agreements',
251
251
  { timeout: 120000 },
252
252
  );
253
253
  if (result.success) {
@@ -4,31 +4,37 @@
4
4
  * Check for updates and upgrade easy-devops in place.
5
5
  *
6
6
  * Flow:
7
- * 1. Fetch latest version from npm registry
8
- * 2. If an update is available, offer to install it
9
- * 3. Before installing: record dashboard running state in DB
10
- * (key: 'update-pre-dashboard') so it survives a crash mid-update
11
- * 4. Stop dashboard if it was running
12
- * 5. Run: npm install -g easy-devops@<latest>
13
- * 6. Re-start dashboard if it was running before
14
- * 7. Clear the saved state key
7
+ * 1. Fetch latest version from npm registry
8
+ * 2. If an update is available, offer to install it
9
+ * 3. Before installing: record dashboard running state in DB
10
+ * (key: 'update-pre-dashboard') so it survives a crash mid-update
11
+ * 4. Stop dashboard if it was running
12
+ * 5. Close database connection
13
+ * 6. Spawn an external update script that runs npm install
14
+ * after this process exits (to avoid EBUSY on Windows)
15
+ * 7. This process exits; update script runs
16
+ * 8. On next launch, recoverIfNeeded restarts dashboard if needed
15
17
  */
16
18
 
17
19
  import chalk from 'chalk';
18
20
  import inquirer from 'inquirer';
19
21
  import ora from 'ora';
20
22
  import path from 'path';
23
+ import fs from 'fs';
21
24
  import { fileURLToPath } from 'url';
22
25
  import { createRequire } from 'module';
26
+ import { spawn } from 'child_process';
23
27
  import { run } from '../../core/shell.js';
24
- import { dbGet, dbSet, closeDb, initDb } from '../../core/db.js';
28
+ import { dbGet, dbSet, closeDb } from '../../core/db.js';
25
29
  import { loadConfig } from '../../core/config.js';
26
- import { getDashboardStatus, startDashboard, stopDashboard } from './dashboard.js';
30
+ import { getDashboardStatus, stopDashboard } from './dashboard.js';
27
31
 
28
32
  const __dirname = path.dirname(fileURLToPath(import.meta.url));
29
- const require = createRequire(import.meta.url);
33
+ const require = createRequire(import.meta.url);
30
34
  const { version: currentVersion } = require('../../package.json');
31
35
 
36
+ const isWindows = process.platform === 'win32';
37
+
32
38
  // ─── Version helpers ──────────────────────────────────────────────────────────
33
39
 
34
40
  async function fetchLatestVersion() {
@@ -54,18 +60,20 @@ async function recoverIfNeeded() {
54
60
  const saved = dbGet('update-pre-dashboard');
55
61
  if (!saved?.wasRunning) return;
56
62
 
57
- console.log(chalk.yellow('\n A previous update left the dashboard stopped.'));
63
+ console.log(chalk.yellow('\n A previous update left the dashboard stopped.'));
58
64
  const { restart } = await inquirer.prompt([{
59
- type: 'confirm',
60
- name: 'restart',
65
+ type: 'confirm',
66
+ name: 'restart',
61
67
  message: 'Restart the dashboard now?',
62
68
  default: true,
63
69
  }]);
64
70
 
65
71
  if (restart) {
72
+ // Dynamic import to avoid circular dependency and ensure fresh state
73
+ const { startDashboard } = await import('./dashboard.js');
66
74
  const port = saved.port || loadConfig().dashboardPort;
67
- const sp = ora(`Starting dashboard on port ${port}...`).start();
68
- const res = await startDashboard(port);
75
+ const sp = ora(`Starting dashboard on port ${port}...`).start();
76
+ const res = await startDashboard(port);
69
77
  res.success
70
78
  ? sp.succeed(`Dashboard restarted on port ${port}`)
71
79
  : sp.fail('Could not restart dashboard — use the Dashboard menu');
@@ -76,54 +84,141 @@ async function recoverIfNeeded() {
76
84
 
77
85
  // ─── Perform update ───────────────────────────────────────────────────────────
78
86
 
87
+ /**
88
+ * Creates and spawns an external update script that runs after this process exits.
89
+ * This is necessary on Windows because better-sqlite3's native module stays locked
90
+ * as long as any Node.js process has the database open.
91
+ */
79
92
  async function performUpdate(latestVersion) {
80
93
  // Step 1 — snapshot dashboard state and persist it
81
94
  const status = await getDashboardStatus();
82
95
  dbSet('update-pre-dashboard', {
83
96
  wasRunning: status.running,
84
- pid: status.pid,
85
- port: status.port,
97
+ pid: status.pid,
98
+ port: status.port,
86
99
  });
87
100
 
88
- // Step 2 — stop dashboard if running
101
+ // Step 2 — stop dashboard if running (it has its own DB connection)
89
102
  if (status.running) {
90
103
  const sp = ora('Stopping dashboard...').start();
91
104
  await stopDashboard(status.pid);
92
105
  sp.succeed('Dashboard stopped');
106
+ // Give the dashboard process a moment to fully terminate
107
+ await new Promise(r => setTimeout(r, 2000));
93
108
  }
94
109
 
95
- // Step 3 — close the SQLite connection so npm can rename the db file (EBUSY on Windows)
110
+ // Step 3 — close this process's database connection
96
111
  closeDb();
97
112
 
98
- // Step 4 — install new version
99
- const sp = ora(`Installing easy-devops@${latestVersion}...`).start();
100
- const result = await run(`npm install -g easy-devops@${latestVersion}`, { timeout: 120000 });
113
+ // Step 4 — create an external update script and spawn it
114
+ // The script will run npm install after this process exits
115
+ const updateScriptPS = `
116
+ $ProgressPreference = 'SilentlyContinue'
117
+ Write-Host ""
118
+ Write-Host "Installing easy-devops@${latestVersion}..." -ForegroundColor Cyan
119
+ Write-Host ""
120
+ npm install -g easy-devops@${latestVersion}
121
+ if ($LASTEXITCODE -eq 0) {
122
+ Write-Host ""
123
+ Write-Host "Successfully updated to v${latestVersion}" -ForegroundColor Green
124
+ Write-Host "Run 'easy-devops' to start the new version." -ForegroundColor Gray
125
+ } else {
126
+ Write-Host ""
127
+ Write-Host "Update failed. Please try again or update manually:" -ForegroundColor Red
128
+ Write-Host " npm install -g easy-devops@${latestVersion}" -ForegroundColor Yellow
129
+ }
130
+ Write-Host ""
131
+ Write-Host "Press any key to close this window..." -ForegroundColor Gray
132
+ $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
133
+ `;
101
134
 
102
- if (!result.success) {
103
- sp.fail('Update failed');
104
- console.log(chalk.red('\n' + (result.stderr || result.stdout) + '\n'));
105
- // Leave 'update-pre-dashboard' in DB so recovery runs on next launch
106
- return false;
107
- }
135
+ const updateScriptBash = `#!/bin/bash
136
+ echo ""
137
+ echo -e "\\033[36mInstalling easy-devops@${latestVersion}...\\033[0m"
138
+ echo ""
139
+ npm install -g easy-devops@${latestVersion}
140
+ if [ $? -eq 0 ]; then
141
+ echo ""
142
+ echo -e "\\033[32mSuccessfully updated to v${latestVersion}\\033[0m"
143
+ echo -e "\\033[90mRun 'easy-devops' to start the new version.\\033[0m"
144
+ else
145
+ echo ""
146
+ echo -e "\\033[31mUpdate failed. Please try again or update manually:\\033[0m"
147
+ echo -e "\\033[33m npm install -g easy-devops@${latestVersion}\\033[0m"
148
+ fi
149
+ echo ""
150
+ read -p "Press Enter to close this window..."
151
+ `;
108
152
 
109
- sp.succeed(`Updated to v${latestVersion}`);
153
+ console.log(chalk.cyan('\n Starting external update process...'));
154
+ console.log(chalk.gray(' A new window will open to complete the update.'));
155
+ console.log(chalk.gray(' This window will close after the update starts.\n'));
110
156
 
111
- // Re-initialize the database connection after npm replaced the module
112
- initDb();
157
+ if (isWindows) {
158
+ // Write script to temp file and run in new PowerShell window
159
+ const tempDir = process.env.TEMP || 'C:\\Windows\\Temp';
160
+ const tempScript = path.join(tempDir, 'easy-devops-update.ps1');
161
+ fs.writeFileSync(tempScript, updateScriptPS, 'utf8');
113
162
 
114
- // Step 5 restart dashboard if it was running before
115
- const saved = dbGet('update-pre-dashboard');
116
- dbSet('update-pre-dashboard', null);
163
+ // Run in a new window, this process will exit
164
+ spawn('powershell.exe', [
165
+ '-NoExit',
166
+ '-ExecutionPolicy', 'Bypass',
167
+ '-File', tempScript
168
+ ], {
169
+ detached: true,
170
+ stdio: 'ignore',
171
+ windowsHide: false
172
+ }).unref();
117
173
 
118
- if (saved?.wasRunning) {
119
- const port = saved.port || loadConfig().dashboardPort;
120
- const restSp = ora(`Restarting dashboard on port ${port}...`).start();
121
- const res = await startDashboard(port);
122
- res.success
123
- ? restSp.succeed(`Dashboard restarted on port ${port}`)
124
- : restSp.fail('Could not restart dashboard — use the Dashboard menu');
174
+ // Clean up hint
175
+ console.log(chalk.gray(` Update script: ${tempScript}`));
176
+ } else {
177
+ // On Linux/Mac, run in a new terminal window if possible
178
+ const tempScript = '/tmp/easy-devops-update.sh';
179
+ fs.writeFileSync(tempScript, updateScriptBash, 'utf8');
180
+ fs.chmodSync(tempScript, '755');
181
+
182
+ // Try common terminal emulators
183
+ const terminals = [
184
+ ['gnome-terminal', '--', 'bash', '-c', updateScriptBash],
185
+ ['xterm', '-e', 'bash', '-c', updateScriptBash],
186
+ ['konsole', '-e', 'bash', '-c', updateScriptBash],
187
+ ];
188
+ let launched = false;
189
+
190
+ for (const [cmd, ...args] of terminals) {
191
+ try {
192
+ spawn(cmd, args, {
193
+ detached: true,
194
+ stdio: 'ignore'
195
+ }).unref();
196
+ launched = true;
197
+ break;
198
+ } catch { /* try next */ }
199
+ }
200
+
201
+ if (!launched) {
202
+ // Fallback: just run npm directly in this terminal
203
+ console.log(chalk.yellow('\nRunning update in this window...'));
204
+ const result = await run(`npm install -g easy-devops@${latestVersion}`, { timeout: 120000 });
205
+ if (result.success) {
206
+ console.log(chalk.green(`\n Successfully updated to v${latestVersion}`));
207
+ } else {
208
+ console.log(chalk.red('\n Update failed:'));
209
+ console.log(result.stderr || result.stdout);
210
+ }
211
+ return result.success;
212
+ }
125
213
  }
126
214
 
215
+ // Give a moment for the external process to start
216
+ await new Promise(r => setTimeout(r, 1000));
217
+
218
+ console.log(chalk.green('\n Update process launched.'));
219
+ console.log(chalk.gray(' Complete the update in the new window, then run: easy-devops'));
220
+ console.log(chalk.gray(' If the dashboard was running, it will be restarted on next launch.\n'));
221
+
127
222
  return true;
128
223
  }
129
224
 
@@ -137,12 +232,12 @@ export default async function updateMenu() {
137
232
  const latestVersion = await fetchLatestVersion();
138
233
  spinner.stop();
139
234
 
140
- console.log(chalk.bold('\n Check for Updates'));
141
- console.log(chalk.gray(' ' + '─'.repeat(40)));
142
- console.log(` Current version : ${chalk.cyan('v' + currentVersion)}`);
235
+ console.log(chalk.bold('\n Check for Updates'));
236
+ console.log(chalk.gray(' ' + '─'.repeat(40)));
237
+ console.log(` Current version : ${chalk.cyan('v' + currentVersion)}`);
143
238
 
144
239
  if (!latestVersion) {
145
- console.log(chalk.yellow(' Could not reach npm registry. Check your internet connection.\n'));
240
+ console.log(chalk.yellow(' Could not reach npm registry. Check your internet connection.\n'));
146
241
  await inquirer.prompt([{ type: 'input', name: '_', message: 'Press Enter to go back...' }]);
147
242
  return;
148
243
  }
@@ -150,9 +245,9 @@ export default async function updateMenu() {
150
245
  const updateAvailable = isNewer(latestVersion, currentVersion);
151
246
 
152
247
  if (updateAvailable) {
153
- console.log(` Latest version : ${chalk.green('v' + latestVersion)} ${chalk.yellow('← update available')}\n`);
248
+ console.log(` Latest version : ${chalk.green('v' + latestVersion)} ${chalk.yellow('← update available')}\n`);
154
249
  } else {
155
- console.log(` Latest version : ${chalk.green('v' + latestVersion)} ${chalk.gray('✓ up to date')}\n`);
250
+ console.log(` Latest version : ${chalk.green('v' + latestVersion)} ${chalk.gray('✓ up to date')}\n`);
156
251
  }
157
252
 
158
253
  const choices = updateAvailable
@@ -162,8 +257,8 @@ export default async function updateMenu() {
162
257
  let choice;
163
258
  try {
164
259
  ({ choice } = await inquirer.prompt([{
165
- type: 'list',
166
- name: 'choice',
260
+ type: 'list',
261
+ name: 'choice',
167
262
  message: 'Select an option:',
168
263
  choices,
169
264
  }]));
@@ -173,12 +268,9 @@ export default async function updateMenu() {
173
268
  }
174
269
 
175
270
  if (choice === `Update to v${latestVersion}`) {
176
- const success = await performUpdate(latestVersion);
177
- if (success) {
178
- console.log(chalk.gray('\n Restart easy-devops to use the new version.\n'));
179
- }
271
+ await performUpdate(latestVersion);
180
272
  try {
181
- await inquirer.prompt([{ type: 'input', name: '_', message: 'Press Enter to continue...' }]);
273
+ await inquirer.prompt([{ type: 'input', name: '_', message: 'Press Enter to exit...' }]);
182
274
  } catch { /* ExitPromptError */ }
183
275
  }
184
276
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "easy-devops",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "description": "A unified DevOps management tool with CLI and web dashboard for managing Nginx, SSL certificates, and Node.js on Linux and Windows servers.",
5
5
  "keywords": [
6
6
  "devops",