Fixed require resolution for CLI scripts.
This commit is contained in:
parent
3c184ace21
commit
c04f9a7fff
@ -3,6 +3,8 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
|
import _module from "module";
|
||||||
|
import { dirname, resolve } from "path";
|
||||||
import REPL from "repl";
|
import REPL from "repl";
|
||||||
import util from "util";
|
import util from "util";
|
||||||
import vm from "vm";
|
import vm from "vm";
|
||||||
@ -13,13 +15,17 @@ import { ArgParser, CLI, dump, Help, Plugin } from "../cli";
|
|||||||
import { getPassword, getProgressBar } from "../prompt";
|
import { getPassword, getProgressBar } from "../prompt";
|
||||||
import { compile } from "../solc";
|
import { compile } from "../solc";
|
||||||
|
|
||||||
function setupContext(context: any, plugin: Plugin) {
|
function setupContext(path: string, context: any, plugin: Plugin) {
|
||||||
|
|
||||||
context.provider = plugin.provider;
|
context.provider = plugin.provider;
|
||||||
context.accounts = plugin.accounts;
|
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.console) { context.console = console; }
|
||||||
if (!context.require) { context.require = require; }
|
if (!context.require) {
|
||||||
|
context.require = _module.createRequireFromPath(path);
|
||||||
|
}
|
||||||
if (!context.process) { context.process = process; }
|
if (!context.process) { context.process = process; }
|
||||||
|
|
||||||
context.ethers = ethers;
|
context.ethers = ethers;
|
||||||
@ -124,7 +130,7 @@ class SandboxPlugin extends Plugin {
|
|||||||
prompt: (this.provider ? this.network.name: "no-network") + "> ",
|
prompt: (this.provider ? this.network.name: "no-network") + "> ",
|
||||||
writer: promiseWriter
|
writer: promiseWriter
|
||||||
});
|
});
|
||||||
setupContext(repl.context, this);
|
setupContext(resolve(process.cwd(), "./sandbox.js"), repl.context, this);
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
repl.on("exit", function() {
|
repl.on("exit", function() {
|
||||||
@ -492,7 +498,7 @@ class EvalPlugin extends Plugin {
|
|||||||
|
|
||||||
async run(): Promise<void> {
|
async run(): Promise<void> {
|
||||||
let contextObject = { };
|
let contextObject = { };
|
||||||
setupContext(contextObject, this);
|
setupContext(resolve(process.cwd(), "./sandbox.js"), contextObject, this);
|
||||||
|
|
||||||
let context = vm.createContext(contextObject);
|
let context = vm.createContext(contextObject);
|
||||||
let script = new vm.Script(this.code, { filename: "-" });
|
let script = new vm.Script(this.code, { filename: "-" });
|
||||||
@ -530,7 +536,7 @@ class RunPlugin extends Plugin {
|
|||||||
|
|
||||||
async run(): Promise<void> {
|
async run(): Promise<void> {
|
||||||
let contextObject = { };
|
let contextObject = { };
|
||||||
setupContext(contextObject, this);
|
setupContext(resolve(this.filename), contextObject, this);
|
||||||
|
|
||||||
let context = vm.createContext(contextObject);
|
let context = vm.createContext(contextObject);
|
||||||
let script = new vm.Script(fs.readFileSync(this.filename).toString(), { filename: this.filename });
|
let script = new vm.Script(fs.readFileSync(this.filename).toString(), { filename: this.filename });
|
||||||
|
@ -395,13 +395,13 @@ async function loadAccount(arg: string, plugin: Plugin, preventFile?: boolean):
|
|||||||
|
|
||||||
// Secure entry; use prompt with mask
|
// Secure entry; use prompt with mask
|
||||||
if (arg === "-") {
|
if (arg === "-") {
|
||||||
let content = await getPassword("Private Key / Mnemonic:");
|
const content = await getPassword("Private Key / Mnemonic:");
|
||||||
return loadAccount(content, plugin, true);
|
return loadAccount(content, plugin, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Raw private key
|
// Raw private key
|
||||||
if (ethers.utils.isHexString(arg, 32)) {
|
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));
|
return Promise.resolve(new WrappedSigner(signer.getAddress(), () => Promise.resolve(signer), plugin));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -512,7 +512,7 @@ export abstract class Plugin {
|
|||||||
return [ ];
|
return [ ];
|
||||||
}
|
}
|
||||||
|
|
||||||
async prepareOptions(argParser: ArgParser): Promise<void> {
|
async prepareOptions(argParser: ArgParser, verifyOnly?: boolean): Promise<void> {
|
||||||
let runners: Array<Promise<void>> = [ ];
|
let runners: Array<Promise<void>> = [ ];
|
||||||
|
|
||||||
this.wait = argParser.consumeFlag("wait");
|
this.wait = argParser.consumeFlag("wait");
|
||||||
@ -573,6 +573,8 @@ export abstract class Plugin {
|
|||||||
let account = accountOptions[i];
|
let account = accountOptions[i];
|
||||||
switch (account.name) {
|
switch (account.name) {
|
||||||
case "account":
|
case "account":
|
||||||
|
// Verifying does not need to ask for passwords, etc.
|
||||||
|
if (verifyOnly) { break; }
|
||||||
let wrappedSigner = await loadAccount(account.value, this);
|
let wrappedSigner = await loadAccount(account.value, this);
|
||||||
accounts.push(wrappedSigner);
|
accounts.push(wrappedSigner);
|
||||||
break;
|
break;
|
||||||
@ -612,21 +614,21 @@ export abstract class Plugin {
|
|||||||
/////////////////////
|
/////////////////////
|
||||||
// Transaction Options
|
// Transaction Options
|
||||||
|
|
||||||
let gasPrice = argParser.consumeOption("gas-price");
|
const gasPrice = argParser.consumeOption("gas-price");
|
||||||
if (gasPrice) {
|
if (gasPrice) {
|
||||||
ethers.utils.defineReadOnly(this, "gasPrice", ethers.utils.parseUnits(gasPrice, "gwei"));
|
ethers.utils.defineReadOnly(this, "gasPrice", ethers.utils.parseUnits(gasPrice, "gwei"));
|
||||||
} else {
|
} else {
|
||||||
ethers.utils.defineReadOnly(this, "gasPrice", null);
|
ethers.utils.defineReadOnly(this, "gasPrice", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
let gasLimit = argParser.consumeOption("gas-limit");
|
const gasLimit = argParser.consumeOption("gas-limit");
|
||||||
if (gasLimit) {
|
if (gasLimit) {
|
||||||
ethers.utils.defineReadOnly(this, "gasLimit", ethers.BigNumber.from(gasLimit));
|
ethers.utils.defineReadOnly(this, "gasLimit", ethers.BigNumber.from(gasLimit));
|
||||||
} else {
|
} else {
|
||||||
ethers.utils.defineReadOnly(this, "gasLimit", null);
|
ethers.utils.defineReadOnly(this, "gasLimit", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
let nonce = argParser.consumeOption("nonce");
|
const nonce = argParser.consumeOption("nonce");
|
||||||
if (nonce) {
|
if (nonce) {
|
||||||
this.nonce = ethers.BigNumber.from(nonce).toNumber();
|
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();
|
return this.showUsage();
|
||||||
}
|
}
|
||||||
|
|
||||||
let debug = argParser.consumeFlag("debug");
|
const debug = argParser.consumeFlag("debug");
|
||||||
|
|
||||||
// Create Plug-in instance
|
// Create Plug-in instance
|
||||||
let plugin: Plugin = null;
|
let plugin: Plugin = null;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
{
|
{
|
||||||
"extends": "../../tsconfig.package.json",
|
"extends": "../../tsconfig.package.json",
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
|
"esModuleInterop": true,
|
||||||
"rootDir": "./src.ts",
|
"rootDir": "./src.ts",
|
||||||
"outDir": "./lib/"
|
"outDir": "./lib/"
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user