Skip to content

Commit

Permalink
Updating main process initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
jarrodek committed Dec 5, 2018
1 parent dc7de75 commit dba5c85
Show file tree
Hide file tree
Showing 42 changed files with 694 additions and 362 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ Temporary Items
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json

test/tests-exe-dir
40 changes: 27 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class ArcInit {
listen() {
this.contextActions.listenMainEvents();
window.onbeforeunload = this.beforeUnloadWindow.bind(this);
const updateHandler = this.updateEventHandler.bind(this);
this.driveBridge.listen();
this.oauth2Proxy.listen();
this.themeManager.listen();
Expand All @@ -57,12 +56,25 @@ class ArcInit {
this.fs.listen();
this.amfService.listen();
this.search.listen();
ipc.on('checking-for-update', updateHandler);
ipc.on('update-available', updateHandler);
ipc.on('update-not-available', updateHandler);
ipc.on('autoupdate-error', updateHandler);
ipc.on('download-progress', updateHandler);
ipc.on('update-downloaded', updateHandler);

ipc.on('checking-for-update', () => {
this.updateEventHandler('checking-for-update');
});
ipc.on('update-available', (info) => {
this.updateEventHandler('update-available', info);
});
ipc.on('update-not-available', () => {
this.updateEventHandler('update-not-available');
});
ipc.on('autoupdate-error', (error) => {
this.updateEventHandler('autoupdate-error', error);
});
ipc.on('download-progress', (progressObj) => {
this.updateEventHandler('download-progress', progressObj);
});
ipc.on('update-downloaded', (info) => {
this.updateEventHandler('update-downloaded', info);
});
ipc.on('command', this.commandHandler.bind(this));
ipc.on('request-action', this.execRequestAction.bind(this));
ipc.on('theme-editor-preview', this._themePreviewHandler.bind(this));
Expand Down Expand Up @@ -208,21 +220,23 @@ class ArcInit {
beforeUnloadWindow() {
ipc.send('window-reloading');
}

/**
* Handles events related to the application auto-update action.
*
* @param {Object} sender
* @param {Array} message
* @param {String} type
* @param {Object|undefined} args
*/
updateEventHandler(sender, message) {
updateEventHandler(type, args) {
const app = this.app;
if (!app) {
return;
}
// console.log('updateEventHandler', message);
app.updateState = message;
if (message[0] === 'update-downloaded') {
app.updateState = type;
if (args) {
console.log(type, args);
}
if (type === 'update-downloaded') {
app.hasAppUpdate = true;
}
}
Expand Down
1 change: 1 addition & 0 deletions dev-app-update.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
owner: advanced-rest-client
repo: arc-electron
provider: github
updaterCacheDirName: advanced-rest-client-updater
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
"url": "https://github.com/advanced-rest-client/arc-electron"
},
"scripts": {
"start": "electron . --inspect --debug --workspace-path=\"~/arc-dev/workspace\" --settings-file=\"~/arc-dev/dev-settings.json\"",
"start": "electron . --inspect --debug --debug-level=\"silly\" --with-devtools --workspace-path=\"~/arc-dev/workspace\" --settings-file=\"~/arc-dev/dev-settings.json\" --themes-path=\"~/arc-dev/themes\"",
"postinstall": "electron-builder install-app-deps",
"test": "npm run test-main && npm run test-renderer && npm run test-app",
"test-main": "NODE_ENV=test ELECTRON_PATH=node_modules/.bin/electron electron-mocha test/**/*.main.spec.js scripts/packages/**/*.main.spec.js",
"test-renderer": "NODE_ENV=test ELECTRON_PATH=node_modules/.bin/electron electron-mocha --renderer scripts/packages/**/*.renderer.spec.js",
"test-renderer": "NODE_ENV=test ELECTRON_PATH=node_modules/.bin/electron electron-mocha --renderer --require-main 'test/renderer-setup-paths.js' scripts/packages/**/*.renderer.spec.js",
"test-app": "NODE_ENV=test ELECTRON_PATH=node_modules/.bin/electron mocha test/**.app.spec.js",
"test-win-all": "npm run test-win-main && npm run test-win-renderer && npm run test-win-app",
"test-win-main": "node node_modules/.bin/electron-mocha test/**/*.main.spec.js scripts/packages/**/*.main.spec.js",
Expand Down
7 changes: 6 additions & 1 deletion scripts/main/app-defaults.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const {ThemeDefaults} = require('./defaults/theme-defaults');
const {WorkspaceDefaults} = require('./defaults/workspace-defaults');
/**
* Class responsible for keeping application base environment stable.
*
Expand All @@ -13,7 +14,11 @@ class AppDefaults {
*/
prepareEnvironment() {
const td = new ThemeDefaults();
return td.prepareEnvironment();
return td.prepareEnvironment()
.then(() => {
const wd = new WorkspaceDefaults();
return wd.prepareEnvironment();
});
}
}
exports.AppDefaults = AppDefaults;
19 changes: 19 additions & 0 deletions scripts/main/app-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,26 @@ class AppOptions {
name: '--themes-path',
shortcut: '-t',
type: String
}, {
// Path to application components directory (with ARC components)
name: '--components-path',
shortcut: '-c',
type: String
}, {
// Opens ARC in dev mode (opened console, verbose log)
name: '--debug',
shortcut: '-d',
type: Boolean
}, {
// Debug log level. Default to "debug". Only valid when `--debug` is set
name: '--debug-level',
shortcut: '-l',
type: String
}, {
// Opens ARC in dev mode (opened console, verbose log)
name: '--with-devtools',
shortcut: '-w',
type: Boolean
}, {
name: '.', // from "npm start" to not print error
shortcut: '-dot',
Expand All @@ -48,6 +63,10 @@ class AppOptions {
shortcut: '-o',
type: String,
allowArray: true
}, {
name: '--test',
shortcut: '-test',
type: Boolean
}];
}
/**
Expand Down
32 changes: 31 additions & 1 deletion scripts/main/arc-environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {AppMenuService} = require('./app-menu-service');
const {DriveExport} = require('@advanced-rest-client/electron-drive');
const {SessionManager} = require('@advanced-rest-client/electron-session-state/main');
const {ContentSearchService} = require('../packages/search-service/main');
const {ThemeManager} = require('../packages/themes-manager/main/');
const {ArcWindowsManager} = require('./windows-manager');
const {UpdateStatus} = require('./update-status');
const {AppPrompts} = require('./app-prompts');
Expand All @@ -16,6 +17,7 @@ const log = require('./logger');
class ArcEnvironment {
constructor(params = {}) {
this.isDebug = params.isDebug || false;
this.withDevtools = params.withDevtools || false;
this._initializeConfiguration(params);
this._initializeWindowsManager(params);
this._initializeMenu();
Expand All @@ -24,7 +26,7 @@ class ArcEnvironment {
this._initializeSessionManager();
this._initializeApplicationMenu();
this._initializeAppPrompts();

this._initializeThemes();
Oauth2Identity.listen();

// Remote commands protocol
Expand Down Expand Up @@ -148,6 +150,11 @@ class ArcEnvironment {
this.prompts.listen();
}

_initializeThemes() {
this.themes = new ThemeManager(this);
this.themes.listen();
}

/**
* Handler for settings change.
* @param {String} name Changed property name
Expand Down Expand Up @@ -242,5 +249,28 @@ class ArcEnvironment {
log.debug('Opening external URL: ' + url);
shell.openExternal(url);
}

allClosedHandler() {
log.debug('All windows are now closed.');
if (process.platform !== 'darwin') {
log.debug('Quiting main thread.');
app.quit();
} else {
log.debug('Keeping main thread running.');
}
}

/**
* On OS X it's common to re-create a window in the app when the
* dock icon is clicked and there are no other windows open.
*/
activateHandler() {
log.debug('Activating window.');
if (!this.wm.hasWindow) {
this.wm.open();
} else {
this.wm.restoreLast();
}
}
}
module.exports.ArcEnvironment = ArcEnvironment;
27 changes: 25 additions & 2 deletions scripts/main/arc-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ArcPaths {
}
}
if (!this._settingsFile) {
this._settingsFile = path.join(app.getPath('userData'), 'settings.json');
this._settingsFile = path.join(process.env.ARC_HOME, 'settings.json');
}
process.env.ARC_SETTINGS_FILE = this._settingsFile;
}
Expand Down Expand Up @@ -90,7 +90,7 @@ class ArcPaths {
console.warn(`Insufficient permission to themes installation location "${themesPath}".`);
}
}
if (!this._themesSettings) {
if (!this._themesBasePath) {
this._themesBasePath = path.join(process.env.ARC_HOME, 'themes');
}
if (!themesSettingsFile) {
Expand Down Expand Up @@ -119,6 +119,25 @@ class ArcPaths {
}
process.env.ARC_WORKSPACE_PATH = this._workspacePath;
}

get componentsPath() {
return this._componentsPath;
}

setComponentsPath(componentsPath) {
if (componentsPath) {
componentsPath = this._resolvePath(componentsPath);
try {
this._componentsPath = componentsPath;
} catch (_) {
console.warn(`Insufficient permission to components installation location "${componentsPath}".`);
}
}
if (!this._componentsPath) {
this._componentsPath = path.join(process.env.ARC_HOME, 'components');
}
process.env.ARC_COMPONENTS_PATH = this._componentsPath;
}
}

const paths = new ArcPaths();
Expand All @@ -128,6 +147,7 @@ module.exports = {
setThemesPath: (themesPath, themesSettingsFil) => paths.setThemesPath(themesPath, themesSettingsFil),
setHome: () => paths.setHome(),
setWorkspacePath: (workspacePath) => paths.setWorkspacePath(workspacePath),
setComponentsPath: (componentsPath) => paths.setComponentsPath(componentsPath),
get workspacePath() {
return paths.workspacePath;
},
Expand All @@ -139,5 +159,8 @@ module.exports = {
},
get settingsFile() {
return paths.settingsFile;
},
get componentsPath() {
return paths.componentsPath;
}
};
9 changes: 3 additions & 6 deletions scripts/main/components-protocol.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {session, app} = require('electron');
const {session} = require('electron');
const path = require('path');
const log = require('./logger');
const fs = require('fs-extra');
Expand Down Expand Up @@ -29,7 +29,7 @@ class ComponentsProtocolHandler {
* Base path to the themes folder.
* @type {String}
*/
this.modulesBasePath = path.join(app.getPath('userData'), 'modules');
this.componentsBasePath = process.env.ARC_COMPONENTS_PATH;
/**
* Base path to the themes folder.
* @type {String}
Expand Down Expand Up @@ -62,7 +62,7 @@ class ComponentsProtocolHandler {
log.debug('Handling component url: ' + url);
const paths = [
path.join(this.basePath, url),
path.join(this.modulesBasePath, url),
path.join(this.componentsBasePath, url),
url
];
let file;
Expand All @@ -85,9 +85,6 @@ class ComponentsProtocolHandler {
const mimeType = mime.lookup(location) || 'application/octet-stream';
return fs.readFile(location, 'utf8')
.then((data) => {
console.log('SENDING DATA');
console.log(mimeType);
console.log(location);
callback({
data,
mimeType,
Expand Down
Loading

0 comments on commit dba5c85

Please sign in to comment.