Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
Draft
21 changes: 19 additions & 2 deletions packages/core/lib/commands/migrate/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ module.exports = {
type: "boolean",
default: true,
hidden: true
},
"url": {
describe:
"Creates a provider using the given url and connects to the network",
type: "string"
}
},
help: {
Expand All @@ -62,7 +67,9 @@ module.exports = {
" " + // spacing to align with previous line
"[--compile-all] [--compile-none] [--verbose-rpc] [--interactive]\n" +
" " + // spacing to align with previous line
"[--skip-dry-run] [--describe-json] [--dry-run]",
"[--skip-dry-run] [--describe-json] [--dry-run]\n" +
" " + // spacing to align with previous line
"[--network <network>|--url <provider_url>]",
options: [
{
option: "--reset",
Expand Down Expand Up @@ -118,8 +125,18 @@ module.exports = {
option: "--save",
description: "Specify whether the migration will save on chain",
hidden: true
},
{
option: "--url",
description:
"Creates a provider using the given url and connects to the network."
},
{
option: "--network",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about why you prefer --network lives here vs with the other global options.

Actually, can url be a global option as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global options generally appends the options at the end of the usage. The reason behind keeping --network and --url options here rather than in the global options, is to show that they are mutually exclusive.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okie. thank you for the explanation.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a note in each of these letting the user know that these options are mutually exclusive? do you think that would be helpful?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage section shows that they are mutually exclusive but if adding a note in the descriptions is preferable, then we can add it here too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whatever you think, I don't feel strongly about it :)

description:
"The network to connect to, as specified in the Truffle config."
}
],
allowedGlobalOptions: ["network", "config", "quiet"]
allowedGlobalOptions: ["config", "quiet"]
}
};
18 changes: 16 additions & 2 deletions packages/core/lib/commands/migrate/run.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
module.exports = async function (options) {
const WorkflowCompile = require("@truffle/workflow-compile").default;
const { Environment } = require("@truffle/environment");
const Config = require("@truffle/config");
const determineDryRunSettings = require("./determineDryRunSettings");
const prepareConfigForRealMigrations = require("./prepareConfigForRealMigrations");
const runMigrations = require("./runMigrations");
const setUpDryRunEnvironmentThenRunMigrations = require("./setUpDryRunEnvironmentThenRunMigrations");
const loadConfig = require("../../loadConfig");
const OS = require("os");
const TruffleError = require("@truffle/error");
const tmp = require("tmp");
tmp.setGracefulCleanup();

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/#migrate" +
OS.EOL;
throw new TruffleError(message);
}

const config = loadConfig(options);
if (config.compileNone || config["compile-none"]) {
config.compiler = "none";
}
Expand Down
21 changes: 20 additions & 1 deletion packages/core/lib/console.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const validTruffleCommands = require("./commands/commands");
// by the REPL
const makeIIFE = str => `(() => "${str}")()`;

function hasMutuallyExclusiveOptions(words) {
if (words.includes("--url")) {
const message = "url option is not supported within Truffle REPL";
return makeIIFE(`ℹ️ : ${message}`);
}
if (words.includes("--network")) {
const message = "network option is not supported within Truffle REPL";
return makeIIFE(`ℹ️ : ${message}`);
}
}

const processInput = (input, allowedCommands) => {
const words = input.trim().split(/\s+/);

Expand All @@ -33,19 +44,27 @@ const processInput = (input, allowedCommands) => {

if (cmd === undefined) {
return makeIIFE(
`ℹ️ : 'Missing truffle command. Please include a valid truffle command.`
`ℹ️ : Missing truffle command. Please include a valid truffle command.`
);
}

const normalizedCommand = cmd.toLowerCase();
if (validTruffleCommands.includes(normalizedCommand)) {
const errorMessage = hasMutuallyExclusiveOptions(words);
if (errorMessage) return errorMessage;

return allowedCommands.includes(normalizedCommand)
? words.slice(1).join(" ")
: makeIIFE(`ℹ️ : '${cmd}' is not allowed within Truffle REPL`);
}
return makeIIFE(`ℹ️ : '${cmd}' is not a valid Truffle command`);
}

if (validTruffleCommands.includes(words[0].toLowerCase())) {
const errorMessage = hasMutuallyExclusiveOptions(words);
if (errorMessage) return errorMessage;
}

// an expression
return input.trim();
};
Expand Down
34 changes: 33 additions & 1 deletion packages/truffle/test/scenarios/commands/develop.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe("truffle develop", function () {
},
{
cmd: "",
expectedError: `ℹ️ : 'Missing truffle command. Please include a valid truffle command.`
expectedError: `ℹ️ : Missing truffle command. Please include a valid truffle command.`
}
].forEach(({ cmd, expectedError }) => {
it(`alerts on 'truffle ${cmd}'`, async function () {
Expand All @@ -116,6 +116,38 @@ describe("truffle develop", function () {
});
});

describe("Improper command options", async function () {
this.timeout(7000);

[
{
option: "--url",
expectedError: `ℹ️ : url option is not supported within Truffle REPL`
},
{
option: "--network",
expectedError: `ℹ️ : network option is not supported within Truffle REPL`
}
].forEach(({ option, expectedError }) => {
it(`alerts on 'migrate ${option}'`, async function () {
await CommandRunner.runInREPL({
inputCommands: [`migrate ${option}`],
config,
executableCommand: "develop",
displayHost: "develop"
});

const output = logger.contents();
assert(
output.includes(expectedError),
`Expected string in output: "${expectedError}"\n${formatLines(
output
)}`
);
});
});
});

it("handles awaits", async function () {
this.timeout(70000);
const input = "await Promise.resolve(`${6*7} is probably not a prime`)";
Expand Down
10 changes: 10 additions & 0 deletions packages/truffle/test/scenarios/commands/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,15 @@ describe("truffle migrate", () => {
assert.fail();
}
}).timeout(20000);

it("doesn't throw when --url option is passed", async () => {
try {
await CommandRunner.run("migrate --url http://127.0.0.1:8545", config);
} catch (error) {
console.log("the logger contents -- %o", config.logger.loggedStuff);
console.log("the following error occurred -- %o", error.message);
assert.fail();
}
}).timeout(20000);
});
});