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
|
@ -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) {
|
Loading…
Add table
Add a link
Reference in a new issue