@warren-bank/dapp-console
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -9,2 +9,3 @@ #! /usr/bin/env node | ||
const timer = require('timers') | ||
const path = require('path') | ||
@@ -154,8 +155,39 @@ const argv = yargs | ||
const run_script = function(script){ | ||
var result | ||
const run_script = function(script, fpath){ | ||
var fcontext, result | ||
// wrap 'script' code inside an anonymous self-invoking function. | ||
// by doing so, the `return` keyword can be used to pass a `Promise` to 'result'. | ||
// otherwise, the final statement in the block of 'script' code would need to evaluate to the `Promise`, | ||
// which, imho, is a coding style that's restrictive and awkward to use. | ||
script = '(function(){' + "\n" + script + "\n" + '})()' | ||
fcontext = {} | ||
if (fpath){ | ||
// for scripts that are read from an input file, | ||
// include additional filesystem-related context variables. | ||
fcontext = (function(){ | ||
var $cwd, $realpath, $dirname, $filename | ||
$cwd = process.cwd() | ||
if (path.isAbsolute(fpath)){ | ||
$realpath = fpath | ||
} | ||
else { | ||
$realpath = $cwd + '/' + fpath | ||
$realpath = path.normalize($realpath) | ||
} | ||
$dirname = path.dirname($realpath) | ||
$filename = path.basename($realpath) | ||
return { | ||
"__cwd": $cwd, | ||
"__realpath": $realpath, | ||
"__dirname": $dirname, | ||
"__filename": $filename | ||
} | ||
})() | ||
} | ||
result = vm.runInNewContext( | ||
script, | ||
Object.assign({}, contract_objects, {web3}, {console, fs, timer}) | ||
Object.assign({}, fcontext, contract_objects, {web3}, {console, path, fs, timer}) | ||
) | ||
@@ -174,3 +206,3 @@ | ||
else if (input_file){ | ||
run_script(fs.readFileSync(input_file).toString()) | ||
run_script(fs.readFileSync(input_file).toString(), input_file) | ||
} | ||
@@ -177,0 +209,0 @@ |
{ | ||
"name": "@warren-bank/dapp-console", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"dependencies": { | ||
@@ -5,0 +5,0 @@ "web3": "^0.19.0", |
28400
194