<commit_before>const {
  BrowserWindow,
  protocol,
  ipcMain,
  session,
} = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const mime = require('mime-types');
const windowStateKeeper = require('electron-window-state');

const { getDistDirectory } = require('altair-static');

const { getStore } = require('./store');
const { createMenu } = require('./menu');
const { createTouchBar } = require('./touchbar');
const { checkForUpdates } = require('./updates');
const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions');
const { importBackupData, exportBackupData } = require('./utils/backup');

/**
 * @type {BrowserWindow}
 */
let instance = null;

const headersToSet = [ 'Origin', 'Cookie' ];
let requestHeaders = {};

const actions = {
  createTab: () => {
    console.log('Create tab.');
    instance.webContents.send('create-tab', true);
  },
  closeTab: () => {
    console.log('Close tab.');
    instance.webContents.send('close-tab', true);
  },
  sendRequest: () => {
    instance.webContents.send('send-request', true);
  },
  reloadDocs: () => {
    instance.webContents.send('reload-docs', true);
  },
  showDocs: () => {
    instance.webContents.send('show-docs', true);
  },
  showSettings: () => {
    instance.webContents.send('show-settings', true);
  },
  importAppData: () => {
    return importBackupData(instance);
  },
  exportAppData: () => {
    return exportBackupData(instance);
  },
  checkForUpdates,
};

const createWindow = () => {

  const getFilePath = (filePath) => {
    return new Promise((resolve, reject) => {

      if (!filePath) {
        return resolve();
      }

      console.log('checking stat..', filePath);
      fs.stat(filePath, (err, stats) => {
        if (err) {
          return reject(err);
        }
        if (stats.isFile()) {
          return resolve(filePath);
        }

        if (stats.isDirectory()) {
          return resolve(getFilePath(path.join(filePath, 'index.html')));
        }

        return resolve();
      });
    });
  };

  /**
   * Using a custom buffer protocol, instead of a file protocol because of restrictions with the file protocol.
   */
  protocol.registerBufferProtocol('altair', (request, callback) => {

    const requestDirectory = getDistDirectory();
    const filePath = path.join(requestDirectory, new url.URL(request.url).pathname);
    const indexPath = path.join(requestDirectory, 'index.html');

    getFilePath(filePath).then((filePath) => {
      if (!filePath) {
        filePath = indexPath;
      }

      fs.readFile(filePath, 'utf8', function(err, data) {
        if (err) {
          return console.log('Error loading file to buffer.', err);
        }

        // Load the data from the file into a buffer and pass it to the callback
        // Using the mime package to get the mime type for the file, based on the file name
        callback({ mimeType: mime.lookup(filePath), data: Buffer.from(data) });
      });<commit_msg>Added init function to electron.<commit_after>const {
  BrowserWindow,
  protocol,
  ipcMain,
  session,
} = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const mime = require('mime-types');
const windowStateKeeper = require('electron-window-state');

const { getDistDirectory } = require('altair-static');

const { getStore } = require('./store');
const { createMenu } = require('./menu');
const { createTouchBar } = require('./touchbar');
const { checkForUpdates } = require('./updates');
const { checkMultipleDataVersions } = require('./utils/check-multi-data-versions');
const { importBackupData, exportBackupData } = require('./utils/backup');

/**
 * @type {BrowserWindow}
 */
let instance = null;

const headersToSet = [ 'Origin', 'Cookie' ];
let requestHeaders = {};

const actions = {
  createTab: () => {
    console.log('Create tab.');
    instance.webContents.send('create-tab', true);
  },
  closeTab: () => {
    console.log('Close tab.');
    instance.webContents.send('close-tab', true);
  },
  sendRequest: () => {
    instance.webContents.send('send-request', true);
  },
  reloadDocs: () => {
    instance.webContents.send('reload-docs', true);
  },
  showDocs: () => {
    instance.webContents.send('show-docs', true);
  },
  showSettings: () => {
    instance.webContents.send('show-settings', true);
  },
  importAppData: () => {
    return importBackupData(instance);
  },
  exportAppData: () => {
    return exportBackupData(instance);
  },
  checkForUpdates,
};

const createWindow = () => {

  const getFilePath = (filePath) => {
    return new Promise((resolve, reject) => {

      if (!filePath) {
        return resolve();
      }

      console.log('checking stat..', filePath);
      fs.stat(filePath, (err, stats) => {
        if (err) {
          return reject(err);
        }
        if (stats.isFile()) {
          return resolve(filePath);
        }

        if (stats.isDirectory()) {
          return resolve(getFilePath(path.join(filePath, 'index.html')));
        }

        return resolve();
      });
    });
  };

  /**
   * Using a custom buffer protocol, instead of a file protocol because of restrictions with the file protocol.
   */
  protocol.registerBufferProtocol('altair', (request, callback) => {

    const requestDirectory = getDistDirectory();
    const filePath = path.join(requestDirectory, new url.URL(request.url).pathname);
    const indexPath = path.join(requestDirectory, 'index.html');

    getFilePath(filePath).then((filePath) => {
      if (!filePath) {
        filePath = indexPath;
      }

      fs.readFile(filePath, 'utf8', function(err, data) {
        if (err) {
          return console.log('Error loading file to buffer.', err);
        }
        if (filePath && filePath.includes('index.html') && data && !data.includes('AltairGraphQL.init')) {
          data = data.replace('</body>', '<script>AltairGraphQL.init()</script></body>')
        }

        // Load the data from the file into a buffer and pass it to the callback
        // Using the mime package to get the mime type for the file, based on the file name
        callback({ mimeType: mime.lookup(filePath), data: Buffer.from(data) });
      });
{'fraction_non_alphanumeric': 0.16243411595591759, 'fraction_numerical': 0.00031943778949049674, 'mean_word_length': 3.095487246566383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, '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': '4923987', 'n_tokens_mistral': 1855, 'n_tokens_neox': 1834, 'n_words': 424}