index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. (function() {
  2. var chalk, extendOptions, globalOptions, logger, main, parseArgv, path, usage,
  3. indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
  4. chalk = require('chalk');
  5. parseArgv = require('minimist');
  6. path = require('path');
  7. logger = require('./../core/logger').logger;
  8. extendOptions = require('./common').extendOptions;
  9. usage = "\nusage: wintersmith [options] [command]\n\ncommands:\n\n " + (chalk.bold('build')) + " [options] - build a site\n " + (chalk.bold('preview')) + " [options] - run local webserver\n " + (chalk.bold('new')) + " <location> - create a new site\n " + (chalk.bold('plugin')) + " - manage plugins\n\n also see [command] --help\n\nglobal options:\n\n -v, --verbose show debug information\n -q, --quiet only output critical errors\n -V, --version output version and exit\n -h, --help show help\n";
  10. globalOptions = {
  11. boolean: ['verbose', 'quiet', 'version', 'help'],
  12. alias: {
  13. verbose: 'v',
  14. quiet: 'q',
  15. version: 'V',
  16. help: 'h'
  17. }
  18. };
  19. main = function(argv) {
  20. var cmd, error, opts;
  21. opts = parseArgv(argv, globalOptions);
  22. cmd = opts._[2];
  23. if (cmd != null) {
  24. try {
  25. cmd = require("./" + cmd);
  26. } catch (error1) {
  27. error = error1;
  28. if (error.code === 'MODULE_NOT_FOUND') {
  29. console.log("'" + cmd + "' - no such command");
  30. process.exit(1);
  31. } else {
  32. throw error;
  33. }
  34. }
  35. }
  36. if (opts.version) {
  37. console.log(require('./version'));
  38. process.exit(0);
  39. }
  40. if (opts.help || !cmd) {
  41. console.log(cmd ? cmd.usage : usage);
  42. process.exit(0);
  43. }
  44. if (opts.verbose) {
  45. if (indexOf.call(argv, '-vv') >= 0) {
  46. logger.transports[0].level = 'silly';
  47. } else {
  48. logger.transports[0].level = 'verbose';
  49. }
  50. }
  51. if (opts.quiet) {
  52. logger.transports[0].quiet = true;
  53. }
  54. if (cmd) {
  55. extendOptions(cmd.options, globalOptions);
  56. opts = parseArgv(argv, cmd.options);
  57. return cmd(opts);
  58. }
  59. };
  60. module.exports.main = main;
  61. }).call(this);