Browse Source

use @electron/remote

Eugene Pankov 4 years ago
parent
commit
bd46b08c9d

+ 4 - 0
app/lib/app.ts

@@ -1,5 +1,7 @@
 import { app, ipcMain, Menu, Tray, shell, screen, globalShortcut, MenuItemConstructorOptions } from 'electron'
 import * as promiseIpc from 'electron-promise-ipc'
+import * as remote from '@electron/remote/main'
+
 import { loadConfig } from './config'
 import { Window, WindowOptions } from './window'
 import { pluginManager } from './pluginManager'
@@ -9,6 +11,8 @@ export class Application {
     private windows: Window[] = []
 
     constructor () {
+        remote.initialize()
+
         ipcMain.on('app:config-change', (_event, config) => {
             this.broadcast('host:config-change', config)
         })

+ 3 - 11
app/lib/portable.ts

@@ -1,12 +1,8 @@
 import * as path from 'path'
 import * as fs from 'fs'
+import * as electron from 'electron'
 
-let appPath: string | null = null
-try {
-    appPath = path.dirname(require('electron').app.getPath('exe'))
-} catch {
-    appPath = path.dirname(require('electron').remote.app.getPath('exe'))
-}
+const appPath = path.dirname(electron.app.getPath('exe'))
 
 if (fs.existsSync(path.join(appPath, 'terminus-data'))) {
     fs.renameSync(path.join(appPath, 'terminus-data'), path.join(appPath, 'data'))
@@ -14,9 +10,5 @@ if (fs.existsSync(path.join(appPath, 'terminus-data'))) {
 const portableData = path.join(appPath, 'data')
 if (fs.existsSync(portableData)) {
     console.log('reset user data to ' + portableData)
-    try {
-        require('electron').app.setPath('userData', portableData)
-    } catch {
-        require('electron').remote.app.setPath('userData', portableData)
-    }
+    electron.app.setPath('userData', portableData)
 }

+ 2 - 4
app/lib/sentry.ts

@@ -1,16 +1,14 @@
 const { init } = String(process.type) === 'main' ? require('@sentry/electron/dist/main') : require('@sentry/electron/dist/renderer')
-import * as isDev from 'electron-is-dev'
-
 
 const SENTRY_DSN = 'https://[email protected]/181876'
 let release = null
 try {
     release = require('electron').app.getVersion()
 } catch {
-    release = require('electron').remote.app.getVersion()
+    release = require('@electron/remote').app.getVersion()
 }
 
-if (!isDev) {
+if (!process.env.TERMINUS_DEV) {
     init({
         dsn: SENTRY_DSN,
         release,

+ 1 - 1
app/package.json

@@ -21,11 +21,11 @@
     "@angular/forms": "^11.1.1",
     "@angular/platform-browser": "^11.1.1",
     "@angular/platform-browser-dynamic": "^11.1.1",
+    "@electron/remote": "^1.0.4",
     "@ng-bootstrap/ng-bootstrap": "^7.0.0",
     "@terminus-term/node-pty": "0.10.0-terminus.3",
     "electron-config": "2.0.0",
     "electron-debug": "^3.2.0",
-    "electron-is-dev": "1.2.0",
     "electron-promise-ipc": "^2.2.4",
     "fontmanager-redux": "1.0.0",
     "glasstron": "0.0.7",

+ 2 - 4
app/src/entry.ts

@@ -2,8 +2,6 @@ import 'zone.js'
 import 'core-js/proposals/reflect-metadata'
 import 'rxjs'
 
-import * as isDev from 'electron-is-dev'
-
 import './global.scss'
 import './toastr.scss'
 
@@ -23,7 +21,7 @@ if (process.platform === 'win32' && !('HOME' in process.env)) {
     process.env.HOME = `${process.env.HOMEDRIVE}${process.env.HOMEPATH}`
 }
 
-if (isDev) {
+if (process.env.TERMINUS_DEV) {
     console.warn('Running in debug mode')
 } else {
     enableProdMode()
@@ -39,7 +37,7 @@ async function bootstrap (plugins: PluginInfo[], safeMode = false): Promise<NgMo
     const module = getRootModule(pluginsModules)
     window['rootModule'] = module
     return platformBrowserDynamic().bootstrapModule(module).then(moduleRef => {
-        if (isDev) {
+        if (process.env.TERMINUS_DEV) {
             const applicationRef = moduleRef.injector.get(ApplicationRef)
             const componentRef = applicationRef.components[0]
             enableDebugTools(componentRef)

+ 4 - 3
app/src/plugins.ts

@@ -1,5 +1,6 @@
 import * as fs from 'mz/fs'
 import * as path from 'path'
+import * as remote from '@electron/remote'
 const nodeModule = require('module') // eslint-disable-line @typescript-eslint/no-var-requires
 const nodeRequire = (global as any).require
 
@@ -15,13 +16,13 @@ function normalizePath (p: string): string {
 global['module'].paths.map((x: string) => nodeModule.globalPaths.push(normalizePath(x)))
 
 if (process.env.TERMINUS_DEV) {
-    nodeModule.globalPaths.unshift(path.dirname(require('electron').remote.app.getAppPath()))
+    nodeModule.globalPaths.unshift(path.dirname(remote.app.getAppPath()))
 }
 
-const builtinPluginsPath = process.env.TERMINUS_DEV ? path.dirname(require('electron').remote.app.getAppPath()) : path.join((process as any).resourcesPath, 'builtin-plugins')
+const builtinPluginsPath = process.env.TERMINUS_DEV ? path.dirname(remote.app.getAppPath()) : path.join((process as any).resourcesPath, 'builtin-plugins')
 
 const userPluginsPath = path.join(
-    require('electron').remote.app.getPath('userData'),
+    remote.app.getPath('userData'),
     'plugins',
 )
 

+ 0 - 1
app/webpack.config.js

@@ -68,7 +68,6 @@ module.exports = {
     '@ng-bootstrap/ng-bootstrap': 'commonjs @ng-bootstrap/ng-bootstrap',
     child_process: 'commonjs child_process',
     electron: 'commonjs electron',
-    'electron-is-dev': 'commonjs electron-is-dev',
     fs: 'commonjs fs',
     'ngx-toastr': 'commonjs ngx-toastr',
     module: 'commonjs module',

+ 5 - 0
app/yarn.lock

@@ -51,6 +51,11 @@
   dependencies:
     tslib "^2.0.0"
 
+"@electron/remote@^1.0.4":
+  version "1.0.4"
+  resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-1.0.4.tgz#f1c8cf3560bab762b462bfae9991919cced8bc33"
+  integrity sha512-kguDJRhL3ZynHrkbX8Tr7xoAzGsNgh4eqXkycXb6cgXbOgehGqkBVe+MnjSVMXz3QJykerGKPy28gqcM7AFGYw==
+
 "@iarna/cli@^1.2.0":
   version "1.2.0"
   resolved "https://registry.yarnpkg.com/@iarna/cli/-/cli-1.2.0.tgz#0f7af5e851afe895104583c4ca07377a8094d641"

+ 1 - 0
terminus-core/package.json

@@ -17,6 +17,7 @@
   "author": "Eugene Pankov",
   "license": "MIT",
   "devDependencies": {
+    "@electron/remote": "^1.0.4",
     "@types/js-yaml": "^4.0.0",
     "@types/shell-escape": "^0.2.0",
     "@types/winston": "^2.3.6",

+ 1 - 1
terminus-core/src/components/tabHeader.component.ts

@@ -105,7 +105,7 @@ export class TabHeaderComponent {
         if ($event.which === 3) {
             $event.preventDefault()
 
-            const contextMenu = this.electron.remote.Menu.buildFromTemplate(await this.buildContextMenu())
+            const contextMenu = this.electron.Menu.buildFromTemplate(await this.buildContextMenu())
 
             contextMenu.popup({
                 x: $event.pageX,

+ 18 - 16
terminus-core/src/services/electron.service.ts

@@ -1,5 +1,6 @@
 import { Injectable } from '@angular/core'
 import { App, IpcRenderer, Shell, Dialog, Clipboard, GlobalShortcut, Screen, Remote, AutoUpdater, TouchBar, BrowserWindow, Menu, MenuItem, NativeImage, MessageBoxOptions } from 'electron'
+import * as remote from '@electron/remote'
 
 export interface MessageBoxResponse {
     response: number
@@ -17,30 +18,31 @@ export class ElectronService {
     nativeImage: typeof NativeImage
     screen: Screen
     remote: Remote
+    process: any
     autoUpdater: AutoUpdater
     TouchBar: typeof TouchBar
     BrowserWindow: typeof BrowserWindow
     Menu: typeof Menu
     MenuItem: typeof MenuItem
-    private electron: any
 
     /** @hidden */
     private constructor () {
-        this.electron = require('electron')
-        this.remote = this.electron.remote
-        this.app = this.remote.app
-        this.screen = this.remote.screen
-        this.dialog = this.remote.dialog
-        this.shell = this.electron.shell
-        this.clipboard = this.electron.clipboard
-        this.ipcRenderer = this.electron.ipcRenderer
-        this.globalShortcut = this.remote.globalShortcut
-        this.nativeImage = this.remote.nativeImage
-        this.autoUpdater = this.remote.autoUpdater
-        this.TouchBar = this.remote.TouchBar
-        this.BrowserWindow = this.remote.BrowserWindow
-        this.Menu = this.remote.Menu
-        this.MenuItem = this.remote.MenuItem
+        const electron = require('electron')
+        this.shell = electron.shell
+        this.clipboard = electron.clipboard
+        this.ipcRenderer = electron.ipcRenderer
+
+        this.process = remote.process
+        this.app = remote.app
+        this.screen = remote.screen
+        this.dialog = remote.dialog
+        this.globalShortcut = remote.globalShortcut
+        this.nativeImage = remote.nativeImage
+        this.autoUpdater = remote.autoUpdater
+        this.TouchBar = remote.TouchBar
+        this.BrowserWindow = remote.BrowserWindow
+        this.Menu = remote.Menu
+        this.MenuItem = remote.MenuItem
     }
 
     async showMessageBox (

+ 5 - 0
terminus-core/yarn.lock

@@ -11,6 +11,11 @@
     enabled "2.0.x"
     kuler "^2.0.0"
 
+"@electron/remote@^1.0.4":
+  version "1.0.4"
+  resolved "https://registry.yarnpkg.com/@electron/remote/-/remote-1.0.4.tgz#f1c8cf3560bab762b462bfae9991919cced8bc33"
+  integrity sha512-kguDJRhL3ZynHrkbX8Tr7xoAzGsNgh4eqXkycXb6cgXbOgehGqkBVe+MnjSVMXz3QJykerGKPy28gqcM7AFGYw==
+
 "@types/js-yaml@^4.0.0":
   version "4.0.0"
   resolved "https://registry.yarnpkg.com/@types/js-yaml/-/js-yaml-4.0.0.tgz#d1a11688112091f2c711674df3a65ea2f47b5dfb"

+ 3 - 3
terminus-terminal/src/buttonProvider.ts

@@ -15,10 +15,10 @@ export class ButtonProvider extends ToolbarButtonProvider {
         private terminal: TerminalService,
     ) {
         super()
-        if (!electron.remote.process.env.TERMINUS_DEV) {
+        if (!electron.process.env.TERMINUS_DEV) {
             setImmediate(async () => {
-                const argv: string[] = electron.remote.process.argv
-                for (const arg of argv.slice(1).concat([electron.remote.process.argv0])) {
+                const argv: string[] = electron.process.argv
+                for (const arg of argv.slice(1).concat([electron.process.argv0])) {
                     if (await fs.exists(arg)) {
                         if ((await fs.stat(arg)).isDirectory()) {
                             this.terminal.openTab(undefined, arg)

+ 2 - 2
terminus-terminal/src/frontends/xtermFrontend.ts

@@ -175,12 +175,12 @@ export class XTermFrontend extends Frontend {
     copySelection (): void {
         const text = this.getSelection()
         if (text.length < 1024 * 32) {
-            require('electron').remote.clipboard.write({
+            require('@electron/remote').clipboard.write({
                 text: this.getSelection(),
                 html: this.getSelectionAsHTML(),
             })
         } else {
-            require('electron').remote.clipboard.write({
+            require('@electron/remote').clipboard.write({
                 text: this.getSelection(),
             })
         }

+ 3 - 3
terminus-terminal/src/index.ts

@@ -6,7 +6,7 @@ import { FormsModule } from '@angular/forms'
 import { NgbModule } from '@ng-bootstrap/ng-bootstrap'
 import { ToastrModule } from 'ngx-toastr'
 
-import TerminusCorePlugin, { HostAppService, ToolbarButtonProvider, TabRecoveryProvider, ConfigProvider, HotkeysService, HotkeyProvider, AppService, ConfigService, TabContextMenuItemProvider } from 'terminus-core'
+import TerminusCorePlugin, { HostAppService, ToolbarButtonProvider, TabRecoveryProvider, ConfigProvider, HotkeysService, HotkeyProvider, AppService, ConfigService, TabContextMenuItemProvider, ElectronService } from 'terminus-core'
 import { SettingsTabProvider } from 'terminus-settings'
 
 import { AppearanceSettingsTabComponent } from './components/appearanceSettingsTab.component'
@@ -139,6 +139,7 @@ export default class TerminalModule { // eslint-disable-line @typescript-eslint/
         terminal: TerminalService,
         hostApp: HostAppService,
         dockMenu: DockMenuService,
+        electron: ElectronService,
     ) {
         const events = [
             {
@@ -165,8 +166,7 @@ export default class TerminalModule { // eslint-disable-line @typescript-eslint/
             }
         })
         if (config.store.terminal.autoOpen) {
-
-            let argv = require('electron').remote.process.argv
+            let argv = electron.process.argv
             if (argv[0].includes('node')) {
                 argv = argv.slice(1)
             }

+ 4 - 1
webpack.plugin.config.js

@@ -41,7 +41,10 @@ module.exports = options => {
                             ],
                             paths: {
                                 'terminus-*': [path.resolve(options.dirname, '../terminus-*')],
-                                '*': [path.resolve(options.dirname, '../app/node_modules/*')],
+                                '*': [
+                                    path.resolve(options.dirname, '../app/node_modules/*'),
+                                    path.resolve(options.dirname, '../node_modules/*'),
+                                ],
                             },
                         },
                     },