@lethevimlet/sshift 1.0.1 → 1.0.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lethevimlet/sshift",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Web-based SSH + SFTP terminal client with tabs and bookmarks",
5
5
  "main": "src/server/index.js",
6
6
  "bin": {
@@ -9,7 +9,6 @@ const { getDataDir } = require('../../utils/config');
9
9
  const SSL_CERT_FILE = 'ssl-cert.pem';
10
10
  const SSL_KEY_FILE = 'ssl-key.pem';
11
11
  const UPDATE_MARKER_FILE = '.updating';
12
- const RESTART_MARKER_FILE = '.restart-after-update';
13
12
 
14
13
  /**
15
14
  * Register system endpoints
@@ -17,21 +16,24 @@ const RESTART_MARKER_FILE = '.restart-after-update';
17
16
  * @param {Object} io - Socket.IO instance
18
17
  */
19
18
  function registerSystemEndpoints(app, io) {
20
- // Clean up stale update marker files from previous update attempts
19
+ // Clean up stale update marker from previous update attempt
21
20
  const dataDir = getDataDir();
22
21
  const staleUpdateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
23
- const staleRestartMarker = path.join(dataDir, RESTART_MARKER_FILE);
22
+ const staleUpdateScript = path.join(dataDir, '.sshift-update.sh');
23
+ const stalePsScript = path.join(dataDir, '.sshift-update.ps1');
24
24
  try {
25
25
  if (fs.existsSync(staleUpdateMarker)) {
26
26
  fs.unlinkSync(staleUpdateMarker);
27
27
  console.log('[UPDATE] Cleaned up stale update marker');
28
28
  }
29
- if (fs.existsSync(staleRestartMarker)) {
30
- fs.unlinkSync(staleRestartMarker);
31
- console.log('[UPDATE] Cleaned up stale restart marker');
29
+ if (fs.existsSync(staleUpdateScript)) {
30
+ fs.unlinkSync(staleUpdateScript);
31
+ }
32
+ if (fs.existsSync(stalePsScript)) {
33
+ fs.unlinkSync(stalePsScript);
32
34
  }
33
35
  } catch (e) {
34
- console.error('[UPDATE] Failed to clean up marker files:', e.message);
36
+ console.error('[UPDATE] Failed to clean up marker file:', e.message);
35
37
  }
36
38
 
37
39
  // API: Get version
@@ -131,7 +133,6 @@ function registerSystemEndpoints(app, io) {
131
133
  app.get('/api/update-status', (req, res) => {
132
134
  const dataDir = getDataDir();
133
135
  const updateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
134
- const restartMarker = path.join(dataDir, RESTART_MARKER_FILE);
135
136
 
136
137
  // Read package.json for current version
137
138
  let version = 'unknown';
@@ -145,13 +146,11 @@ function registerSystemEndpoints(app, io) {
145
146
 
146
147
  // Check if we're in the middle of an update
147
148
  const isUpdating = fs.existsSync(updateMarker);
148
- const restartRequested = fs.existsSync(restartMarker);
149
149
 
150
150
  res.json({
151
151
  version,
152
152
  updating: isUpdating,
153
- restartRequested,
154
- ready: !isUpdating // Server is ready if not updating
153
+ ready: !isUpdating
155
154
  });
156
155
  });
157
156
 
@@ -194,20 +193,6 @@ function registerSystemEndpoints(app, io) {
194
193
  const platform = process.platform;
195
194
  const dataDir = getDataDir();
196
195
 
197
- // Determine the install script based on platform
198
- let installScript;
199
- if (platform === 'win32') {
200
- installScript = path.join(__dirname, '../../../../sshift-install.ps1');
201
- } else {
202
- installScript = path.join(__dirname, '../../../../sshift-install.sh');
203
- }
204
-
205
- // Check if install script exists
206
- if (!fs.existsSync(installScript)) {
207
- res.status(500).json({ error: 'Install script not found' });
208
- return;
209
- }
210
-
211
196
  // Write update marker to indicate update in progress
212
197
  const updateMarker = path.join(dataDir, UPDATE_MARKER_FILE);
213
198
  try {
@@ -221,48 +206,62 @@ function registerSystemEndpoints(app, io) {
221
206
  console.error('[UPDATE] Failed to write update marker:', e.message);
222
207
  }
223
208
 
224
- // Write a restart marker file to indicate we want to restart after update
225
- const restartMarker = path.join(dataDir, RESTART_MARKER_FILE);
226
- try {
227
- fs.writeFileSync(restartMarker, 'true');
228
- } catch (e) {
229
- console.error('[UPDATE] Failed to write restart marker:', e.message);
230
- }
231
-
232
209
  // Send response immediately, then update in background
233
210
  res.json({ message: 'Update started. Server will restart automatically.' });
234
211
 
235
- // Execute update script with --update flag (detached from parent process)
236
- // Set SSHIFT_NO_SUDO=1 to prevent sudo prompts which won't work in detached mode
237
- const updateCommand = platform === 'win32'
238
- ? `powershell.exe -ExecutionPolicy Bypass -File "${installScript}" --update`
239
- : `"${installScript}" --update`;
240
-
241
- const updateEnv = { ...process.env, SSHIFT_NO_SUDO: '1' };
212
+ // Write a temporary update script and run it detached
213
+ const nodeExe = process.execPath;
214
+ const sshiftScript = process.argv[1];
215
+ const updateScriptPath = path.join(dataDir, '.sshift-update.sh');
216
+
217
+ if (platform === 'win32') {
218
+ const psScript = `npm install -g @lethevimlet/sshift@latest; & '${nodeExe}' '${sshiftScript}'`;
219
+ fs.writeFileSync(updateScriptPath.replace('.sh', '.ps1'), psScript, 'utf8');
220
+ } else {
221
+ const updateScript = `#!/bin/sh
222
+ npm install -g @lethevimlet/sshift@latest
223
+ exec "${nodeExe}" "${sshiftScript}"
224
+ `;
225
+ fs.writeFileSync(updateScriptPath, updateScript, 'utf8');
226
+ fs.chmodSync(updateScriptPath, 0o755);
227
+ }
242
228
 
243
229
  console.log('[UPDATE] Starting update process...');
244
230
 
245
- // Wait for response to be sent before spawning update process
246
- res.on('finish', () => {
247
- // Use spawn with detached mode to allow the update script to continue after parent exits
248
- const updateProcess = spawn(updateCommand, [], {
249
- cwd: path.join(__dirname, '../../../..'),
250
- shell: true,
251
- detached: true,
252
- stdio: 'ignore',
253
- env: updateEnv
254
- });
255
-
256
- // Unref the child process so the parent can exit without waiting for it
257
- updateProcess.unref();
258
-
259
- // Give the update script a moment to start
260
- setTimeout(() => {
261
- // Exit immediately to allow the update script to manage the restart
262
- console.log('[UPDATE] Exiting for update...');
263
- process.exit(0);
264
- }, 500);
231
+ let spawnFailed = false;
232
+ let spawnCommand, spawnArgs;
233
+ if (platform === 'win32') {
234
+ spawnCommand = 'powershell';
235
+ spawnArgs = ['-ExecutionPolicy', 'Bypass', '-File', updateScriptPath.replace('.sh', '.ps1')];
236
+ } else {
237
+ spawnCommand = '/bin/sh';
238
+ spawnArgs = [updateScriptPath];
239
+ }
240
+
241
+ const updateProcess = spawn(spawnCommand, spawnArgs, {
242
+ cwd: dataDir,
243
+ detached: true,
244
+ stdio: 'ignore',
245
+ env: { ...process.env }
246
+ });
247
+
248
+ updateProcess.on('error', (err) => {
249
+ console.error('[UPDATE] Failed to start update process:', err.message);
250
+ spawnFailed = true;
251
+ // Clean up marker since update failed
252
+ try { fs.unlinkSync(updateMarker); } catch (e) { /* ignore */ }
265
253
  });
254
+
255
+ updateProcess.unref();
256
+
257
+ setTimeout(() => {
258
+ if (spawnFailed) {
259
+ console.error('[UPDATE] Update process failed to start. Keeping server running.');
260
+ return;
261
+ }
262
+ console.log('[UPDATE] Exiting for update...');
263
+ process.exit(0);
264
+ }, 500);
266
265
  } catch (err) {
267
266
  console.error('Error triggering update:', err);
268
267
  res.status(500).json({ error: 'Failed to trigger update' });