From dc3c40e9f83205404b7037cb463662c617af7653 Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 15 Nov 2022 10:35:44 -0500 Subject: [PATCH 01/24] turn off yargs default behavior for truffle cmd help --- packages/core/lib/command-utils.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 30cc45ae7a3..2941dd74046 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -93,8 +93,10 @@ const prepareOptions = ({ command, inputStrings, options }) => { const yargs = require("yargs/yargs")(); yargs .command(require(`./commands/${command.name}/meta`)) - //Turn off yargs' default behavior when handling "truffle --version" - .version(false); + //Turn off yargs' default behavior when handling `truffle --version` & `truffle --help` + .version(false) + .help(false); + const commandOptions = yargs.parse(inputStrings); // remove the task name itself put there by yargs From 46d5554c271cb1a30645d1ad511325372b195d5a Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 15 Nov 2022 10:56:19 -0500 Subject: [PATCH 02/24] change inputStrings when help or --help is present --- packages/core/cli.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/core/cli.js b/packages/core/cli.js index 67d51425fe7..f55f1534774 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -52,6 +52,12 @@ if (userWantsGeneralHelp) { process.exit(0); } +//if the last word of inputString is `help` or `--help`, re-assign inputString to run as `truffle help ` +if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { + inputStrings.pop(); + inputStrings.unshift("help"); +} + const { getCommand, prepareOptions, From caccaf6846e1b82dc011b0631e666ac5e7bba8df Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 15 Nov 2022 18:10:10 -0500 Subject: [PATCH 03/24] add handling for `truffle --help ` --- packages/core/cli.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index f55f1534774..388b6fdfa39 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -51,8 +51,10 @@ if (userWantsGeneralHelp) { displayGeneralHelp(); process.exit(0); } - -//if the last word of inputString is `help` or `--help`, re-assign inputString to run as `truffle help ` +//clean up inputString to run as `truffle help ` if `help`, `--help` is present with a +if (inputStrings.length > 1 && inputStrings[0] === "--help") { + inputStrings[inputStrings.indexOf("--help")] = "help"; +} if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { inputStrings.pop(); inputStrings.unshift("help"); @@ -73,7 +75,9 @@ const command = getCommand({ //getCommand() will return null if a command not recognized by truffle is used. if (command === null) { console.log( - `\`truffle ${inputStrings}\` is not a valid truffle command. Please see \`truffle help\` for available commands.` + `\`truffle ${inputStrings.join( + " " + )}\` is not a valid truffle command. Please see \`truffle help\` for available commands.` ); process.exit(1); } From 368486722d71343fd4650aeb931eb9d625103297 Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 29 Nov 2022 10:06:10 -0500 Subject: [PATCH 04/24] improve code comment readability --- packages/core/cli.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index 388b6fdfa39..e1e42d6c5bb 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -51,10 +51,11 @@ if (userWantsGeneralHelp) { displayGeneralHelp(); process.exit(0); } -//clean up inputString to run as `truffle help ` if `help`, `--help` is present with a +// when `truffle --help ` is used, convert inputStrings to run as `truffle help ` if (inputStrings.length > 1 && inputStrings[0] === "--help") { inputStrings[inputStrings.indexOf("--help")] = "help"; } +// when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { inputStrings.pop(); inputStrings.unshift("help"); From f373b4c938d7100d0da51a488c3df9188564213e Mon Sep 17 00:00:00 2001 From: liang liang Date: Fri, 6 Jan 2023 14:50:15 -0500 Subject: [PATCH 05/24] Add complete Help command usage info --- packages/core/lib/commands/help/meta.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/commands/help/meta.js b/packages/core/lib/commands/help/meta.js index 9f3d51dc979..1ff74e27f1f 100644 --- a/packages/core/lib/commands/help/meta.js +++ b/packages/core/lib/commands/help/meta.js @@ -1,9 +1,16 @@ +const OS = require("os"); + module.exports = { command: "help", description: "List all commands or provide information about a specific command", help: { - usage: "truffle help []", + usage: + "truffle help [ ]" + + OS.EOL + + " truffle --help [ ]" + + OS.EOL + + " truffle [ ] --help", options: [ { option: "", From f35bc3919d799a3ceade0f18508817d9c6beb642 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 30 Nov 2022 11:00:34 -0500 Subject: [PATCH 06/24] Add scenario test for Help cmd --- .../truffle/test/scenarios/commands/help.js | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/packages/truffle/test/scenarios/commands/help.js b/packages/truffle/test/scenarios/commands/help.js index 6063d1714bb..76f79e037dd 100644 --- a/packages/truffle/test/scenarios/commands/help.js +++ b/packages/truffle/test/scenarios/commands/help.js @@ -42,5 +42,59 @@ describe("truffle help [ @standalone ]", function () { ); assert(output.includes("--environment")); }); + + it("displays help for given command when `--help` is at the end of the command line", async function () { + await CommandRunner.run("compile --help", config); + const output = logger.contents(); + assert(output.includes("Description: Compile contract source files")); + }).timeout(20000); + + it("displays help for given command when `help` is at the end of the command line", async function () { + await CommandRunner.run("compile help", config); + const output = logger.contents(); + assert(output.includes("Description: Compile contract source files")); + }).timeout(20000); + + it("displays help for given command when `--help` is right before the truffle command", async function () { + await CommandRunner.run("help compile", config); + const output = logger.contents(); + assert(output.includes("Description: Compile contract source files")); + }).timeout(20000); + + it("displays help for the `help` command", async function () { + await CommandRunner.run("help help", config); + const output = logger.contents(); + assert( + output.includes( + " Description: List all commands or provide information about a specific command" + ) + ); + }).timeout(20000); + + it("errors when is not valid", async function () { + await CommandRunner.run("help compile boo", config); + const output = logger.contents(); + assert(output.includes("WARNING: compile does not have sub commands")); + }).timeout(20000); + + it("errors with correct `help` usage info when `truffle --help ` is used", async function () { + await CommandRunner.run("db --help serve", config); + const output = logger.contents(); + assert( + output.includes( + "Error: please use below syntax to displaying help information" + ) + ); + }).timeout(20000); + + it("errors with correct `help` usage info when `truffle help ` is used", async function () { + await CommandRunner.run("db help serve", config); + const output = logger.contents(); + assert( + output.includes( + "Error: please use below syntax to displaying help information" + ) + ); + }).timeout(20000); }); }); From 27875a40638c8c1bd862140fa995b48602754ce4 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 30 Nov 2022 11:02:57 -0500 Subject: [PATCH 07/24] Add handling when help cmd is in wrong place --- packages/core/cli.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/core/cli.js b/packages/core/cli.js index e1e42d6c5bb..b5cfd86c6c4 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -42,6 +42,16 @@ listeners.forEach(listener => process.removeListener("warning", listener)); const inputStrings = process.argv.slice(2); +if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) { + const help = require("./lib/commands/help").meta; + console.log( + "Error: please use below syntax to displaying help information\n", + "\n ", + help.help.usage + ); + process.exit(); +} + const userWantsGeneralHelp = inputStrings.length === 0 || (inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0])); From 6d24c86e2933a11c24348898ea2994bbb97826f6 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 30 Nov 2022 11:07:42 -0500 Subject: [PATCH 08/24] Add error handling when subCommand is invalid --- .../core/lib/commands/help/displayCommandHelp.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/core/lib/commands/help/displayCommandHelp.js b/packages/core/lib/commands/help/displayCommandHelp.js index 0d3abeb00ff..e22573803f7 100644 --- a/packages/core/lib/commands/help/displayCommandHelp.js +++ b/packages/core/lib/commands/help/displayCommandHelp.js @@ -6,7 +6,19 @@ module.exports = async function (selectedCommand, subCommand, options) { const chosenCommand = commands[selectedCommand].meta; - if (subCommand && chosenCommand.subCommands[subCommand]) { + if (subCommand) { + if (!chosenCommand.subCommands) { + console.log(`WARNING: ${selectedCommand} does not have sub commands.`); + process.exit(); + } + + if (!chosenCommand.subCommands[subCommand]) { + console.log( + `WARNING: ${subCommand} is an invalid sub command for ${selectedCommand}.` + ); + process.exit(); + } + commandHelp = chosenCommand.subCommands[subCommand].help; commandDescription = chosenCommand.subCommands[subCommand].description; } else { From ad4668825c8e47b68dd6cb52d0846d5da59b8cdf Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 30 Nov 2022 12:14:58 -0500 Subject: [PATCH 09/24] Move help inputStrings process to command-utils --- packages/core/cli.js | 34 ++++++---------------------- packages/core/lib/command-utils.js | 36 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index b5cfd86c6c4..7885e17af92 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -40,35 +40,15 @@ if (!semver.gte(process.version, minimumNodeVersion)) { const listeners = process.listeners("warning"); listeners.forEach(listener => process.removeListener("warning", listener)); -const inputStrings = process.argv.slice(2); +let inputStrings = process.argv.slice(2); -if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) { - const help = require("./lib/commands/help").meta; - console.log( - "Error: please use below syntax to displaying help information\n", - "\n ", - help.help.usage - ); - process.exit(); -} +//check if user wants some help +const helpNeeded = inputStrings.some(r => ["help", "--help"].indexOf(r) >= 0); -const userWantsGeneralHelp = - inputStrings.length === 0 || - (inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0])); - -if (userWantsGeneralHelp) { - const { displayGeneralHelp } = require("./lib/command-utils"); - displayGeneralHelp(); - process.exit(0); -} -// when `truffle --help ` is used, convert inputStrings to run as `truffle help ` -if (inputStrings.length > 1 && inputStrings[0] === "--help") { - inputStrings[inputStrings.indexOf("--help")] = "help"; -} -// when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` -if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { - inputStrings.pop(); - inputStrings.unshift("help"); +if (helpNeeded) { + //check what help does user want + const { processHelpInput } = require("./lib/command-utils"); + inputStrings = processHelpInput(inputStrings); } const { diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 2941dd74046..5d1055ba3c7 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -203,6 +203,41 @@ const runCommand = async function (command, options) { return await command.run(options); }; +const processHelpInput = inputStrings => { + //is it wrong syntax of help? + if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) { + const help = require("./commands/help").meta; + console.log( + "Error: please use below syntax to displaying help information\n", + "\n ", + help.help.usage + ); + process.exit(); + } + + // when `truffle --help ` is used, convert inputStrings to run as `truffle help ` + if (inputStrings.length > 1 && inputStrings[0] === "--help") { + inputStrings[inputStrings.indexOf("--help")] = "help"; + } + + // when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` + if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { + inputStrings.pop(); + inputStrings.unshift("help"); + } + + const userWantsGeneralHelp = + inputStrings.length === 0 || + (inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0])); + + //is it general help? + if (userWantsGeneralHelp) { + displayGeneralHelp(); + process.exit(0); + } + return inputStrings; +}; + const displayGeneralHelp = () => { const yargs = require("yargs/yargs")(); commands.forEach(command => { @@ -307,6 +342,7 @@ const deriveConfigEnvironment = function (detectedConfig, network, url) { }; module.exports = { + processHelpInput, displayGeneralHelp, getCommand, prepareOptions, From f82d559af572380b678992368e3ea4f0450448b0 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 4 Jan 2023 17:52:07 -0500 Subject: [PATCH 10/24] add missing deps to @core/package.json --- packages/core/package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/core/package.json b/packages/core/package.json index ce33f58ce33..cc243105ff7 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -35,6 +35,7 @@ "@truffle/debugger": "^11.0.22", "@truffle/decoder": "^5.3.5", "@truffle/deployer": "^3.3.2", + "@truffle/db-loader": "^0.2.10", "@truffle/environment": "^0.2.135", "@truffle/error": "^0.2.0", "@truffle/expect": "^0.1.4", @@ -69,6 +70,7 @@ "get-random-values": "^1.2.2", "glob": "^7.1.6", "inquirer": "8.2.2", + "iter-tools": "^7.5.0", "js-interpreter": "2.2.0", "lodash": "^4.17.21", "mocha": "10.1.0", @@ -82,6 +84,7 @@ "universal-analytics": "^0.4.17", "web3": "1.8.1", "web3-utils": "1.8.1", + "uuid": "^9.0.0", "xregexp": "^4.2.4", "yargs": "^13.3.0" }, @@ -103,4 +106,4 @@ } ], "namespace": "consensys" -} +} \ No newline at end of file From b14076f753dede91a8d15afb279dcf35d389e59c Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 30 Nov 2022 14:17:26 -0500 Subject: [PATCH 11/24] add yarn.lock --- yarn.lock | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/yarn.lock b/yarn.lock index c2611d55bdb..6b5cfec389c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -16659,6 +16659,13 @@ iter-tools@^7.0.2: dependencies: "@babel/runtime" "^7.12.1" +iter-tools@^7.5.0: + version "7.5.0" + resolved "https://registry.yarnpkg.com/iter-tools/-/iter-tools-7.5.0.tgz#061240dcac13668e66bb787b0fcd407a58ea39ab" + integrity sha512-L0p/RG3Hwk1urilryDKqU8pQ1t5AaaMc7CHmiwJD/uh63Lv7VyjNng/esstf+Tct1587IpetpcDFdufz8sG+sQ== + dependencies: + "@babel/runtime" "^7.12.1" + iterate-iterator@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/iterate-iterator/-/iterate-iterator-1.0.1.tgz#1693a768c1ddd79c969051459453f082fe82e9f6" From b0f9f7b1aa8fdafc7fc85ea884e91f56d0760db5 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 14 Dec 2022 15:12:47 -0500 Subject: [PATCH 12/24] refactor logic to process help input --- packages/core/cli.js | 14 +++++++++----- packages/core/lib/command-utils.js | 16 ++++++---------- packages/truffle/test/scenarios/commands/help.js | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index 7885e17af92..e064f9c7e6b 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -43,12 +43,16 @@ listeners.forEach(listener => process.removeListener("warning", listener)); let inputStrings = process.argv.slice(2); //check if user wants some help -const helpNeeded = inputStrings.some(r => ["help", "--help"].indexOf(r) >= 0); - -if (helpNeeded) { - //check what help does user want +if (inputStrings.length === 0) { + const { displayGeneralHelp } = require("./lib/command-utils"); + displayGeneralHelp(); + process.exit(); +} else if ( + inputStrings.some(inputString => ["help", "--help"].includes(inputString)) +) { + //check what kind of help the user is looking for const { processHelpInput } = require("./lib/command-utils"); - inputStrings = processHelpInput(inputStrings); + processHelpInput(inputStrings); } const { diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 5d1055ba3c7..0ebd8a6c3b1 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -204,11 +204,11 @@ const runCommand = async function (command, options) { }; const processHelpInput = inputStrings => { - //is it wrong syntax of help? + //When we think user is trying to access help incorrectly, provide them with some helpful information. if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) { const help = require("./commands/help").meta; console.log( - "Error: please use below syntax to displaying help information\n", + "Error: please use the following syntax for accessing help information\n", "\n ", help.help.usage ); @@ -221,19 +221,15 @@ const processHelpInput = inputStrings => { } // when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` - if (["help", "--help"].includes(inputStrings[inputStrings.length - 1])) { + if (["help", "--help"].includes(inputStrings[1])) { inputStrings.pop(); inputStrings.unshift("help"); } - const userWantsGeneralHelp = - inputStrings.length === 0 || - (inputStrings.length === 1 && ["help", "--help"].includes(inputStrings[0])); - - //is it general help? - if (userWantsGeneralHelp) { + //when user wants general help + if (inputStrings.length === 1) { displayGeneralHelp(); - process.exit(0); + process.exit(); } return inputStrings; }; diff --git a/packages/truffle/test/scenarios/commands/help.js b/packages/truffle/test/scenarios/commands/help.js index 76f79e037dd..aaf0f83a988 100644 --- a/packages/truffle/test/scenarios/commands/help.js +++ b/packages/truffle/test/scenarios/commands/help.js @@ -82,7 +82,7 @@ describe("truffle help [ @standalone ]", function () { const output = logger.contents(); assert( output.includes( - "Error: please use below syntax to displaying help information" + "Error: please use the following syntax for accessing help information" ) ); }).timeout(20000); @@ -92,7 +92,7 @@ describe("truffle help [ @standalone ]", function () { const output = logger.contents(); assert( output.includes( - "Error: please use below syntax to displaying help information" + "Error: please use the following syntax for accessing help information" ) ); }).timeout(20000); From 1a787221f3162e8ebbfb54bb392174af4ca4e961 Mon Sep 17 00:00:00 2001 From: liang liang Date: Wed, 4 Jan 2023 16:41:25 -0500 Subject: [PATCH 13/24] change inputStrings assignment to use const --- packages/core/cli.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index e064f9c7e6b..442b9f36754 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -40,7 +40,7 @@ if (!semver.gte(process.version, minimumNodeVersion)) { const listeners = process.listeners("warning"); listeners.forEach(listener => process.removeListener("warning", listener)); -let inputStrings = process.argv.slice(2); +const inputStrings = process.argv.slice(2); //check if user wants some help if (inputStrings.length === 0) { From 368ff274b2ec139527d465948ebf35bbcbc77b70 Mon Sep 17 00:00:00 2001 From: liang liang Date: Fri, 6 Jan 2023 14:40:51 -0500 Subject: [PATCH 14/24] rework processHelpInput in command-utils --- packages/core/lib/command-utils.js | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 0ebd8a6c3b1..4e0c7964936 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -204,33 +204,20 @@ const runCommand = async function (command, options) { }; const processHelpInput = inputStrings => { - //When we think user is trying to access help incorrectly, provide them with some helpful information. - if (inputStrings.length > 2 && ["help", "--help"].includes(inputStrings[1])) { - const help = require("./commands/help").meta; - console.log( - "Error: please use the following syntax for accessing help information\n", - "\n ", - help.help.usage - ); + //when user wants general help + if (inputStrings.length === 1) { + displayGeneralHelp(); process.exit(); } - // when `truffle --help ` is used, convert inputStrings to run as `truffle help ` - if (inputStrings.length > 1 && inputStrings[0] === "--help") { - inputStrings[inputStrings.indexOf("--help")] = "help"; - } + //check if --help is used + const helpIndex = inputStrings.findIndex(x => x === "--help"); - // when `truffle help | --help` is used, convert inputStrings to run as `truffle help ` - if (["help", "--help"].includes(inputStrings[1])) { - inputStrings.pop(); + if (helpIndex !== -1) { + inputStrings.splice(helpIndex, 1); inputStrings.unshift("help"); } - //when user wants general help - if (inputStrings.length === 1) { - displayGeneralHelp(); - process.exit(); - } return inputStrings; }; From 9523edf1e4f09860fb92f97c813656ea0183d35d Mon Sep 17 00:00:00 2001 From: liang liang Date: Fri, 6 Jan 2023 14:41:58 -0500 Subject: [PATCH 15/24] use TruffleError for error handling --- .../core/lib/commands/help/displayCommandHelp.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/packages/core/lib/commands/help/displayCommandHelp.js b/packages/core/lib/commands/help/displayCommandHelp.js index e22573803f7..b3fb40312dc 100644 --- a/packages/core/lib/commands/help/displayCommandHelp.js +++ b/packages/core/lib/commands/help/displayCommandHelp.js @@ -1,24 +1,20 @@ module.exports = async function (selectedCommand, subCommand, options) { const commands = require("../index"); const globalCommandOptions = require("../../global-command-options"); + const TruffleError = require("@truffle/error"); let commandHelp, commandDescription; + const inputStrings = process.argv.slice(2); + const chosenCommand = commands[selectedCommand].meta; if (subCommand) { - if (!chosenCommand.subCommands) { - console.log(`WARNING: ${selectedCommand} does not have sub commands.`); - process.exit(); - } - - if (!chosenCommand.subCommands[subCommand]) { - console.log( - `WARNING: ${subCommand} is an invalid sub command for ${selectedCommand}.` + if (!chosenCommand.subCommands || !chosenCommand.subCommands[subCommand]) { + throw new TruffleError( + `"truffle ${inputStrings.join(" ")}" is an anvalid command` ); - process.exit(); } - commandHelp = chosenCommand.subCommands[subCommand].help; commandDescription = chosenCommand.subCommands[subCommand].description; } else { From 56f4106b9fa7a81492d11bb8c69ba1a5d4f911b7 Mon Sep 17 00:00:00 2001 From: liang liang Date: Fri, 6 Jan 2023 14:43:04 -0500 Subject: [PATCH 16/24] refactor test for help command --- .../truffle/test/scenarios/commands/help.js | 36 ++----------------- 1 file changed, 3 insertions(+), 33 deletions(-) diff --git a/packages/truffle/test/scenarios/commands/help.js b/packages/truffle/test/scenarios/commands/help.js index aaf0f83a988..c9c7742f991 100644 --- a/packages/truffle/test/scenarios/commands/help.js +++ b/packages/truffle/test/scenarios/commands/help.js @@ -43,24 +43,12 @@ describe("truffle help [ @standalone ]", function () { assert(output.includes("--environment")); }); - it("displays help for given command when `--help` is at the end of the command line", async function () { + it("displays help for given command when `--help` is in the command line", async function () { await CommandRunner.run("compile --help", config); const output = logger.contents(); assert(output.includes("Description: Compile contract source files")); }).timeout(20000); - it("displays help for given command when `help` is at the end of the command line", async function () { - await CommandRunner.run("compile help", config); - const output = logger.contents(); - assert(output.includes("Description: Compile contract source files")); - }).timeout(20000); - - it("displays help for given command when `--help` is right before the truffle command", async function () { - await CommandRunner.run("help compile", config); - const output = logger.contents(); - assert(output.includes("Description: Compile contract source files")); - }).timeout(20000); - it("displays help for the `help` command", async function () { await CommandRunner.run("help help", config); const output = logger.contents(); @@ -71,29 +59,11 @@ describe("truffle help [ @standalone ]", function () { ); }).timeout(20000); - it("errors when is not valid", async function () { - await CommandRunner.run("help compile boo", config); - const output = logger.contents(); - assert(output.includes("WARNING: compile does not have sub commands")); - }).timeout(20000); - - it("errors with correct `help` usage info when `truffle --help ` is used", async function () { + it("displays help for `truffle --help ` is used", async function () { await CommandRunner.run("db --help serve", config); const output = logger.contents(); assert( - output.includes( - "Error: please use the following syntax for accessing help information" - ) - ); - }).timeout(20000); - - it("errors with correct `help` usage info when `truffle help ` is used", async function () { - await CommandRunner.run("db help serve", config); - const output = logger.contents(); - assert( - output.includes( - "Error: please use the following syntax for accessing help information" - ) + output.includes("Description: Start Truffle's GraphQL UI playground") ); }).timeout(20000); }); From e3cd4363deeccee7e6356e0a2b5bd8a6b6fbe929 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 11:59:30 -0500 Subject: [PATCH 17/24] expand test cases for --help --- .../truffle/test/scenarios/commands/help.js | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/packages/truffle/test/scenarios/commands/help.js b/packages/truffle/test/scenarios/commands/help.js index c9c7742f991..813c59f97f0 100644 --- a/packages/truffle/test/scenarios/commands/help.js +++ b/packages/truffle/test/scenarios/commands/help.js @@ -43,12 +43,6 @@ describe("truffle help [ @standalone ]", function () { assert(output.includes("--environment")); }); - it("displays help for given command when `--help` is in the command line", async function () { - await CommandRunner.run("compile --help", config); - const output = logger.contents(); - assert(output.includes("Description: Compile contract source files")); - }).timeout(20000); - it("displays help for the `help` command", async function () { await CommandRunner.run("help help", config); const output = logger.contents(); @@ -59,7 +53,21 @@ describe("truffle help [ @standalone ]", function () { ); }).timeout(20000); - it("displays help for `truffle --help ` is used", async function () { + it("displays help for given command when `--help` is at final position of the command line", async function () { + await CommandRunner.run("compile --help", config); + const output = logger.contents(); + assert(output.includes("Description: Compile contract source files")); + }).timeout(20000); + + it("displays help for given command when `--help` is at first position of the command line", async function () { + await CommandRunner.run("--help db serve", config); + const output = logger.contents(); + assert( + output.includes("Description: Start Truffle's GraphQL UI playground") + ); + }).timeout(20000); + + it("displays help for given command when `--help` is in the middle of the command line", async function () { await CommandRunner.run("db --help serve", config); const output = logger.contents(); assert( From 39659eaff02aa16e5e2ea030615dde02e2f18bf2 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 12:03:52 -0500 Subject: [PATCH 18/24] Fix usage syntax for help meta.js --- packages/core/lib/commands/help/meta.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/core/lib/commands/help/meta.js b/packages/core/lib/commands/help/meta.js index 1ff74e27f1f..ceb0e6fac9f 100644 --- a/packages/core/lib/commands/help/meta.js +++ b/packages/core/lib/commands/help/meta.js @@ -6,11 +6,11 @@ module.exports = { "List all commands or provide information about a specific command", help: { usage: - "truffle help [ ]" + + "truffle help [ []]" + OS.EOL + - " truffle --help [ ]" + + " truffle --help [ []]" + OS.EOL + - " truffle [ ] --help", + " truffle [ []] --help", options: [ { option: "", From 24307a9f1aa879772f578f7b8cfc2457ebca7b91 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 12:04:25 -0500 Subject: [PATCH 19/24] Fix typo on error message --- packages/core/lib/commands/help/displayCommandHelp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/lib/commands/help/displayCommandHelp.js b/packages/core/lib/commands/help/displayCommandHelp.js index b3fb40312dc..e0ab4837cae 100644 --- a/packages/core/lib/commands/help/displayCommandHelp.js +++ b/packages/core/lib/commands/help/displayCommandHelp.js @@ -12,7 +12,7 @@ module.exports = async function (selectedCommand, subCommand, options) { if (subCommand) { if (!chosenCommand.subCommands || !chosenCommand.subCommands[subCommand]) { throw new TruffleError( - `"truffle ${inputStrings.join(" ")}" is an anvalid command` + `"truffle ${inputStrings.join(" ")}" is an invalid command` ); } commandHelp = chosenCommand.subCommands[subCommand].help; From 0265837460f78745b82723a9620fa6dd86e68120 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 14:35:37 -0500 Subject: [PATCH 20/24] refactor processHelpInput --- packages/core/cli.js | 24 ++++++++++-------------- packages/core/lib/command-utils.js | 8 +++++--- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index 442b9f36754..e1c0de0b589 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -42,25 +42,21 @@ listeners.forEach(listener => process.removeListener("warning", listener)); const inputStrings = process.argv.slice(2); -//check if user wants some help -if (inputStrings.length === 0) { - const { displayGeneralHelp } = require("./lib/command-utils"); - displayGeneralHelp(); - process.exit(); -} else if ( - inputStrings.some(inputString => ["help", "--help"].includes(inputString)) -) { - //check what kind of help the user is looking for - const { processHelpInput } = require("./lib/command-utils"); - processHelpInput(inputStrings); -} - const { getCommand, prepareOptions, - runCommand + runCommand, + processHelpInput } = require("./lib/command-utils"); +//if `help` or `--help` is in the command, validate and transform the input +if ( + inputStrings.some(inputString => ["help", "--help"].includes(inputString)) +) { + //handle general help case and displayCommand Help cases and return the inputStrings to be process further + processHelpInput(inputStrings); +} + const command = getCommand({ inputStrings, options: {}, diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 4e0c7964936..40096b2f74d 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -204,14 +204,16 @@ const runCommand = async function (command, options) { }; const processHelpInput = inputStrings => { + // handle general help case and displayCommand Help cases + //when user wants general help - if (inputStrings.length === 1) { + if (inputStrings.length === 0 || inputStrings.length === 1) { displayGeneralHelp(); process.exit(); } - //check if --help is used - const helpIndex = inputStrings.findIndex(x => x === "--help"); + //check where is --help used + const helpIndex = inputStrings.indexOf("--help"); if (helpIndex !== -1) { inputStrings.splice(helpIndex, 1); From 1ef17767a577c32e46307502104cb6ff28fae643 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 16:28:36 -0500 Subject: [PATCH 21/24] refact processHelpInput to inline check on cli.js --- packages/core/cli.js | 25 +++++++++++++++++++++---- packages/core/lib/command-utils.js | 21 --------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/packages/core/cli.js b/packages/core/cli.js index e1c0de0b589..31f1dd14c96 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -46,15 +46,32 @@ const { getCommand, prepareOptions, runCommand, - processHelpInput + displayGeneralHelp } = require("./lib/command-utils"); -//if `help` or `--help` is in the command, validate and transform the input +//User only enter truffle with no commands, let's show them what's available. +if (inputStrings.length === 0) { + displayGeneralHelp(); + process.exit(); +} + +//if `help` or `--help` is in the command, validate and transform the input argument for help if ( inputStrings.some(inputString => ["help", "--help"].includes(inputString)) ) { - //handle general help case and displayCommand Help cases and return the inputStrings to be process further - processHelpInput(inputStrings); + //when user wants general help + if (inputStrings.length === 1) { + displayGeneralHelp(); + process.exit(); + } + + //check where is --help used, mutate argument into a proper help command + const helpIndex = inputStrings.indexOf("--help"); + + if (helpIndex !== -1) { + inputStrings.splice(helpIndex, 1); + inputStrings.unshift("help"); + } } const command = getCommand({ diff --git a/packages/core/lib/command-utils.js b/packages/core/lib/command-utils.js index 40096b2f74d..2941dd74046 100644 --- a/packages/core/lib/command-utils.js +++ b/packages/core/lib/command-utils.js @@ -203,26 +203,6 @@ const runCommand = async function (command, options) { return await command.run(options); }; -const processHelpInput = inputStrings => { - // handle general help case and displayCommand Help cases - - //when user wants general help - if (inputStrings.length === 0 || inputStrings.length === 1) { - displayGeneralHelp(); - process.exit(); - } - - //check where is --help used - const helpIndex = inputStrings.indexOf("--help"); - - if (helpIndex !== -1) { - inputStrings.splice(helpIndex, 1); - inputStrings.unshift("help"); - } - - return inputStrings; -}; - const displayGeneralHelp = () => { const yargs = require("yargs/yargs")(); commands.forEach(command => { @@ -327,7 +307,6 @@ const deriveConfigEnvironment = function (detectedConfig, network, url) { }; module.exports = { - processHelpInput, displayGeneralHelp, getCommand, prepareOptions, From 580324e38c8a301ea7fa75452e2ea42c8913ddb3 Mon Sep 17 00:00:00 2001 From: liang liang Date: Mon, 9 Jan 2023 19:47:38 -0500 Subject: [PATCH 22/24] error better for unknown truffle db command --- packages/core/lib/commands/db/run.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core/lib/commands/db/run.js b/packages/core/lib/commands/db/run.js index 9f4880e6948..74dca2135f3 100644 --- a/packages/core/lib/commands/db/run.js +++ b/packages/core/lib/commands/db/run.js @@ -13,6 +13,6 @@ module.exports = async function (args) { break; default: - console.log(`Unknown command: ${subCommand}`); + console.log(`Unknown truffle db command: ${subCommand}`); } }; From 3495ea3b618e553afd65da48824ecc5d7a991006 Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 10 Jan 2023 16:16:18 -0500 Subject: [PATCH 23/24] expand documention on handling --help --- packages/core/cli.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/core/cli.js b/packages/core/cli.js index 31f1dd14c96..04ef1a791e1 100755 --- a/packages/core/cli.js +++ b/packages/core/cli.js @@ -69,7 +69,9 @@ if ( const helpIndex = inputStrings.indexOf("--help"); if (helpIndex !== -1) { + //remove `--help` from array inputStrings.splice(helpIndex, 1); + //insert `help` in first position inputStrings.unshift("help"); } } From 75ada28828b8b0e3c4462cfe3f97fdde0ec2a4f0 Mon Sep 17 00:00:00 2001 From: liang liang Date: Tue, 10 Jan 2023 16:19:51 -0500 Subject: [PATCH 24/24] remove incorrect syntax usage info --- packages/core/lib/commands/help/meta.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/packages/core/lib/commands/help/meta.js b/packages/core/lib/commands/help/meta.js index ceb0e6fac9f..af10cf33f9a 100644 --- a/packages/core/lib/commands/help/meta.js +++ b/packages/core/lib/commands/help/meta.js @@ -1,16 +1,9 @@ -const OS = require("os"); - module.exports = { command: "help", description: "List all commands or provide information about a specific command", help: { - usage: - "truffle help [ []]" + - OS.EOL + - " truffle --help [ []]" + - OS.EOL + - " truffle [ []] --help", + usage: "truffle help [ []]", options: [ { option: "",