Repository: jiahaog/nativefier
File: app/src/main.ts (TypeScript)

Code before:
  dialog,
  globalShortcut,
  systemPreferences,
  BrowserWindow,
} from 'electron';
import electronDownload from 'electron-dl';
import * as log from 'loglevel';

import { createLoginWindow } from './components/loginWindow';
import {
  createMainWindow,
  saveAppArgs,
  APP_ARGS_FILE_PATH,
} from './components/mainWindow';
import { createTrayIcon } from './components/trayIcon';
import { isOSX } from './helpers/helpers';
import { inferFlashPath } from './helpers/inferFlash';

// Entrypoint for Squirrel, a windows update framework. See https://github.com/nativefier/nativefier/pull/744
if (require('electron-squirrel-startup')) {
  app.exit();
}

const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));

// Do this relatively early so that we can start storid appData with the app
if (appArgs.portable) {
  log.debug(
    'Setting appData and userData to ',
    path.resolve(path.join(__dirname, '..', 'appData')),
  );
  app.setPath('appData', path.join(__dirname, '..', 'appData'));
  app.setPath('userData', path.join(__dirname, '..', 'appData'));
}

// Take in a URL on the command line as an override
if (process.argv.length > 1) {
  const maybeUrl = process.argv[1];
  try {
    new URL(maybeUrl);
    appArgs.targetUrl = maybeUrl;
    console.info('Loading override URL passed as argument:', maybeUrl);
  } catch (err) {
    console.error(
      'Not loading override URL passed as argument, because failed to parse:',
      maybeUrl,
    );
  }
}

// Nativefier is a browser, and an old browser is an insecure / badly-performant one.

Review comment (suggestion):
```suggestion
// Do this relatively early so that we can start storing appData with the app
```

Code after:
  dialog,
  globalShortcut,
  systemPreferences,
  BrowserWindow,
} from 'electron';
import electronDownload from 'electron-dl';
import * as log from 'loglevel';

import { createLoginWindow } from './components/loginWindow';
import {
  createMainWindow,
  saveAppArgs,
  APP_ARGS_FILE_PATH,
} from './components/mainWindow';
import { createTrayIcon } from './components/trayIcon';
import { isOSX } from './helpers/helpers';
import { inferFlashPath } from './helpers/inferFlash';

// Entrypoint for Squirrel, a windows update framework. See https://github.com/nativefier/nativefier/pull/744
if (require('electron-squirrel-startup')) {
  app.exit();
}

const appArgs = JSON.parse(fs.readFileSync(APP_ARGS_FILE_PATH, 'utf8'));

// Do this relatively early so that we can start storing appData with the app
if (appArgs.portable) {
  log.debug(
    'App was built as portable; setting appData and userData to the app folder: ',
    path.resolve(path.join(__dirname, '..', 'appData')),
  );
  app.setPath('appData', path.join(__dirname, '..', 'appData'));
  app.setPath('userData', path.join(__dirname, '..', 'appData'));
}

// Take in a URL on the command line as an override
if (process.argv.length > 1) {
  const maybeUrl = process.argv[1];
  try {
    new URL(maybeUrl);
    appArgs.targetUrl = maybeUrl;
    console.info('Loading override URL passed as argument:', maybeUrl);
  } catch (err) {
    console.error(
      'Not loading override URL passed as argument, because failed to parse:',
      maybeUrl,
    );
  }
}

// Nativefier is a browser, and an old browser is an insecure / badly-performant one.