refactor: convert more files to typescript (#16820)
This commit is contained in:
parent
cfbdc40814
commit
01c442de64
16 changed files with 169 additions and 90 deletions
|
@ -10,6 +10,11 @@ common.defineProperties(exports)
|
|||
for (const module of moduleList) {
|
||||
Object.defineProperty(exports, module.name, {
|
||||
enumerable: !module.private,
|
||||
get: common.memoizedGetter(() => require(`@electron/internal/browser/api/${module.file}.js`))
|
||||
get: common.memoizedGetter(() => {
|
||||
const value = require(`@electron/internal/browser/api/${module.file}.js`)
|
||||
// Handle Typescript modules with an "export default X" statement
|
||||
if (value.__esModule) return value.default
|
||||
return value
|
||||
})
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,29 +1,29 @@
|
|||
'use strict'
|
||||
|
||||
const { app, session } = require('electron')
|
||||
import { app, session } from 'electron'
|
||||
|
||||
// Global protocol APIs.
|
||||
module.exports = process.atomBinding('protocol')
|
||||
const protocol = process.atomBinding('protocol')
|
||||
|
||||
// Fallback protocol APIs of default session.
|
||||
Object.setPrototypeOf(module.exports, new Proxy({}, {
|
||||
Object.setPrototypeOf(protocol, new Proxy({}, {
|
||||
get (target, property) {
|
||||
if (!app.isReady()) return
|
||||
|
||||
const protocol = session.defaultSession.protocol
|
||||
const protocol = session.defaultSession!.protocol
|
||||
if (!Object.getPrototypeOf(protocol).hasOwnProperty(property)) return
|
||||
|
||||
// Returning a native function directly would throw error.
|
||||
return (...args) => protocol[property](...args)
|
||||
return (...args: any[]) => (protocol[property as keyof Electron.Protocol] as Function)(...args)
|
||||
},
|
||||
|
||||
ownKeys () {
|
||||
if (!app.isReady()) return []
|
||||
|
||||
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession.protocol))
|
||||
return Object.getOwnPropertyNames(Object.getPrototypeOf(session.defaultSession!.protocol))
|
||||
},
|
||||
|
||||
getOwnPropertyDescriptor (target) {
|
||||
return { configurable: true, enumerable: true }
|
||||
}
|
||||
}))
|
||||
|
||||
export default protocol
|
|
@ -1,14 +1,13 @@
|
|||
'use strict'
|
||||
import { shell, Menu } from 'electron'
|
||||
|
||||
const { shell, Menu } = require('electron')
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
|
||||
const isMac = process.platform === 'darwin'
|
||||
|
||||
const setDefaultApplicationMenu = () => {
|
||||
if (v8Util.getHiddenValue(global, 'applicationMenuSet')) return
|
||||
export const setDefaultApplicationMenu = () => {
|
||||
if (v8Util.getHiddenValue<boolean>(global, 'applicationMenuSet')) return
|
||||
|
||||
const helpMenu = {
|
||||
const helpMenu: Electron.MenuItemConstructorOptions = {
|
||||
role: 'help',
|
||||
submenu: [
|
||||
{
|
||||
|
@ -40,8 +39,9 @@ const setDefaultApplicationMenu = () => {
|
|||
]
|
||||
}
|
||||
|
||||
const template = [
|
||||
...(isMac ? [{ role: 'appMenu' }] : []),
|
||||
const macAppMenu: Electron.MenuItemConstructorOptions = { role: 'appMenu' }
|
||||
const template: Electron.MenuItemConstructorOptions[] = [
|
||||
...(isMac ? [macAppMenu] : []),
|
||||
{ role: 'fileMenu' },
|
||||
{ role: 'editMenu' },
|
||||
{ role: 'viewMenu' },
|
||||
|
@ -52,7 +52,3 @@ const setDefaultApplicationMenu = () => {
|
|||
const menu = Menu.buildFromTemplate(template)
|
||||
Menu.setApplicationMenu(menu)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
setDefaultApplicationMenu
|
||||
}
|
|
@ -49,10 +49,15 @@ process.on('uncaughtException', function (error) {
|
|||
}
|
||||
|
||||
// Show error in GUI.
|
||||
const dialog = require('electron').dialog
|
||||
const stack = error.stack ? error.stack : `${error.name}: ${error.message}`
|
||||
const message = 'Uncaught Exception:\n' + stack
|
||||
dialog.showErrorBox('A JavaScript error occurred in the main process', message)
|
||||
// We can't import { dialog } at the top of this file as this file is
|
||||
// responsible for setting up the require hook for the "electron" module
|
||||
// so we import it inside the handler down here
|
||||
import('electron')
|
||||
.then(({ dialog }) => {
|
||||
const stack = error.stack ? error.stack : `${error.name}: ${error.message}`
|
||||
const message = 'Uncaught Exception:\n' + stack
|
||||
dialog.showErrorBox('A JavaScript error occurred in the main process', message)
|
||||
})
|
||||
})
|
||||
|
||||
// Emit 'exit' event on quit.
|
||||
|
@ -195,10 +200,13 @@ app.on('window-all-closed', () => {
|
|||
}
|
||||
})
|
||||
|
||||
const { setDefaultApplicationMenu } = require('@electron/internal/browser/default-menu')
|
||||
|
||||
// Create default menu.
|
||||
app.once('ready', setDefaultApplicationMenu)
|
||||
Promise.all([
|
||||
import('@electron/internal/browser/default-menu'),
|
||||
app.whenReady
|
||||
]).then(([{ setDefaultApplicationMenu }]) => {
|
||||
// Create default menu
|
||||
setDefaultApplicationMenu()
|
||||
})
|
||||
|
||||
if (packagePath) {
|
||||
// Finally load app's main.js and transfer control to C++.
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
'use strict'
|
||||
|
||||
module.exports = function atomBindingSetup (binding, processType) {
|
||||
return function atomBinding (name) {
|
||||
export function atomBindingSetup (binding: typeof process['binding'], processType: typeof process['type']): typeof process['atomBinding'] {
|
||||
return function atomBinding (name: string) {
|
||||
try {
|
||||
return binding(`atom_${processType}_${name}`)
|
||||
} catch (error) {
|
|
@ -1,9 +1,11 @@
|
|||
'use strict'
|
||||
import * as timers from 'timers'
|
||||
import * as util from 'util'
|
||||
|
||||
const timers = require('timers')
|
||||
const util = require('util')
|
||||
import { atomBindingSetup } from '@electron/internal/common/atom-binding-setup'
|
||||
|
||||
process.atomBinding = require('@electron/internal/common/atom-binding-setup')(process.binding, process.type)
|
||||
process.atomBinding = atomBindingSetup(process.binding, process.type)
|
||||
|
||||
type AnyFn = (...args: any[]) => any
|
||||
|
||||
// setImmediate and process.nextTick makes use of uv_check and uv_prepare to
|
||||
// run the callbacks, however since we only run uv loop on requests, the
|
||||
|
@ -11,19 +13,25 @@ process.atomBinding = require('@electron/internal/common/atom-binding-setup')(pr
|
|||
// which would delay the callbacks for arbitrary long time. So we should
|
||||
// initiatively activate the uv loop once setImmediate and process.nextTick is
|
||||
// called.
|
||||
const wrapWithActivateUvLoop = function (func) {
|
||||
const wrapWithActivateUvLoop = function <T extends AnyFn> (func: T): T {
|
||||
return wrap(func, function (func) {
|
||||
return function () {
|
||||
return function (this: any, ...args: any[]) {
|
||||
process.activateUvLoop()
|
||||
return func.apply(this, arguments)
|
||||
return func.apply(this, args)
|
||||
}
|
||||
})
|
||||
}) as T
|
||||
}
|
||||
|
||||
function wrap (func, wrapper) {
|
||||
/**
|
||||
* Casts to any below for func are due to Typescript not supporting symbols
|
||||
* in index signatures
|
||||
*
|
||||
* Refs: https://github.com/Microsoft/TypeScript/issues/1863
|
||||
*/
|
||||
function wrap <T extends AnyFn> (func: T, wrapper: (fn: AnyFn) => T) {
|
||||
const wrapped = wrapper(func)
|
||||
if (func[util.promisify.custom]) {
|
||||
wrapped[util.promisify.custom] = wrapper(func[util.promisify.custom])
|
||||
if ((func as any)[util.promisify.custom]) {
|
||||
(wrapped as any)[util.promisify.custom] = wrapper((func as any)[util.promisify.custom])
|
||||
}
|
||||
return wrapped
|
||||
}
|
||||
|
@ -47,7 +55,11 @@ if (process.platform === 'win32') {
|
|||
const { Readable } = require('stream')
|
||||
const stdin = new Readable()
|
||||
stdin.push(null)
|
||||
process.__defineGetter__('stdin', function () {
|
||||
return stdin
|
||||
Object.defineProperty(process, 'stdin', {
|
||||
configurable: false,
|
||||
enumerable: true,
|
||||
get () {
|
||||
return stdin
|
||||
}
|
||||
})
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
'use strict'
|
||||
import * as path from 'path'
|
||||
|
||||
const path = require('path')
|
||||
const Module = require('module')
|
||||
|
||||
// Clear Node's global search paths.
|
||||
|
@ -8,13 +7,13 @@ Module.globalPaths.length = 0
|
|||
|
||||
// Clear current and parent(init.js)'s search paths.
|
||||
module.paths = []
|
||||
module.parent.paths = []
|
||||
module.parent!.paths = []
|
||||
|
||||
// Prevent Node from adding paths outside this app to search paths.
|
||||
const resourcesPathWithTrailingSlash = process.resourcesPath + path.sep
|
||||
const originalNodeModulePaths = Module._nodeModulePaths
|
||||
Module._nodeModulePaths = function (from) {
|
||||
const paths = originalNodeModulePaths(from)
|
||||
Module._nodeModulePaths = function (from: string) {
|
||||
const paths: string[] = originalNodeModulePaths(from)
|
||||
const fromPath = path.resolve(from) + path.sep
|
||||
// If "from" is outside the app then we do nothing.
|
||||
if (fromPath.startsWith(resourcesPathWithTrailingSlash)) {
|
||||
|
@ -31,9 +30,9 @@ const INTERNAL_MODULE_PREFIX = '@electron/internal/'
|
|||
|
||||
// Patch Module._resolveFilename to always require the Electron API when
|
||||
// require('electron') is done.
|
||||
const electronPath = path.join(__dirname, '..', process.type, 'api', 'exports', 'electron.js')
|
||||
const electronPath = path.join(__dirname, '..', process.type!, 'api', 'exports', 'electron.js')
|
||||
const originalResolveFilename = Module._resolveFilename
|
||||
Module._resolveFilename = function (request, parent, isMain) {
|
||||
Module._resolveFilename = function (request: string, parent: NodeModule, isMain: boolean) {
|
||||
if (request === 'electron') {
|
||||
return electronPath
|
||||
} else if (request.startsWith(INTERNAL_MODULE_PREFIX) && request.length > INTERNAL_MODULE_PREFIX.length) {
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
/* global nodeProcess, isolatedWorld */
|
||||
|
||||
const atomBinding = require('@electron/internal/common/atom-binding-setup')(nodeProcess.binding, 'renderer')
|
||||
const atomBinding = require('@electron/internal/common/atom-binding-setup').atomBindingSetup(nodeProcess.binding, 'renderer')
|
||||
|
||||
const v8Util = atomBinding('v8_util')
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
const events = require('events')
|
||||
const { EventEmitter } = events
|
||||
|
||||
process.atomBinding = require('@electron/internal/common/atom-binding-setup')(binding.get, 'renderer')
|
||||
process.atomBinding = require('@electron/internal/common/atom-binding-setup').atomBindingSetup(binding.get, 'renderer')
|
||||
|
||||
const v8Util = process.atomBinding('v8_util')
|
||||
// Expose browserify Buffer as a hidden value. This is used by C++ code to
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue