Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Add --url option to the truffle exec command #5852

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/core/lib/commands/exec/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <script.js> [--compile]",
usage:
"truffle exec <script.js> [--compile] [--network <network>|--url <provider_url>]",
options: [
{
option: "<script.js>",
Expand All @@ -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"]
}
};
17 changes: 15 additions & 2 deletions packages/core/lib/commands/exec/run.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
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;
const { Environment } = require("@truffle/environment");
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;

Expand Down
17 changes: 17 additions & 0 deletions packages/truffle/test/scenarios/commands/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
});