From abf2e9c93d4a599db737b9bd872d197906254873 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Wed, 24 Jun 2020 00:53:46 -0700 Subject: [PATCH] chore: expose electrons built in modules in the REPL along with nodes (#24249) --- default_app/main.ts | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/default_app/main.ts b/default_app/main.ts index daf7f3142653b..1f2611d06002d 100644 --- a/default_app/main.ts +++ b/default_app/main.ts @@ -168,6 +168,39 @@ function startRepl () { process.exit(0); }); + function defineBuiltin (context: any, name: string, getter: Function) { + const setReal = (val: any) => { + // Deleting the property before re-assigning it disables the + // getter/setter mechanism. + delete context[name]; + context[name] = val; + }; + + Object.defineProperty(context, name, { + get: () => { + const lib = getter(); + + delete context[name]; + Object.defineProperty(context, name, { + get: () => lib, + set: setReal, + configurable: true, + enumerable: false + }); + + return lib; + }, + set: setReal, + configurable: true, + enumerable: false + }); + } + + defineBuiltin(repl.context, 'electron', () => electron); + for (const api of Object.keys(electron) as (keyof typeof electron)[]) { + defineBuiltin(repl.context, api, () => electron[api]); + } + // Copied from node/lib/repl.js. For better DX, we don't want to // show e.g 'contentTracing' at a higher priority than 'const', so // we only trigger custom tab-completion when no common words are