| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- const scandir = require('./scandir');
- 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 scandir(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 = {
- title: repoConfig.title,
- path: repoPath,
- fullPath,
- examples: []
- };
- this.repos.push(repoDescriptor);
- return scandir(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 });
- 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;
|