ExampleStore.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. repoDescriptor.fullPath = this.rootPath + '/' + repoPath;
  45. return fs.mkdirAsync(fullPath)
  46. .then(() => fs.writeFileAsync(repoConfigFile, repoConfig))
  47. .then(() => self.repos.push(repoDescriptor));
  48. }
  49. ExampleStore.prototype.loadExample = function(repo, slug) {
  50. const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
  51. repo.examples.push(Object.assign({ slug }, exampleConfig));
  52. return exampleConfig;
  53. };
  54. ExampleStore.prototype.addExampleFile = function(repoSlug, slug, filename) {
  55. const repo = this.getRepo(repoSlug);
  56. const example = _.find(repo.examples, { slug });
  57. let ext = path.extname(filename).substr(1);
  58. const target = repo.fullPath + '/' + slug + '/config.json';
  59. example[ext].push(filename);
  60. console.log('after adding file', _.find(repo.examples, { slug }));
  61. const config = _.clone(example);
  62. delete config.slug;
  63. configJSON = beautify(config, null, 2, 100);
  64. return fs.writeFileAsync(target, configJSON);
  65. };
  66. ExampleStore.prototype.getList = function(path) {
  67. return _.find(this.repos, { path });
  68. }
  69. ExampleStore.prototype.getRepoMenu = function(path) {
  70. return this.repos.map(
  71. ({ title, path }) => ({ title, slug: path })
  72. )
  73. };
  74. ExampleStore.prototype.getExampleMenu = function(path) {
  75. const repo = _.find(this.repos, { path });
  76. if(! repo.categories) {
  77. console.error('Repo ' + repo.title + 'has no categories key');
  78. return [];
  79. }
  80. console.log('getExampleMenu repo examples', repo.examples)
  81. const menu = repo.categories.map(category => {
  82. const examplesInCat = repo.examples.filter(ex => (ex.category === category.slug));
  83. // console.log('examples in cat', category, examplesInCat);
  84. const examples = examplesInCat.map(
  85. ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  86. );
  87. return { category, examples };
  88. });
  89. // console.log(menu);
  90. return menu;
  91. // return repo.examples.map(
  92. // ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
  93. // );
  94. };
  95. ExampleStore.prototype.getMenu = function(path) {
  96. const self = this;
  97. return '<ul>' + self.repos.reduce((menu, repo) => {
  98. return menu + '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
  99. (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
  100. '') + '</ul></li>';
  101. }, '') + '</ul>';
  102. }
  103. ExampleStore.prototype.getRepo = function(path) {
  104. return _.find(this.repos, { path });
  105. }
  106. module.exports = ExampleStore;