partHandlers.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* global __dirname */
  2. "use strict";
  3. var _ = require('lodash');
  4. var Mustache = require('mustache');
  5. var path = require('path');
  6. var fs = require('fs');
  7. var {
  8. readFilesAsync
  9. } = require('../lib/fsio');
  10. var translator = require('../lib/translator');
  11. var passLog = require('../lib/passLog');
  12. module.exports = function(exStore, exDir) {
  13. function readExampleFiles(repoSlug, exampleSlug, config) {
  14. const exampleDir = exDir + '/' + repoSlug + '/' + exampleSlug;
  15. const libsCssDir = path.normalize(__dirname + '/../css/vendor');
  16. const libsJsDir = path.normalize(__dirname + '/../js/vendor');
  17. const { html, js, css } = config; // libsCss, libsJs
  18. const files = [].concat(html, js, css);
  19. return readFilesAsync(exampleDir, files);
  20. }
  21. function renderPart(req, withRepo, withExample) {
  22. // Extract repoSlug and exampleSlug from req.params
  23. const { locale, params: { repoSlug, exampleSlug } } = req;
  24. let repo;
  25. let menuExample;
  26. let statusCode;
  27. // Initialize view data
  28. let data = {
  29. // menuRepo: exStore.getRepoMenu(),
  30. _: translator.getAll(locale)
  31. };
  32. // Fetch example repository if needed
  33. if(withRepo) {
  34. data.repo = exStore.getRepo(repoSlug);
  35. if(! data.repo) {
  36. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  37. data.errorMessage = translator.getOne(locale, "repoNotFound", [repoSlug]); //'Repo ' + params.repoSlug + ' not found';
  38. statusCode = 404;
  39. }
  40. else {
  41. data.menuExample = exStore.getExampleMenu(data.repo.path);
  42. }
  43. }
  44. // Fetch example if needed
  45. if(withExample && data.repo) {
  46. data.example = _.find(data.repo.examples, { slug: exampleSlug });
  47. if(! data.example) {
  48. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  49. data.errorMessage = translator.getOne(locale, "exampleNotFound", [repoSlug, exampleSlug]);
  50. statusCode = 404;
  51. }
  52. }
  53. // Mustache.render(indexTpml, data);
  54. return (
  55. exampleSlug && data.example ?
  56. readExampleFiles(repoSlug, exampleSlug, data.example) : Promise.resolve([])
  57. ).then(files => Object.assign(data, { files }))
  58. .then(passLog('data before rendering parts, path: ' + req.path));
  59. }
  60. return {
  61. /**
  62. * Get bare index without repo or examples
  63. */
  64. // getIndexBare: function(req, res) {
  65. // renderIndex(req)
  66. // .then(({ html, code }) => res.send(html));
  67. // },
  68. /**
  69. * Get index with repo selected only
  70. */
  71. getPartsRepo: function(req, res) {
  72. renderPart(req, true)
  73. .then(data => res.json(data));
  74. },
  75. /**
  76. * Get index with selected repo&example
  77. */
  78. getPartsExample: function(req, res) {
  79. renderPart(req, true, true)
  80. .then(data => res.json(data));
  81. }
  82. };
  83. };