<commit_before>import '../lib/logging/main/install'

import { app, Menu, MenuItem, ipcMain, BrowserWindow, autoUpdater, dialog } from 'electron'

import { AppWindow } from './app-window'
import { CrashWindow } from './crash-window'
import { buildDefaultMenu, MenuEvent, findMenuItemByID, setCrashMenu } from './menu'
import { shellNeedsPatching, updateEnvironmentForProcess } from '../lib/shell'
import { parseURL } from '../lib/parse-url'
import { handleSquirrelEvent } from './squirrel-updater'
import { SharedProcess } from '../shared-process/shared-process'
import { fatalError } from '../lib/fatal-error'

import { IMenuItemState } from '../lib/menu-update'
import { LogLevel } from '../lib/logging/log-level'
import { log as writeLog } from './log'
import { formatError } from '../lib/logging/format-error'
import { reportError } from './exception-reporting'
import { enableSourceMaps, withSourceMappedStack } from '../lib/source-map-support'
import { now } from './now'

enableSourceMaps()

let mainWindow: AppWindow | null = null
let sharedProcess: SharedProcess | null = null

const launchTime = now()

let preventQuit = false
let readyTime: number | null = null
let hasReportedUncaughtException = false

type OnDidLoadFn = (window: AppWindow) => void
/** See the `onDidLoad` function. */
let onDidLoadFns: Array<OnDidLoadFn> | null = []

function uncaughtException(error: Error) {
  error = withSourceMappedStack(error)

  log.error(formatError(error))

  if (hasReportedUncaughtException) {
    return
  }

  hasReportedUncaughtException = true
  preventQuit = true

  setCrashMenu()

  const isLaunchError = !mainWindow

  if (mainWindow) {
    mainWindow.destroy()
    mainWindow = null
  }

  if (sharedProcess) {
    sharedProcess.destroy()
    mainWindow = null
  }

  const crashWindow = new CrashWindow(isLaunchError ? 'launch' : 'generic', error)

  crashWindow.onDidLoad(() => {
    crashWindow.show()
  })

  crashWindow.onFailedToLoad(() => {
    dialog.showMessageBox({
      type: 'error',
      title: __DARWIN__ ? `Unrecoverable Error` : 'Unrecoverable error',
      message:
        `GitHub Desktop has encountered an unrecoverable error and will need to restart.\n\n` +
        `This has been reported to the team, but if you encounter this repeatedly please report ` +
        `this issue to the GitHub Desktop issue tracker.\n\n${error.stack || error.message}`,
    }, (response) => {
      if (!__DEV__) {
        app.relaunch()
      }
      app.quit()
    })
  })

  crashWindow.onClose(() => {
    if (!__DEV__) {
      app.relaunch()
    }
    app.quit()
  })

  crashWindow.load()
}

process.on('uncaughtException', (error: Error) => {
  reportError(error)
  uncaughtException(error)
})

if (__WIN32__ && process.argv.length > 1) {
  if (handleSquirrelEvent(process.argv[1])) {
    app.quit()
  } else {
    const action = parseURL(process.argv[1])
    if (action.name === 'open-repository') {
      onDidLoad(window => {
        window.sendURLAction(action)
      })
    }
  }
}

if (shellNeedsPatching(process)) {
  updateEnvironmentForProcess()
}

const isDuplicateInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (mainWindow) {
    if (mainWindow.isMinimized()) {
      mainWindow.restore()
    }
    mainWindow.focus()
  }

  // look at the second argument received, it should have the OAuth
  // callback contents and code for us to complete the signin flow
  if (commandLine.length > 1) {
    const action = parseURL(commandLine[1])
    onDidLoad(window => {
      window.sendURLAction(action)
    })
  }
})

if (isDuplicateInstance) {
  app.quit()
}

app.on('will-finish-launching', () => {
  app.on('open-url', (event, url) => {
    event.preventDefault()

    const action = parseURL(url)
    onDidLoad(window => {
      // This manual focus call _shouldn't_ be necessary, but is for Chrome on
      // macOS. See https://github.com/desktop/desktop/issues/973.
      window.focus()
      window.sendURLAction(action)
    })
  })
})

