new.js 995 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. 'use strict';
  2. const tildify = require('tildify');
  3. const chalk = require('chalk');
  4. const reservedKeys = {
  5. _: true,
  6. title: true,
  7. layout: true,
  8. slug: true,
  9. path: true,
  10. replace: true,
  11. // Global options
  12. config: true,
  13. debug: true,
  14. safe: true,
  15. silent: true
  16. };
  17. function newConsole(args) {
  18. // Display help message if user didn't input any arguments
  19. if (!args._.length) {
  20. return this.call('help', {_: ['new']});
  21. }
  22. const data = {
  23. title: args._.pop(),
  24. layout: args._.length ? args._[0] : this.config.default_layout,
  25. slug: args.s || args.slug,
  26. path: args.p || args.path
  27. };
  28. const keys = Object.keys(args);
  29. let key = '';
  30. const self = this;
  31. for (let i = 0, len = keys.length; i < len; i++) {
  32. key = keys[i];
  33. if (!reservedKeys[key]) data[key] = args[key];
  34. }
  35. return this.post.create(data, args.r || args.replace).then(post => {
  36. self.log.info('Created: %s', chalk.magenta(tildify(post.path)));
  37. });
  38. }
  39. module.exports = newConsole;