indexHandlers.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 chokidar = require('chokidar');
  8. var indexTmplPath = path.normalize(__dirname + '/../html/index.mustache.html');
  9. var indexTpml;
  10. var {
  11. readFilesAsync
  12. } = require('../lib/fsio');
  13. var translator = require('../lib/translator');
  14. var passLog = require('../lib/passLog');
  15. // One-liner for current directory, ignores .dotfiles
  16. chokidar.watch('./html', {ignored: /(^|[\/\\])\../}).on('all', (event, path) => {
  17. // console.log(event, path);
  18. if(path === 'html/index.mustache.html') {
  19. console.log('reload index html');
  20. indexTpml = fs.readFileSync(indexTmplPath).toString();
  21. }
  22. });
  23. module.exports = function(exStore, exDir) {
  24. function readExampleFiles(repoSlug, exampleSlug, config) {
  25. const exampleDir = exDir + '/' + repoSlug + '/' + exampleSlug;
  26. const libsCssDir = path.normalize(__dirname + '/../css/vendor');
  27. const libsJsDir = path.normalize(__dirname + '/../js/vendor');
  28. const { html, js, css } = config; // libsCss, libsJs
  29. const files = [].concat(html, js, css);
  30. return readFilesAsync(exampleDir, files);
  31. }
  32. function renderIndex(req, withRepo, withExample) {
  33. // Extract repoSlug and exampleSlug from req.params
  34. const { locale, params: { repoSlug, exampleSlug } } = req;
  35. let repo;
  36. let menuExample;
  37. let statusCode;
  38. // Initialize view data
  39. let data = {
  40. menuRepo: exStore.getRepoMenu(),
  41. _: translator.getAll(locale)
  42. };
  43. // Fetch example repository if needed
  44. if(withRepo) {
  45. data.repo = exStore.getRepo(repoSlug);
  46. if(! data.repo) {
  47. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  48. data.errorMessage = translator.getOne(locale, "repoNotFound", [repoSlug]); //'Repo ' + params.repoSlug + ' not found';
  49. statusCode = 404;
  50. }
  51. else {
  52. data.menuExample = exStore.getExampleMenu(data.repo.path);
  53. data.showControls = true;
  54. }
  55. }
  56. // Fetch example if needed
  57. if(withExample && data.repo) {
  58. data.example = _.find(data.repo.examples, { slug: exampleSlug });
  59. if(! data.example) {
  60. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  61. data.errorMessage = translator.getOne(locale, "exampleNotFound", [repoSlug, exampleSlug]);
  62. statusCode = 404;
  63. }
  64. else {
  65. data.showEditor = true;
  66. }
  67. }
  68. // Mustache.render(indexTpml, data);
  69. return (
  70. exampleSlug && data.example ?
  71. readExampleFiles(repoSlug, exampleSlug, data.example) : Promise.resolve([])
  72. ).then(files =>
  73. ({ files, filesJSON: JSON.stringify(files) })
  74. )
  75. .then(({ files, filesJSON }) => Object.assign(data, { files, filesJSON }))
  76. .then(passLog('data before rendering, path: ' + req.path))
  77. .then(data => ({
  78. html: Mustache.render(indexTpml, data),
  79. code: statusCode ? statusCode : 200
  80. }));
  81. }
  82. return {
  83. /**
  84. * Extract language header from req
  85. */
  86. getAcceptLanguage: function (req, res, next) {
  87. const acceptLanguageHdr = req.headers["accept-language"];
  88. const re = /[a-z]{2}\-[A-Z]{2}/;
  89. const matches = re.exec(acceptLanguageHdr);
  90. if(matches) {
  91. req.locale = matches[0];
  92. }
  93. next();
  94. },
  95. /**
  96. * Get bare index without repo or examples
  97. */
  98. getIndexBare: function(req, res) {
  99. renderIndex(req)
  100. .then(({ html, code }) => res.send(html));
  101. },
  102. /**
  103. * Get index with repo selected only
  104. */
  105. getIndexRepo: function(req, res) {
  106. renderIndex(req, true)
  107. .then(({ html, code }) => res.status(code).send(html));
  108. // const repo = exStore.getRepo(req.params.repoSlug);
  109. // if(! repo) {
  110. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  111. // }
  112. // const menuRepo = exStore.getRepoMenu();
  113. // const menuExample = exStore.getExampleMenu(repo.path);
  114. // // const title = 'Home';
  115. // // console.log(menuExample);
  116. // res.send(Mustache.render(indexTpml, {
  117. // // title,
  118. // menuRepo,
  119. // menuExample,
  120. // filesJSON: '[]'
  121. // }));
  122. },
  123. /**
  124. * Get index with selected repo&example
  125. */
  126. getIndexExample: function(req, res) {
  127. renderIndex(req, true, true)
  128. .then(({ html, code }) => res.status(code).send(html));
  129. // console.log('getIndexExample', req.params);
  130. // const repo = exStore.getRepo(req.params.repoSlug);
  131. // if(! repo) {
  132. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  133. // }
  134. // const menuRepo = exStore.getRepoMenu();
  135. // const menuExample = exStore.getExampleMenu(repo.path);
  136. // const example = _.find(repo.examples, { slug: req.params.exampleSlug });
  137. // if(! example) {
  138. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  139. // }
  140. // // console.log('#1');
  141. // // const title = 'Home';
  142. // // console.log(menuExample);
  143. // const { repoSlug, exampleSlug } = req.params;
  144. // readExampleFiles(repoSlug, exampleSlug, example)
  145. // .then(files => {
  146. // console.log('example files', files);
  147. // res.send(Mustache.render(indexTpml, {
  148. // // title,
  149. // menuRepo,
  150. // menuExample,
  151. // files,
  152. // filesJSON: JSON.stringify(files)
  153. // }));
  154. // });
  155. }
  156. };
  157. };