app.on('ready', () => {
  if (isDuplicateInstance) { return }

  readyTime = now() - launchTime

  app.setAsDefaultProtocolClient('x-github-client')
  // Also support Desktop Classic's protocols.
  if (__DARWIN__) {
    app.setAsDefaultProtocolClient('github-mac')
  } else if (__WIN32__) {
    app.setAsDefaultProtocolClient('github-windows')
  }
<commit_msg>Add protocol client based on dev or not<commit_after>import '../lib/logging/main/install'

import { app, Menu, MenuItem, ipcMain, BrowserWindow, autoUpdater, dialog } from 'electron'

import { AppWindow } from './app-window'
import { CrashWindow } from './crash-window'
import { buildDefaultMenu, MenuEvent, findMenuItemByID, setCrashMenu } from './menu'
import { shellNeedsPatching, updateEnvironmentForProcess } from '../lib/shell'
import { parseURL } from '../lib/parse-url'
import { handleSquirrelEvent } from './squirrel-updater'
import { SharedProcess } from '../shared-process/shared-process'
import { fatalError } from '../lib/fatal-error'

import { IMenuItemState } from '../lib/menu-update'
import { LogLevel } from '../lib/logging/log-level'
import { log as writeLog } from './log'
import { formatError } from '../lib/logging/format-error'
import { reportError } from './exception-reporting'
import { enableSourceMaps, withSourceMappedStack } from '../lib/source-map-support'
import { now } from './now'

enableSourceMaps()

let mainWindow: AppWindow | null = null
let sharedProcess: SharedProcess | null = null

const launchTime = now()

let preventQuit = false
let readyTime: number | null = null
let hasReportedUncaughtException = false

type OnDidLoadFn = (window: AppWindow) => void
/** See the `onDidLoad` function. */
let onDidLoadFns: Array<OnDidLoadFn> | null = []

function uncaughtException(error: Error) {
  error = withSourceMappedStack(error)

  log.error(formatError(error))

  if (hasReportedUncaughtException) {
    return
  }

  hasReportedUncaughtException = true
  preventQuit = true

  setCrashMenu()

  const isLaunchError = !mainWindow

  if (mainWindow) {
    mainWindow.destroy()
    mainWindow = null
  }

  if (sharedProcess) {
    sharedProcess.destroy()
    mainWindow = null
  }

  const crashWindow = new CrashWindow(isLaunchError ? 'launch' : 'generic', error)

  crashWindow.onDidLoad(() => {
    crashWindow.show()
  })

  crashWindow.onFailedToLoad(() => {
    dialog.showMessageBox({
      type: 'error',
      title: __DARWIN__ ? `Unrecoverable Error` : 'Unrecoverable error',
      message:
        `GitHub Desktop has encountered an unrecoverable error and will need to restart.\n\n` +
        `This has been reported to the team, but if you encounter this repeatedly please report ` +
        `this issue to the GitHub Desktop issue tracker.\n\n${error.stack || error.message}`,
    }, (response) => {
      if (!__DEV__) {
        app.relaunch()
      }
      app.quit()
    })
  })

  crashWindow.onClose(() => {
    if (!__DEV__) {
      app.relaunch()
    }
    app.quit()
  })

  crashWindow.load()
}

process.on('uncaughtException', (error: Error) => {
  reportError(error)
  uncaughtException(error)
})

if (__WIN32__ && process.argv.length > 1) {
  if (handleSquirrelEvent(process.argv[1])) {
    app.quit()
  } else {
    const action = parseURL(process.argv[1])
    if (action.name === 'open-repository') {
      onDidLoad(window => {
        window.sendURLAction(action)
      })
    }
  }
}

if (shellNeedsPatching(process)) {
  updateEnvironmentForProcess()
}

const isDuplicateInstance = app.makeSingleInstance((commandLine, workingDirectory) => {
  // Someone tried to run a second instance, we should focus our window.
  if (mainWindow) {
    if (mainWindow.isMinimized()) {
      mainWindow.restore()
    }
    mainWindow.focus()
  }

  // look at the second argument received, it should have the OAuth
  // callback contents and code for us to complete the signin flow
  if (commandLine.length > 1) {
    const action = parseURL(commandLine[1])
    onDidLoad(window => {
      window.sendURLAction(action)
    })
  }
})

if (isDuplicateInstance) {
  app.quit()
}

app.on('will-finish-launching', () => {
  app.on('open-url', (event, url) => {
    event.preventDefault()

    const action = parseURL(url)
    onDidLoad(window => {
      // This manual focus call _shouldn't_ be necessary, but is for Chrome on
      // macOS. See https://github.com/desktop/desktop/issues/973.
      window.focus()
      window.sendURLAction(action)
    })
  })
})

app.on('ready', () => {
  if (isDuplicateInstance) { return }

  readyTime = now() - launchTime

   if (__DEV__) {
    app.setAsDefaultProtocolClient('x-github-desktop-dev-auth')
  } else {
    app.setAsDefaultProtocolClient('x-github-desktop-auth')
    app.setAsDefaultProtocolClient('x-github-client')
  }

  // Also support Desktop Classic's protocols.
  if (__DARWIN__) {
    app.setAsDefaultProtocolClient('github-mac')
  } else if (__WIN32__) {
    app.setAsDefaultProtocolClient('github-windows')
  }

{'fraction_non_alphanumeric': 0.13161959399823478, 'fraction_numerical': 0.00264783759929391, 'mean_word_length': 3.4523575638506876, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1435261', 'n_tokens_mistral': 2884, 'n_tokens_neox': 2818, 'n_words': 684}