cli.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 cmd = commands.addParser("platform-add", {help: "Adds a platform to the project",
  63. description: "Adds a platform to the project"});
  64. cmd.addArgument(["platform"], {help: "The platform to add (windows|mac|ios|android|ios)"});
  65. cmd.setDefaults({action: function (args) {
  66. cli.addPlatform(args.platform)
  67. .then(function () {
  68. console.log("Platform added " + path.resolve(args.path));
  69. })
  70. .catch(function (error) {
  71. console.error("Error: Platform Could not be added " + path.resolve(args.path));
  72. process.exit(1);
  73. });
  74. }});
  75. var cmd = commands.addParser("serve", {help: "Start a development server.",
  76. description: "Starts a development web server for testing.",
  77. aliases: ["server"]});
  78. cmd.setDefaults({action: function (args) {
  79. var server = new cli.Server();
  80. server.start();
  81. }});
  82. // GO!
  83. if (process.argv.length > 2) {
  84. var args = parser.parseArgs();
  85. args.action(args);
  86. } else {
  87. parser.printHelp();
  88. }
  89. // spawn(cli.ATOMIC_TOOL_BIN, { stdio: 'inherit' });