ExampleStore.js 3.8 KB

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