ExampleStore.js 3.8 KB

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