<commit_before>/* eslint-env node */
const electron = require("electron")
const {app, BrowserWindow, ipcMain, Menu, shell} = electron
const isDev = require("electron-is-dev")
const { URL } = require("url")
const defaultMenu = require("electron-default-menu")
const updater = require("./updater")
const logger = require("./logger")

const {authorizeUser, fetchAccessToken} = require("./userAuthentication")

let mainWindow
let deepLinkURLOnReady = null

logger.init()

function createWindow () {
  logger.info("creating app window")

  app.setAsDefaultProtocolClient("x-github-classroom")
  const menu = defaultMenu(app, shell)
  Menu.setApplicationMenu(Menu.buildFromTemplate(menu))

  mainWindow = new BrowserWindow({width: 900, height: 600})
  const url = `file://${__dirname}/index.html`
  mainWindow.loadURL(url)

  if (isDev) {
    mainWindow.webContents.openDevTools()
  }

  if (!isDev) {
    let msBetweenUpdates = 1000 * 60 * 30
    updater.start(app, msBetweenUpdates, () => {
      mainWindow.webContents.send("info", {msg: "update found"})
    }, (err) => {
      mainWindow.webContents.send("info", {msg: err})
    })
  }

  mainWindow.on("closed", function () {
    mainWindow = null
  })

  ipcMain.on("initialized", () => {
    if (deepLinkURLOnReady != null) {
      // If open-url event was fired before app was ready
      loadPopulatePage(deepLinkURLOnReady)
      deepLinkURLOnReady = null
    }
  })

  ipcMain.on("requestAuthorization", (e, assignmentURL) => {
    authorizeUser(mainWindow, assignmentURL)
  })
}

function loadPopulatePage (assignmentURL) {
  mainWindow.webContents.send("open-url", assignmentURL)
}

app.on("open-url", function (event, urlToOpen) {
  event.preventDefault()
  let urlParams = new URL(urlToOpen).searchParams
  let isClassroomDeeplink = urlParams.has("assignment_url")
  let isOAuthDeeplink = urlParams.has("code")

  if (isClassroomDeeplink) {
    let assignmentURL = urlParams.get("assignment_url")
    if (app.isReady()) {
      loadPopulatePage(assignmentURL)
    } else {
      deepLinkURLOnReady = assignmentURL
    }
  } else if (isOAuthDeeplink) {
    let oauthCode = urlParams.get("code")
    fetchAccessToken(oauthCode)
  }
})

app.on("ready", createWindow)

app.on("window-all-closed", function () {
  if (process.platform !== "darwin") {
    app.quit()
  }
})

app.on("activate", function () {
  if (mainWindow === null) {
    createWindow()
  }
})<commit_msg>move protocol handler to app ready event instead of create window<commit_after>/* eslint-env node */
const electron = require("electron")
const {app, BrowserWindow, ipcMain, Menu, shell} = electron
const isDev = require("electron-is-dev")
const { URL } = require("url")
const defaultMenu = require("electron-default-menu")
const updater = require("./updater")
const logger = require("./logger")

const {authorizeUser, fetchAccessToken} = require("./userAuthentication")

let mainWindow
let deepLinkURLOnReady = null

logger.init()

function createWindow () {
  logger.info("creating app window")

  const menu = defaultMenu(app, shell)
  Menu.setApplicationMenu(Menu.buildFromTemplate(menu))

  mainWindow = new BrowserWindow({width: 900, height: 600})
  const url = `file://${__dirname}/index.html`
  mainWindow.loadURL(url)

  if (isDev) {
    mainWindow.webContents.openDevTools()
  }

  if (!isDev) {
    let msBetweenUpdates = 1000 * 60 * 30
    updater.start(app, msBetweenUpdates, () => {
      mainWindow.webContents.send("info", {msg: "update found"})
    }, (err) => {
      mainWindow.webContents.send("info", {msg: err})
    })
  }

  mainWindow.on("closed", function () {
    mainWindow = null
  })

  ipcMain.on("initialized", () => {
    if (deepLinkURLOnReady != null) {
      // If open-url event was fired before app was ready
      loadPopulatePage(deepLinkURLOnReady)
      deepLinkURLOnReady = null
    }
  })

  ipcMain.on("requestAuthorization", (e, assignmentURL) => {
    authorizeUser(mainWindow, assignmentURL)
  })
}

function loadPopulatePage (assignmentURL) {
  mainWindow.webContents.send("open-url", assignmentURL)
}

app.on("open-url", function (event, urlToOpen) {
  event.preventDefault()
  let urlParams = new URL(urlToOpen).searchParams
  let isClassroomDeeplink = urlParams.has("assignment_url")
  let isOAuthDeeplink = urlParams.has("code")

  if (isClassroomDeeplink) {
    let assignmentURL = urlParams.get("assignment_url")
    if (app.isReady()) {
      loadPopulatePage(assignmentURL)
    } else {
      deepLinkURLOnReady = assignmentURL
    }
  } else if (isOAuthDeeplink) {
    let oauthCode = urlParams.get("code")
    fetchAccessToken(oauthCode)
  }
})

app.on("ready", () => {
  app.setAsDefaultProtocolClient("x-github-classroom")
  createWindow()
})

app.on("window-all-closed", function () {
  if (process.platform !== "darwin") {
    app.quit()
  }
})

app.on("activate", function () {
  if (mainWindow === null) {
    createWindow()
  }
})
{'fraction_non_alphanumeric': 0.14425030475416498, 'fraction_numerical': 0.005688744412840309, 'mean_word_length': 4.09100310237849, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, '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': '284806', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1591, 'n_words': 322}