Skip to content

Commit

Permalink
Move wp-desktop files to wp-calypso monorepo (Automattic#43324)
Browse files Browse the repository at this point in the history
* Copy files from wp-desktop

* Remove obsolete module replacements

* Fix tests by upgrading express and deduplicating cookie pkg

* Add ELECTRON_BUILDER_ARGS option to the build-desktop:app task

* Desktop Server: eliminate node_modules and bundle all server modules into the output

* Desktop: turn on minification for both server and client

* AssetsWriter: ensure with mkdirp that the target dir exists

* Make the repo compatible with both old- and new-style desktop build

* Fix import paths for desktop modules

* Setup build scripts and packages for the desktop app

* Reformat imported wp-desktop files with Prettier

* ESLint: Remove use strict from wp-desktop modules

* ESLint: Fix issues in wp-desktop modules

* Check the config/secrets.json file during build-desktop:config

* Remove the desktop/ folder from the workspace tree

* Install a desktop-specific packages root in desktop/
  • Loading branch information
jsnajdr authored Jun 29, 2020
1 parent 92224ff commit 367b1fa
Show file tree
Hide file tree
Showing 145 changed files with 7,959 additions and 29 deletions.
16 changes: 14 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ yarn-error.log
/server/devdocs/search-index.js
/server/devdocs/components-usage-stats.json
/server/devdocs/proptypes-index.json
/server/bundler/assets*.json
/client/server/devdocs/search-index.js
/client/server/devdocs/components-usage-stats.json
/client/server/devdocs/proptypes-index.json
/client/server/bundler/assets*.json

*.rdb
*.db
Expand All @@ -66,3 +64,17 @@ cached-requests.json
/apps/*/dist/
/apps/*/types/
/packages/*/dist/

# webpack assets
/assets*.json
/desktop/assets*.json
/server/bundler/assets*.json
/client/server/bundler/assets*.json

# Desktop app
/desktop/build
/desktop/client
/desktop/config
/desktop/public
/desktop/release
/client/desktop/config.json
13 changes: 13 additions & 0 deletions client/desktop/app-handlers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
App Handlers
=====================

App handlers are bits of code that run before the app is started and the main window has opened. Code here cannot use window or dialog functions.

The following handlers are available:

- [Crash Reporting](crash-reporting/README.md)
- [Exceptions](exceptions/README.md)
- [Preferences](preferences/README.md)
- [Printer](printer/README.md)
- [Secrets](secrets/README.md)
- [Updater](updater/README.md)
8 changes: 8 additions & 0 deletions client/desktop/app-handlers/crash-reporting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Crash Reporting
=========

Uses the [Electron crash reporter module](https://github.com/atom/electron/blob/master/docs/api/crash-reporter.md) to send crashes to a remote URL.

# Config

Requires `crash_reporter` to be set
25 changes: 25 additions & 0 deletions client/desktop/app-handlers/crash-reporting/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External Dependencies
*/
const { app, crashReporter } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

/**
* Internal dependencies
*/
const Config = require( 'desktop/lib/config' );
const log = require( 'desktop/lib/logger' )( 'desktop:crash-reporting' );

module.exports = function () {
if ( Config.crash_reporter.electron ) {
app.on( 'will-finish-launching', function () {
log.info( 'Crash reporter started' );

crashReporter.start( {
productName: Config.description,
companyName: Config.author,
submitURL: Config.crash_reporter.url,
uploadToServer: true,
} );
} );
}
};
8 changes: 8 additions & 0 deletions client/desktop/app-handlers/exceptions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Exceptions
=========

Wraps exceptions in the app to provide a better experience.

Uses the [crash tracker](../../lib/crash-tracker/README.md) to send crashes to a remote URL.

When running in release mode the exception stack trace is hidden and replaced with a custom dialog that quits the app.
82 changes: 82 additions & 0 deletions client/desktop/app-handlers/exceptions/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* External Dependencies
*/
const { app, dialog } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

/**
* Internal dependencies
*/
const crashTracker = require( 'desktop/lib/crash-tracker' );
const system = require( 'desktop/lib/system' );
const log = require( 'desktop/lib/logger' )( 'desktop:exceptions', { handleExceptions: true } );

/**
* Module variables
*/
let isReady = false;
let thereCanBeOnlyOne = false;

// We ignore any of these errors as they are probably temporary
const NETWORK_ERRORS = [ 'ETIMEDOUT', 'ENOTFOUND', 'ECONNREFUSED', 'ECONNRESET' ];

function exit() {
if ( isReady ) {
app.quit();
} else {
process.exit( 1 ); // eslint-disable-line no-process-exit
}
}

function showErrorAndExit( error ) {
if ( thereCanBeOnlyOne ) {
exit();
}

thereCanBeOnlyOne = true;

dialog.showErrorBox(
'WordPress.com ran into an error',
'Please restart the app and try again.' +
'\n\n' +
'If you continue to have issues, please contact us at [email protected] and mention the error details below:' +
'\n\n' +
error.stack +
'\n\n' +
'System info: ' +
JSON.stringify( system.getVersionData() )
);

exit();
}

function isFatalError( error ) {
return ! NETWORK_ERRORS.includes( error.code );
}

function exceptionHandler( error ) {
if ( ! isFatalError( error ) ) {
return;
}

log.error( error );

if ( crashTracker.isEnabled() ) {
crashTracker.track(
'exception',
{ name: error.name, message: error.message, stack: error.stack },
function () {
showErrorAndExit( error );
}
);
} else {
showErrorAndExit( error );
}
}

module.exports = function () {
app.on( 'ready', function () {
isReady = true;
} );

process.on( 'uncaughtException', exceptionHandler );
};
5 changes: 5 additions & 0 deletions client/desktop/app-handlers/logging/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const ipcHandler = require( './ipc-handler' );

module.exports = function () {
ipcHandler.listen();
};
58 changes: 58 additions & 0 deletions client/desktop/app-handlers/logging/ipc-handler/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* External Dependencies
*/
const { ipcMain: ipc } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

module.exports = {
/**
* JSON object that stores references to loggers initialized
* by calls from the renderer process over IPC (keyed by `namespace`).
*/
loggers: {},

/**
* Gets the logging object for the provided `namespace` (and creates a logger
* if one doesn't exist). Logger references are tracked in the `loggers`
* object and keyed by `namespace`.
*
* @param {string} namespace Namespace of the logger to be used or initialized.
* @param {object} options Logger configuration.
* @returns {object} Logger instance.
*/
getLogger: function ( namespace, options ) {
let logger = this.loggers[ namespace ];
if ( ! logger ) {
logger = require( 'desktop/lib/logger' )( namespace, options );
this.loggers[ namespace ] = logger;
}
return logger;
},

/**
* Registers an ipc listener on the `log` channel to relay logging
* events from the renderer process.
*/
listen: function () {
ipc.on( 'log', ( _, level, namespace, options, message, meta ) => {
const logger = this.getLogger( namespace, options );

switch ( level ) {
case 'error':
logger.error( message, meta );
break;
case 'warn':
logger.warn( message, meta );
break;
case 'info':
logger.info( message, meta );
break;
case 'debug':
logger.debug( message, meta );
break;
case 'silly':
logger.silly( message, meta );
break;
}
} );
},
};
11 changes: 11 additions & 0 deletions client/desktop/app-handlers/preferences/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Preferences
=========

Monitors for preference IPC messages and updates the settings file.

## IPC messages

Listens for the following:

- `preferences-changed-proxy-type` - proxy changed, tell user to restart the app
- `preferences-changed` - a preference setting has changed
37 changes: 37 additions & 0 deletions client/desktop/app-handlers/preferences/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* External Dependencies
*/
const { dialog, ipcMain: ipc } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

/**
* Internal dependencies
*/
const Settings = require( 'desktop/lib/settings' );

function promptForRestart( title, message ) {
// Warn user they need to restart the app
dialog.showMessageBox(
{
buttons: [ 'OK' ],
title: title,
message: message,
detail: 'The app needs to be restarted for this to take effect.',
},
function () {}
);
}

module.exports = function () {
ipc.on( 'preferences-changed', function ( event, { name, value } ) {
if ( 'proxy-type' === name ) {
promptForRestart( 'Proxy changed', 'You have changed the proxy settings.' );
} else if ( 'spellcheck-enabled' === name ) {
promptForRestart(
value ? 'Spellchecker enabled' : 'Spellchecker disabled',
'You have changed the spellchecker settings.'
);
}

Settings.saveSetting( name, value );
} );
};
10 changes: 10 additions & 0 deletions client/desktop/app-handlers/printer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Printer
=========

Provides a way for Calypso to print. This is used for printing backup codes.

## IPC messages

Listens for the following:

- `print` - print a given bit of HTML
29 changes: 29 additions & 0 deletions client/desktop/app-handlers/printer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* External Dependencies
*/
const { BrowserWindow, ipcMain: ipc } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

/**
* Internal dependencies
*/
const log = require( 'desktop/lib/logger' )( 'desktop:printer' );

module.exports = function () {
ipc.on( 'print', function ( event, title, html ) {
let printer = new BrowserWindow( { width: 350, height: 300, title: title } );

log.debug( 'Printing HTML' );

printer.loadURL( 'data:text/html,' + encodeURIComponent( html ) );

printer.webContents.on( 'dom-ready', function () {
setTimeout( function () {
printer.webContents.print();
}, 500 );
} );

printer.on( 'closed', function () {
printer = null;
} );
} );
};
10 changes: 10 additions & 0 deletions client/desktop/app-handlers/secrets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Secrets
=========

Show a secret screen. Shhh.

## IPC messages

Listens for the following:

- `secrets` - which secret to show
21 changes: 21 additions & 0 deletions client/desktop/app-handlers/secrets/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* External Dependencies
*/
const { ipcMain: ipc } = require( 'electron' ); // eslint-disable-line import/no-extraneous-dependencies

/**
* Internal dependencies
*/
const WindowManager = require( 'desktop/lib/window-manager' );

ipc.on( 'secrets', function ( ev, which ) {
which = parseInt( which, 10 );

if ( which === 0 ) {
WindowManager.openSecret();
} else if ( which === 1 ) {
WindowManager.openWapuu();
}
} );

module.exports = function () {};
11 changes: 11 additions & 0 deletions client/desktop/app-handlers/updater/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Updater
=========

Uses either the manual or auto updater to check for updates.

The config `updater.url` is used as the base URL and has the platform, version, and beta setting appended.

The updater uses:

- [Auto Updater](auto-updater/README.md)
- [Manual Updater](manual-updater/README.md)
8 changes: 8 additions & 0 deletions client/desktop/app-handlers/updater/auto-updater/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Auto Updater
=========

Handles Squirrel auto-update events. Note that the app must be signed for this to work.

## Functions

`ping()` - checks for an update
Loading

0 comments on commit 367b1fa

Please sign in to comment.