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

Commit

Permalink
Merge pull request #3393 from trufflesuite/add-subcommands
Browse files Browse the repository at this point in the history
Enhancement: Subcommands and Truffle DB Serve functionality
  • Loading branch information
fainashalts authored and g. nicholas d'andrea committed Oct 31, 2020
2 parents e53d490 + e252637 commit 9af28a7
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 4 deletions.
31 changes: 31 additions & 0 deletions packages/core/lib/commands/db/commands/serve.js
Original file line number Diff line number Diff line change
@@ -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;
47 changes: 47 additions & 0 deletions packages/core/lib/commands/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const OS = require("os");
const serveCommand = require("./commands/serve");

const usage =
"truffle db <sub-command> [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;
19 changes: 15 additions & 4 deletions packages/core/lib/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}'`);
Expand All @@ -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: `);
Expand Down
1 change: 1 addition & 0 deletions packages/core/lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit 9af28a7

Please sign in to comment.