ExampleStore.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const {
  2. scandirAsync
  3. } = require('./fsio');
  4. const Promise = require('bluebird');
  5. const _ = require('lodash');
  6. const beautify = require('json-beautify');
  7. const fs = require('fs');
  8. function ExampleStore(path) {
  9. this.rootPath = path;
  10. this.repos = [];
  11. }
  12. ExampleStore.prototype.init = function() {
  13. const loadRepository = this.loadRepository.bind(this);
  14. return scandirAsync(this.rootPath, ['.gitkeep'])
  15. .then(repositories => Promise.map(
  16. repositories, loadRepository
  17. ));
  18. };
  19. ExampleStore.prototype.loadRepository = function(repoPath) {
  20. const loadExample = this.loadExample.bind(this);
  21. const fullPath = this.rootPath + '/' + repoPath;
  22. const repoConfig = require(fullPath + '/repo-config.json');
  23. const repoDescriptor = Object.assign(repoConfig, {
  24. path: repoPath,
  25. fullPath,
  26. examples: []
  27. });
  28. this.repos.push(repoDescriptor);
  29. return scandirAsync(fullPath, ['.gitkeep', 'repo-config.json'])
  30. .then(examples => Promise.map(
  31. examples, example => loadExample(repoDescriptor, example)
  32. ));
  33. // .then(() => console.log(this.repos[0]))
  34. };
  35. ExampleStore.prototype.addRepository = function(repoPath, repoDescriptor) {
  36. // Prepare files to write
  37. const self = this;
  38. const fullPath = this.rootPath + '/' + repoPath;
  39. const repoConfigFile = fullPath + '/repo-config.json';
  40. const repoConfig = beautify(repoDescriptor, null, 2, 100);
  41. return fs.mkdirAsync(fullPath)
  42. .then(() => fs.writeFileAsync(repoConfigFile, repoConfig))
  43. .then(() => self.repos.push(repoDescriptor));
  44. }
  45. ExampleStore.prototype.loadExample = function(repo, slug) {
  46. const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
  47. repo.examples.push(Object.assign({ slug }, exampleConfig));
  48. return exampleConfig;
  49. };
  50. ExampleStore.prototype.getList = function(path) {
  51. return _.find(this.repos, { path });
  52. }
  53. ExampleStore.prototype.getRepoMenu = function(path) {
  54. return this.repos.map(
  55. ({ title, path }) => ({ title, slug: path })
  56. )
  57. };
  58. ExampleStore.prototype.getExampleMenu = function(path) {
  59. const repo = _.find(this.repos, { path });
  60. if(! repo.categories) {
  61. console.error('Repo ' + repo.title + 'has no categories key');
  62. return [];
  63. }
  64. console.log('getExampleMenu repo examples', repo.examples)
  65. const menu = repo.categories.map(category => {
  66. const examplesInCat = repo.examples.filter(ex => (ex.category === category.slug));
  67. // console.log('examples in cat', category, examplesInCat);
  68. const examples = examplesInCat.map(
  69. ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  70. );
  71. return { category, examples };
  72. });
  73. // console.log(menu);
  74. return menu;
  75. // return repo.examples.map(
  76. // ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  77. // );
  78. };
  79. ExampleStore.prototype.getMenu = function(path) {
  80. const self = this;
  81. return '<ul>' + self.repos.reduce((menu, repo) => {
  82. return menu + '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
  83. (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
  84. '') + '</ul></li>';
  85. }, '') + '</ul>';
  86. }
  87. ExampleStore.prototype.getRepo = function(path) {
  88. return _.find(this.repos, { path });
  89. }
  90. module.exports = ExampleStore;