new.js 997 B

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