ExampleStore.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const { scandirAsync } = require('./fsio');
  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 scandirAsync(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 scandirAsync(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. if(! repo.categories) {
  47. console.error('Repo ' + repo.title + 'has no categories key');
  48. return [];
  49. }
  50. // console.log('repo examples', repo.examples)
  51. const menu = repo.categories.map(category => {
  52. const examplesInCat = repo.examples.filter(ex => (ex.category === category.slug));
  53. // console.log('examples in cat', category, examplesInCat);
  54. const examples = examplesInCat.map(
  55. ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  56. );
  57. return { category, examples };
  58. });
  59. // console.log(menu);
  60. return menu;
  61. // return repo.examples.map(
  62. // ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  63. // );
  64. };
  65. ExampleStore.prototype.getMenu = function(path) {
  66. const self = this;
  67. return '<ul>' + self.repos.reduce((menu, repo) => {
  68. return menu + '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
  69. (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
  70. '') + '</ul></li>';
  71. }, '') + '</ul>';
  72. }
  73. ExampleStore.prototype.getRepo = function(path) {
  74. return _.find(this.repos, { path });
  75. }
  76. module.exports = ExampleStore;