post.js 922 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. const { gray, magenta, underline } = require('chalk');
  3. const table = require('text-table');
  4. const { stringLength } = require('./common');
  5. function mapName(item) {
  6. return item.name;
  7. }
  8. function listPost() {
  9. const Post = this.model('Post');
  10. const data = Post.sort({published: -1, date: 1}).map(post => {
  11. const date = post.published ? post.date.format('YYYY-MM-DD') : 'Draft';
  12. const tags = post.tags.map(mapName);
  13. const categories = post.categories.map(mapName);
  14. return [
  15. gray(date),
  16. post.title,
  17. magenta(post.source),
  18. categories.join(', '),
  19. tags.join(', ')
  20. ];
  21. });
  22. // Table header
  23. const header = ['Date', 'Title', 'Path', 'Category', 'Tags'].map(str => underline(str));
  24. data.unshift(header);
  25. const t = table(data, {
  26. stringLength
  27. });
  28. console.log(t);
  29. if (data.length === 1) console.log('No posts.');
  30. }
  31. module.exports = listPost;