| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- const { scandirAsync } = require('./fsio');
- var Promise = require('bluebird');
- var _ = require('lodash');
- function ExampleStore(path) {
- this.rootPath = path;
- this.repos = [];
- }
- ExampleStore.prototype.init = function() {
- const loadRepository = this.loadRepository.bind(this);
- return scandirAsync(this.rootPath, ['.gitkeep'])
- .then(repositories => Promise.map(
- repositories, loadRepository
- ));
- };
- ExampleStore.prototype.loadRepository = function(repoPath) {
- const loadExample = this.loadExample.bind(this);
- const fullPath = this.rootPath + '/' + repoPath;
- const repoConfig = require(fullPath + '/repo-config.json');
- const repoDescriptor = Object.assign(repoConfig, {
- path: repoPath,
- fullPath,
- examples: []
- });
- this.repos.push(repoDescriptor);
- return scandirAsync(fullPath, ['.gitkeep', 'repo-config.json'])
- .then(examples => Promise.map(
- examples, example => loadExample(repoDescriptor, example)
- ));
- // .then(() => console.log(this.repos[0]))
- };
- ExampleStore.prototype.loadExample = function(repo, slug) {
- const exampleConfig = require(repo.fullPath + '/' + slug + '/config.json');
- repo.examples.push(Object.assign({ slug }, exampleConfig));
- return exampleConfig;
- };
- ExampleStore.prototype.getList = function(path) {
- return _.find(this.repos, { path });
- }
- ExampleStore.prototype.getRepoMenu = function(path) {
- return this.repos.map(
- ({ title, path }) => ({ title, slug: path })
- )
- };
- ExampleStore.prototype.getExampleMenu = function(path) {
- const repo = _.find(this.repos, { path });
- if(! repo.categories) {
- console.error('Repo ' + repo.title + 'has no categories key');
- return [];
- }
- // console.log('repo examples', repo.examples)
- const menu = repo.categories.map(category => {
- const examplesInCat = repo.examples.filter(ex => (ex.category === category.slug));
- // console.log('examples in cat', category, examplesInCat);
- const examples = examplesInCat.map(
- ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
- );
- return { category, examples };
- });
- // console.log(menu);
- return menu;
- // return repo.examples.map(
- // ({ title, slug }) => ({ title, slug: repo.path + '/' + slug })
- // );
- };
- ExampleStore.prototype.getMenu = function(path) {
- const self = this;
- return '<ul>' + self.repos.reduce((menu, repo) => {
- return menu + '<li>'+ repo.title + '<ul>' + repo.examples.reduce((submenu, example) =>
- (submenu + '<li><a href="#' + repo.path + '/' + example.slug + '">' + example.title + '</a></li>'),
- '') + '</ul></li>';
- }, '') + '</ul>';
- }
- ExampleStore.prototype.getRepo = function(path) {
- return _.find(this.repos, { path });
- }
- module.exports = ExampleStore;
|