-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathtruffleScriptRunnerFactory.js
More file actions
68 lines (63 loc) · 1.94 KB
/
truffleScriptRunnerFactory.js
File metadata and controls
68 lines (63 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @dev Parse colon marked arguments
*
* NOTE:
* Provide arguments to the script through ":" separator
*/
function parseColonArgs(argv) {
const argIndex = argv.indexOf(":");
if (argIndex < 0) {
console.log("No colon arguments");
return [];
} else {
const args = argv.slice(argIndex + 1);
return args;
}
}
/**
* @dev Script runner for logic function
*
* This is development framework dependent, and it's currently for Truffle.
*
* runnerOpts.skipArgv:
* - most scripts supports argv followed by an options, but for compatibility issue
* some script wants to skip the argv. This option enables the hack.
*/
module.exports = function (ctxFn, logicFn, runnerOpts) {
return async function (cb, argv, options = {}) {
try {
const {artifacts, web3} = ctxFn();
let args;
if (runnerOpts.skipArgv) {
// skip argv arguments
options = argv || {};
} else {
// Parse colon indicated arguments
args = parseColonArgs(argv || process.argv);
runnerOpts.doNotPrintColonArgs ||
console.log("Colon arguments", args);
}
// normalize web3 environment
if (!options.web3) {
throw Error(
"A web3 instance is not provided."
);
}
global.web3 = options.web3;
// Use common environment variables
options.protocolReleaseVersion =
options.protocolReleaseVersion ||
process.env.RELEASE_VERSION ||
"test";
console.log(
"protocol release version:",
options.protocolReleaseVersion
);
const retVal = await logicFn(args, options);
cb();
return retVal;
} catch (err) {
cb(err);
}
};
};