diff --git a/packages/core/lib/commands/migrate/meta.js b/packages/core/lib/commands/migrate/meta.js index 1bc941990ec..46b54a4d9da 100644 --- a/packages/core/lib/commands/migrate/meta.js +++ b/packages/core/lib/commands/migrate/meta.js @@ -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: { @@ -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 |--url ]", options: [ { option: "--reset", @@ -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", + description: + "The network to connect to, as specified in the Truffle config." } ], - allowedGlobalOptions: ["network", "config", "quiet"] + allowedGlobalOptions: ["config", "quiet"] } }; diff --git a/packages/core/lib/commands/migrate/run.js b/packages/core/lib/commands/migrate/run.js index fb383ffa639..a1b4b0b7a42 100644 --- a/packages/core/lib/commands/migrate/run.js +++ b/packages/core/lib/commands/migrate/run.js @@ -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"; } diff --git a/packages/core/lib/console.js b/packages/core/lib/console.js index c00b14144a6..469e1948f01 100644 --- a/packages/core/lib/console.js +++ b/packages/core/lib/console.js @@ -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+/); @@ -33,12 +44,15 @@ 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`); @@ -46,6 +60,11 @@ const processInput = (input, allowedCommands) => { 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(); }; diff --git a/packages/truffle/test/scenarios/commands/develop.js b/packages/truffle/test/scenarios/commands/develop.js index c7d611f8fb7..445222633ec 100644 --- a/packages/truffle/test/scenarios/commands/develop.js +++ b/packages/truffle/test/scenarios/commands/develop.js @@ -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 () { @@ -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`)"; diff --git a/packages/truffle/test/scenarios/commands/migrate.js b/packages/truffle/test/scenarios/commands/migrate.js index 86d36876f9b..2a0a2927851 100644 --- a/packages/truffle/test/scenarios/commands/migrate.js +++ b/packages/truffle/test/scenarios/commands/migrate.js @@ -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); }); });