ExampleStore.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const scandir = require('./scandir');
  2. var Promise = require('bluebird');
  3. var _ = require('lodash');
  4. function ExampleStore(path) {
  5. this.rootPath = path;
  6. this.repos = [];
  7. }
  8. ExampleStore.prototype.init = function() {
  9. const loadRepository = this.loadRepository.bind(this);
  10. return scandir(this.rootPath, ['.gitkeep'])
  11. .then(repositories => Promise.map(
  12. repositories, loadRepository
  13. ));
  14. };
  15. ExampleStore.prototype.loadRepository = function(repoPath) {
  16. const loadExample = this.loadExample.bind(this);
  17. const fullPath = this.rootPath + '/' + repoPath;
  18. const repoConfig = require(fullPath + '/repo-config.json');
  19. const repoDescriptor = Object.assign(repoConfig, {
  20. path: repoPath,
  21. fullPath,
  22. examples: []
  23. });
  24. this.repos.push(repoDescriptor);
  25. return scandir(fullPath, ['.gitkeep', 'repo-config.json'])
  26. .then(examples => Promise.map(
  27. examples, example => loadExample(repoDescriptor, example)
  28. ))
  29. .then(() => console.log(this.repos[0]))
  30. };
  31. ExampleStore.prototype.loadExample = function(repo, slug) {
  32. const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
  33. repo.examples.push(Object.assign({ slug }, exampleConfig));
  34. return exampleConfig;
  35. };
  36. ExampleStore.prototype.getList = function(path) {
  37. return _.find(this.repos, { path });
  38. }
  39. ExampleStore.prototype.getRepoMenu = function(path) {
  40. return this.repos.map(
  41. ({ title, path }) => ({ title, slug: path })
  42. )
  43. };
  44. ExampleStore.prototype.getExampleMenu = function(path) {
  45. const repo = _.find(this.repos, { path });
  46. console.log('repo examples', repo.examples)
  47. const menu = repo.categories.map(category => {
  48. const examplesInCat = repo.examples.filter(ex => (ex.category === category.slug));
  49. console.log('examples in cat', category, examplesInCat);
  50. const examples = examplesInCat.map(
  51. ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  52. );
  53. return { category, examples };
  54. });
  55. console.log(menu);
  56. return menu;
  57. // return repo.examples.map(
  58. // ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  59. // );
  60. };
  61. ExampleStore.prototype.getMenu = function(path) {
  62. const self = this;
  63. return '<ul>' + self.repos.reduce((menu, repo) => {
  64. return menu + '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
  65. (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
  66. '') + '</ul></li>';
  67. }, '') + '</ul>';
  68. }
  69. ExampleStore.prototype.getRepo = function(path) {
  70. return _.find(this.repos, { path });
  71. }
  72. module.exports = ExampleStore;