Fixed require resolution for CLI scripts.

This commit is contained in:
Richard Moore 2020-01-09 03:22:19 -05:00
parent 3c184ace21
commit c04f9a7fff
No known key found for this signature in database
GPG Key ID: 665176BE8E9DC651
3 changed files with 26 additions and 13 deletions

View File

@ -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<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: "-" });
@ -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 });

View File

@ -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<void> {
async prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise<void> {
let runners: Array<Promise<void>> = [ ];
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<void> {
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;

View File

@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.package.json",
"compilerOptions": {
"esModuleInterop": true,
"rootDir": "./src.ts",
"outDir": "./lib/"
},