/ migrations / migrateAutoUpdatesToSettings.ts
migrateAutoUpdatesToSettings.ts
 1  import { logEvent } from 'src/services/analytics/index.js'
 2  import { getGlobalConfig, saveGlobalConfig } from '../utils/config.js'
 3  import { logError } from '../utils/log.js'
 4  import {
 5    getSettingsForSource,
 6    updateSettingsForSource,
 7  } from '../utils/settings/settings.js'
 8  /**
 9   * Migration: Move user-set autoUpdates preference to settings.json env var
10   * Only migrates if user explicitly disabled auto-updates (not for protection)
11   * This preserves user intent while allowing native installations to auto-update
12   */
13  export function migrateAutoUpdatesToSettings(): void {
14    const globalConfig = getGlobalConfig()
15  
16    // Only migrate if autoUpdates was explicitly set to false by user preference
17    // (not automatically for native protection)
18    if (
19      globalConfig.autoUpdates !== false ||
20      globalConfig.autoUpdatesProtectedForNative === true
21    ) {
22      return
23    }
24  
25    try {
26      const userSettings = getSettingsForSource('userSettings') || {}
27  
28      // Always set DISABLE_AUTOUPDATER to preserve user intent
29      // We need to overwrite even if it exists, to ensure the migration is complete
30      updateSettingsForSource('userSettings', {
31        ...userSettings,
32        env: {
33          ...userSettings.env,
34          DISABLE_AUTOUPDATER: '1',
35        },
36      })
37  
38      logEvent('tengu_migrate_autoupdates_to_settings', {
39        was_user_preference: true,
40        already_had_env_var: !!userSettings.env?.DISABLE_AUTOUPDATER,
41      })
42  
43      // explicitly set, so this takes effect immediately
44      process.env.DISABLE_AUTOUPDATER = '1'
45  
46      // Remove autoUpdates from global config after successful migration
47      saveGlobalConfig(current => {
48        const {
49          autoUpdates: _,
50          autoUpdatesProtectedForNative: __,
51          ...updatedConfig
52        } = current
53        return updatedConfig
54      })
55    } catch (error) {
56      logError(new Error(`Failed to migrate auto-updates: ${error}`))
57      logEvent('tengu_migrate_autoupdates_error', {
58        has_error: true,
59      })
60    }
61  }