diff --git a/packages/core/lib/commands/exec/meta.js b/packages/core/lib/commands/exec/meta.js index ad8b0c3f5c5..fe8ebc68e9e 100644 --- a/packages/core/lib/commands/exec/meta.js +++ b/packages/core/lib/commands/exec/meta.js @@ -12,10 +12,15 @@ module.exports = { compile: { type: "boolean", default: false + }, + url: { + describe: "Connect to a specified provider given via URL", + type: "string" } }, help: { - usage: "truffle exec [--compile]", + usage: + "truffle exec [--compile] [--network |--url ]", options: [ { option: "", @@ -26,8 +31,18 @@ module.exports = { { option: "--compile", description: "Compile contracts before executing the script." + }, + { + option: "--url", + description: + "Connects to a specified provider given via URL, ignoring networks in config." + }, + { + option: "--network", + description: + "The network to connect to, as specified in the Truffle config." } ], - allowedGlobalOptions: ["network", "config"] + allowedGlobalOptions: ["config"] } }; diff --git a/packages/core/lib/commands/exec/run.js b/packages/core/lib/commands/exec/run.js index 8a758dc2465..fd8b9cc42ad 100644 --- a/packages/core/lib/commands/exec/run.js +++ b/packages/core/lib/commands/exec/run.js @@ -1,5 +1,4 @@ module.exports = async function (options) { - const Config = require("@truffle/config"); const WorkflowCompile = require("@truffle/workflow-compile").default; const ConfigurationError = require("../../errors/configurationerror"); const exec = require("@truffle/require").exec; @@ -7,8 +6,22 @@ module.exports = async function (options) { const path = require("path"); const OS = require("os"); const { promisify } = require("util"); + const loadConfig = require("../../loadConfig"); + const TruffleError = require("@truffle/error"); - const config = Config.detect(options); + if (options.url && options.network) { + const message = + "" + + "Mutually exclusive options, --url and --network detected!" + + OS.EOL + + "Please use either --url or --network and try again." + + OS.EOL + + "See: https://trufflesuite.com/docs/truffle/reference/truffle-commands/#exec" + + OS.EOL; + throw new TruffleError(message); + } + + const config = loadConfig(options); let file = options.file; diff --git a/packages/truffle/test/scenarios/commands/exec.js b/packages/truffle/test/scenarios/commands/exec.js index daf28199681..565300198e5 100644 --- a/packages/truffle/test/scenarios/commands/exec.js +++ b/packages/truffle/test/scenarios/commands/exec.js @@ -58,4 +58,21 @@ describe("truffle exec [ @standalone ]", function () { const output = logger.contents(); assert(output.includes("5")); }); + + it("runs script when --url option is passed", async function () { + this.timeout(30000); + await CommandRunner.run("compile", config); + assert( + fs.existsSync( + path.join(config.contracts_build_directory, "Executable.json") + ) + ); + + await CommandRunner.run( + "exec script.js --url http://127.0.0.1:8545", + config + ); + const output = logger.contents(); + assert(output.includes("5")); + }); });