diff --git a/packages/core/lib/commands/db/commands/serve.js b/packages/core/lib/commands/db/commands/serve.js new file mode 100644 index 00000000000..4d11c6e22b1 --- /dev/null +++ b/packages/core/lib/commands/db/commands/serve.js @@ -0,0 +1,31 @@ +const command = { + command: "serve", + description: "Start Truffle's GraphQL UI playground", + builder: {}, + help: { + usage: "truffle db serve", + options: [] + }, + + /* This command does starts an express derived server that invokes + * `process.exit()` on SIGINT. As a result there is no need to invoke + * truffle's own `process.exit()` which is triggered by invoking the `done` + * callback. + * + * Todo: blacklist this command for REPLs + */ + run: async function (argv) { + const Config = require("@truffle/config"); + const { playgroundServer } = require("@truffle/db"); + + const config = Config.detect(argv); + const port = (config.db && config.db.port) || 4444; + + const { url } = await playgroundServer(config).listen({ port }); + + console.log(`🚀 Playground listening at ${url}`); + console.log(`ℹ Press Ctrl-C to exit`); + } +}; + +module.exports = command; diff --git a/packages/core/lib/commands/db/index.js b/packages/core/lib/commands/db/index.js new file mode 100644 index 00000000000..2075b076465 --- /dev/null +++ b/packages/core/lib/commands/db/index.js @@ -0,0 +1,47 @@ +const OS = require("os"); +const serveCommand = require("./commands/serve"); + +const usage = + "truffle db [options]" + + OS.EOL + + OS.EOL + + " Available sub-commands: " + + OS.EOL + + OS.EOL + + " serve \tStart the GraphQL server" + + OS.EOL; + +const command = { + command: "db", + description: "Database interface commands", + builder: function (yargs) { + return yargs.commandDir("commands").demandCommand(); + }, + + subCommands: { + serve: { + help: serveCommand.help, + description: serveCommand.description + } + }, + + help: { + usage, + options: [] + }, + + run: function (args, done) { + const [subCommand] = args._; + switch (subCommand) { + case "serve": + serveCommand.run(args, done); + break; + + default: + console.log(`Unknown command: ${subCommand}`); + done(); + } + } +}; + +module.exports = command; diff --git a/packages/core/lib/commands/help.js b/packages/core/lib/commands/help.js index 0e340dd7393..f275e96e81c 100644 --- a/packages/core/lib/commands/help.js +++ b/packages/core/lib/commands/help.js @@ -19,9 +19,10 @@ var command = { return callback(); } var selectedCommand = options._[0]; + var subCommand = options._[1]; if (commands[selectedCommand]) { - this.displayCommandHelp(selectedCommand); + this.displayCommandHelp(selectedCommand, subCommand); return callback(); } else { console.log(`\n Cannot find the given command '${selectedCommand}'`); @@ -33,12 +34,22 @@ var command = { return callback(); } }, - displayCommandHelp: function (selectedCommand) { + displayCommandHelp: function (selectedCommand, subCommand) { let commands = require("./index"); - var commandHelp = commands[selectedCommand].help; + let commandHelp, commandDescription; + + const chosenCommand = commands[selectedCommand]; + + if (subCommand && chosenCommand.subCommands[subCommand]) { + commandHelp = chosenCommand.subCommands[subCommand].help; + commandDescription = chosenCommand.subCommands[subCommand].description; + } else { + commandHelp = chosenCommand.help; + commandDescription = chosenCommand.description; + } console.log(`\n Usage: ${commandHelp.usage}`); - console.log(` Description: ${commands[selectedCommand].description}`); + console.log(` Description: ${commandDescription}`); if (commandHelp.options.length > 0) { console.log(` Options: `); diff --git a/packages/core/lib/commands/index.js b/packages/core/lib/commands/index.js index a51a73ad619..716d29df218 100644 --- a/packages/core/lib/commands/index.js +++ b/packages/core/lib/commands/index.js @@ -4,6 +4,7 @@ module.exports = { config: require("./config"), console: require("./console"), create: require("./create"), + db: require("./db"), debug: require("./debug"), deploy: require("./deploy"), develop: require("./develop"), diff --git a/packages/truffle/webpack.config.js b/packages/truffle/webpack.config.js index fb8a6dd1729..91232bcbe58 100644 --- a/packages/truffle/webpack.config.js +++ b/packages/truffle/webpack.config.js @@ -98,6 +98,7 @@ module.exports = { // module that's a dependency of Truffle instead. /^original-require$/, /^mocha$/, + /^@truffle\/db/, // this is the commands portion shared by cli.js and console-child.js /^\.\/commands.bundled.js$/ ],