Skip to content

Commit

Permalink
Fixed require resolution for CLI scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Jan 9, 2020
1 parent 3c184ac commit c04f9a7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
16 changes: 11 additions & 5 deletions packages/cli/src.ts/bin/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -492,7 +498,7 @@ class EvalPlugin extends Plugin {

async run(): Promise<void> {
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: "-" });
Expand Down Expand Up @@ -530,7 +536,7 @@ class RunPlugin extends Plugin {

async run(): Promise<void> {
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 });
Expand Down
22 changes: 14 additions & 8 deletions packages/cli/src.ts/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down Expand Up @@ -512,7 +512,7 @@ export abstract class Plugin {
return [ ];
}

async prepareOptions(argParser: ArgParser): Promise<void> {
async prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise<void> {
let runners: Array<Promise<void>> = [ ];

this.wait = argParser.consumeFlag("wait");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -693,7 +695,11 @@ export abstract class Plugin {
}
}

class CheckPlugin extends Plugin { }
class CheckPlugin extends Plugin {
prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise<void> {
return super.prepareOptions(argParser, true);
}
}


/////////////////////////////
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.package.json",
"compilerOptions": {
"esModuleInterop": true,
"rootDir": "./src.ts",
"outDir": "./lib/"
},
Expand Down

0 comments on commit c04f9a7

Please sign in to comment.