cli.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. var cmd = commands.addParser("build", {help: "Builds the project",
  50. description: "Builds the platform"});
  51. cmd.addArgument(["platform"], {help: "The platform to build (windows|mac|ios|android|ios)"});
  52. cmd.setDefaults({action: function (args) {
  53. cli.build(args.platform)
  54. .then(function () {
  55. console.log("Project built " + path.resolve(args.path));
  56. })
  57. .catch(function (error) {
  58. console.error("Error: Could not build " + path.resolve(args.path));
  59. process.exit(1);
  60. });
  61. }});
  62. var addCommonArguments = function (parser) {
  63. parser.addArgument(["--debug"], {action: "storeTrue", help: "Build in debug mode."});
  64. };
  65. var cmd = commands.addParser("run", {help: "Build and run on a given platform.",
  66. description: "Builds and runs the game on a single given platform."});
  67. cmd.addArgument(["platform"], {metavar: "platform", nargs: "?",
  68. help: "A platform to target. Choose from " + cli.PLATFORMS.join(", ") + ". If omitted, 'default_platform' will be used."});
  69. addCommonArguments(cmd);
  70. cmd.addArgument(["--no-build"], {action: "storeTrue", help: "Don't rebuild before running."});
  71. cmd.setDefaults({action: function (args) {
  72. cli.run(args.platform, {
  73. debug: args.debug,
  74. noBuild: args.no_build,
  75. });
  76. }});
  77. var cmd = commands.addParser("platform-add", {help: "Adds a platform to the project",
  78. description: "Adds a platform to the project"});
  79. cmd.addArgument(["platform"], {help: "The platform to add (windows|mac|ios|android|ios)"});
  80. cmd.setDefaults({action: function (args) {
  81. cli.addPlatform(args.platform)
  82. .then(function () {
  83. console.log("Platform added " + path.resolve(args.path));
  84. })
  85. .catch(function (error) {
  86. console.error("Error: Platform Could not be added " + path.resolve(args.path));
  87. process.exit(1);
  88. });
  89. }});
  90. var cmd = commands.addParser("serve", {help: "Start a development server.",
  91. description: "Starts a development web server for testing.",
  92. aliases: ["server"]});
  93. cmd.setDefaults({action: function (args) {
  94. var server = new cli.Server();
  95. server.start();
  96. }});
  97. var cmd = commands.addParser("editor", {help: "Starts the Atomic Editor and loads current project",
  98. description: "Starts the Atomic Editor and loads current project.",
  99. aliases: ["edit"]});
  100. cmd.setDefaults({action: function (args) {
  101. cli.editor()
  102. }});
  103. // GO!
  104. if (process.argv.length > 2) {
  105. var args = parser.parseArgs();
  106. args.action(args);
  107. } else {
  108. parser.printHelp();
  109. }