cli.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env node
  2. "use strict";
  3. var argparse = require("argparse");
  4. var fs = require("fs");
  5. var path = require("path");
  6. var util = require("util");
  7. var httpreq = require("httpreq");
  8. var cli = require("atomic-cli");
  9. var AtomicHelpFormatter = function (opts) {
  10. argparse.HelpFormatter.call(this, opts);
  11. };
  12. util.inherits(AtomicHelpFormatter, argparse.HelpFormatter);
  13. // http://stackoverflow.com/questions/13423540/argparse-subparser-hide-metavar-in-command-listing
  14. AtomicHelpFormatter.prototype._formatAction = function (action) {
  15. var parts = argparse.HelpFormatter.prototype._formatAction.call(this, action);
  16. if (action.nargs == argparse.Const.PARSER) {
  17. var lines = parts.split("\n");
  18. lines.shift();
  19. parts = lines.join("\n");
  20. }
  21. return parts;
  22. };
  23. var catchErrors = function (promise) {
  24. promise.catch(function (error) {
  25. if (Array.isArray(error)) error = error[0]; // NCP throws an array of errors...?
  26. if (error) console.error(error.message || error);
  27. process.exit(1);
  28. });
  29. };
  30. var parser = new argparse.ArgumentParser({prog: "atomic-cli", formatterClass: AtomicHelpFormatter,
  31. description: "Atomic Game Engine CLI"});
  32. parser.addArgument(["-v", "--version"], {action: "version", help: "Print version and exit.",
  33. version: cli.VERSION});
  34. var commands = parser.addSubparsers({title: "Commands", metavar: "<command>"});
  35. var cmd = commands.addParser("new", {help: "Create a new project.",
  36. description: "Creates a new project at the given path.",
  37. aliases: ["create"]});
  38. cmd.addArgument(["path"], {help: "The new project directory to create."});
  39. cmd.setDefaults({action: function (args) {
  40. cli.newProject(args.path)
  41. .then(function () {
  42. console.log("New Atomic project created in " + path.resolve(args.path));
  43. })
  44. .catch(function (error) {
  45. console.error("Error: Could not create " + path.resolve(args.path));
  46. process.exit(1);
  47. });
  48. }});
  49. // GO!
  50. if (process.argv.length > 2) {
  51. var args = parser.parseArgs();
  52. args.action(args);
  53. } else {
  54. parser.printHelp();
  55. }
  56. // spawn(cli.ATOMIC_TOOL_BIN, { stdio: 'inherit' });