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

Code before:
  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.
// Given our builder/app design, we currently don't have an easy way to offer
// upgrades from the app themselves (like browsers do).

Review comment (bug):
This log isn't visible. And I realize that none of the `log.` logs in `app` are visible (neither in the terminal, nor in the app's devtools/console), because we do import `log` but we never set the level.

Since there are many instances of the problem (all in `app/main.ts` and `app/mainWindow.ts`), I suggest we let it be in this PR, but a separate PR could address this, by

1. Looking at argv and setting `log`'s level, to `info` or to `debug` if a `--debug` or `--verbose` arg is passed.
2. Converting `console.`... calls in `app` to use `log` (14 hits across 6 files in `app`)

Code after:
  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.
// Given our builder/app design, we currently don't have an easy way to offer
// upgrades from the app themselves (like browsers do).