From c04f9a7fff727bb04a4aa3a0fa05fd5cd8e795a6 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Thu, 9 Jan 2020 03:22:19 -0500 Subject: [PATCH] Fixed require resolution for CLI scripts. --- packages/cli/src.ts/bin/ethers.ts | 16 +++++++++++----- packages/cli/src.ts/cli.ts | 22 ++++++++++++++-------- packages/cli/tsconfig.json | 1 + 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/cli/src.ts/bin/ethers.ts b/packages/cli/src.ts/bin/ethers.ts index 80d42d9845..9d375a186a 100644 --- a/packages/cli/src.ts/bin/ethers.ts +++ b/packages/cli/src.ts/bin/ethers.ts @@ -3,6 +3,8 @@ "use strict"; import fs from "fs"; +import _module from "module"; +import { dirname, resolve } from "path"; import REPL from "repl"; import util from "util"; import vm from "vm"; @@ -13,13 +15,17 @@ import { ArgParser, CLI, dump, Help, Plugin } from "../cli"; import { getPassword, getProgressBar } from "../prompt"; import { compile } from "../solc"; -function setupContext(context: any, plugin: Plugin) { +function setupContext(path: string, context: any, plugin: Plugin) { context.provider = plugin.provider; context.accounts = plugin.accounts; + if (!context.__filename) { context.__filename = path; } + if (!context.__dirname) { context.__dirname = dirname(path); } if (!context.console) { context.console = console; } - if (!context.require) { context.require = require; } + if (!context.require) { + context.require = _module.createRequireFromPath(path); + } if (!context.process) { context.process = process; } context.ethers = ethers; @@ -124,7 +130,7 @@ class SandboxPlugin extends Plugin { prompt: (this.provider ? this.network.name: "no-network") + "> ", writer: promiseWriter }); - setupContext(repl.context, this); + setupContext(resolve(process.cwd(), "./sandbox.js"), repl.context, this); return new Promise((resolve) => { repl.on("exit", function() { @@ -492,7 +498,7 @@ class EvalPlugin extends Plugin { async run(): Promise { let contextObject = { }; - setupContext(contextObject, this); + setupContext(resolve(process.cwd(), "./sandbox.js"), contextObject, this); let context = vm.createContext(contextObject); let script = new vm.Script(this.code, { filename: "-" }); @@ -530,7 +536,7 @@ class RunPlugin extends Plugin { async run(): Promise { let contextObject = { }; - setupContext(contextObject, this); + setupContext(resolve(this.filename), contextObject, this); let context = vm.createContext(contextObject); let script = new vm.Script(fs.readFileSync(this.filename).toString(), { filename: this.filename }); diff --git a/packages/cli/src.ts/cli.ts b/packages/cli/src.ts/cli.ts index a6d5bba25f..2baf24677c 100644 --- a/packages/cli/src.ts/cli.ts +++ b/packages/cli/src.ts/cli.ts @@ -395,13 +395,13 @@ async function loadAccount(arg: string, plugin: Plugin, preventFile?: boolean): // Secure entry; use prompt with mask if (arg === "-") { - let content = await getPassword("Private Key / Mnemonic:"); + const content = await getPassword("Private Key / Mnemonic:"); return loadAccount(content, plugin, true); } // Raw private key if (ethers.utils.isHexString(arg, 32)) { - let signer = new ethers.Wallet(arg, plugin.provider) + const signer = new ethers.Wallet(arg, plugin.provider); return Promise.resolve(new WrappedSigner(signer.getAddress(), () => Promise.resolve(signer), plugin)); } @@ -512,7 +512,7 @@ export abstract class Plugin { return [ ]; } - async prepareOptions(argParser: ArgParser): Promise { + async prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise { let runners: Array> = [ ]; this.wait = argParser.consumeFlag("wait"); @@ -573,6 +573,8 @@ export abstract class Plugin { let account = accountOptions[i]; switch (account.name) { case "account": + // Verifying does not need to ask for passwords, etc. + if (verifyOnly) { break; } let wrappedSigner = await loadAccount(account.value, this); accounts.push(wrappedSigner); break; @@ -612,21 +614,21 @@ export abstract class Plugin { ///////////////////// // Transaction Options - let gasPrice = argParser.consumeOption("gas-price"); + const gasPrice = argParser.consumeOption("gas-price"); if (gasPrice) { ethers.utils.defineReadOnly(this, "gasPrice", ethers.utils.parseUnits(gasPrice, "gwei")); } else { ethers.utils.defineReadOnly(this, "gasPrice", null); } - let gasLimit = argParser.consumeOption("gas-limit"); + const gasLimit = argParser.consumeOption("gas-limit"); if (gasLimit) { ethers.utils.defineReadOnly(this, "gasLimit", ethers.BigNumber.from(gasLimit)); } else { ethers.utils.defineReadOnly(this, "gasLimit", null); } - let nonce = argParser.consumeOption("nonce"); + const nonce = argParser.consumeOption("nonce"); if (nonce) { this.nonce = ethers.BigNumber.from(nonce).toNumber(); } @@ -693,7 +695,11 @@ export abstract class Plugin { } } -class CheckPlugin extends Plugin { } +class CheckPlugin extends Plugin { + prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise { + return super.prepareOptions(argParser, true); + } +} ///////////////////////////// @@ -941,7 +947,7 @@ export class CLI { return this.showUsage(); } - let debug = argParser.consumeFlag("debug"); + const debug = argParser.consumeFlag("debug"); // Create Plug-in instance let plugin: Plugin = null; diff --git a/packages/cli/tsconfig.json b/packages/cli/tsconfig.json index d48832e0c8..c011c05188 100644 --- a/packages/cli/tsconfig.json +++ b/packages/cli/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../../tsconfig.package.json", "compilerOptions": { + "esModuleInterop": true, "rootDir": "./src.ts", "outDir": "./lib/" },