indexHandlers.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, testMode) {
  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. testMode,
  42. _: translator.getAll(locale)
  43. };
  44. // Fetch example repository if needed
  45. if(withRepo) {
  46. data.repo = exStore.getRepo(repoSlug);
  47. if(! data.repo) {
  48. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  49. data.errorMessage = translator.getOne(locale, "repoNotFound", [repoSlug]); //'Repo ' + params.repoSlug + ' not found';
  50. statusCode = 404;
  51. }
  52. else {
  53. data.menuExample = exStore.getExampleMenu(data.repo.path);
  54. data.showControls = true;
  55. }
  56. }
  57. // Fetch example if needed
  58. if(withExample && data.repo) {
  59. data.example = _.find(data.repo.examples, { slug: exampleSlug });
  60. if(! data.example) {
  61. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  62. data.errorMessage = translator.getOne(locale, "exampleNotFound", [repoSlug, exampleSlug]);
  63. statusCode = 404;
  64. }
  65. else {
  66. data.showEditor = true;
  67. }
  68. }
  69. // Mustache.render(indexTpml, data);
  70. return (
  71. exampleSlug && data.example ?
  72. readExampleFiles(repoSlug, exampleSlug, data.example) : Promise.resolve([])
  73. ).then(files =>
  74. ({ files, filesJSON: JSON.stringify(files) })
  75. )
  76. .then(({ files, filesJSON }) => Object.assign(data, { files, filesJSON }))
  77. .then(passLog('data before rendering, path: ' + req.path))
  78. .then(data => ({
  79. html: Mustache.render(indexTpml, data),
  80. code: statusCode ? statusCode : 200
  81. }));
  82. }
  83. return {
  84. /**
  85. * Extract language header from req
  86. */
  87. getAcceptLanguage: function (req, res, next) {
  88. const acceptLanguageHdr = req.headers["accept-language"];
  89. const re = /[a-z]{2}\-[A-Z]{2}/;
  90. const matches = re.exec(acceptLanguageHdr);
  91. if(matches) {
  92. req.locale = matches[0];
  93. }
  94. next();
  95. },
  96. /**
  97. * Get bare index without repo or examples
  98. */
  99. getIndexBare: function(req, res) {
  100. renderIndex(req)
  101. .then(({ html, code }) => res.send(html));
  102. },
  103. /**
  104. * Get bare index without repo or examples,
  105. * for running tests with QUnit
  106. */
  107. getIndexTest: function(req, res) {
  108. renderIndex(req, false, false, true)
  109. .then(({ html, code }) => res.send(html));
  110. },
  111. /**
  112. * Get index with repo selected only
  113. */
  114. getIndexRepo: function(req, res) {
  115. renderIndex(req, true)
  116. .then(({ html, code }) => res.status(code).send(html));
  117. // const repo = exStore.getRepo(req.params.repoSlug);
  118. // if(! repo) {
  119. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  120. // }
  121. // const menuRepo = exStore.getRepoMenu();
  122. // const menuExample = exStore.getExampleMenu(repo.path);
  123. // // const title = 'Home';
  124. // // console.log(menuExample);
  125. // res.send(Mustache.render(indexTpml, {
  126. // // title,
  127. // menuRepo,
  128. // menuExample,
  129. // filesJSON: '[]'
  130. // }));
  131. },
  132. /**
  133. * Get index with selected repo&example
  134. */
  135. getIndexExample: function(req, res) {
  136. renderIndex(req, true, true)
  137. .then(({ html, code }) => res.status(code).send(html));
  138. // console.log('getIndexExample', req.params);
  139. // const repo = exStore.getRepo(req.params.repoSlug);
  140. // if(! repo) {
  141. // return res.status(404).send('Repo ' + req.params.repoSlug + ' not found');
  142. // }
  143. // const menuRepo = exStore.getRepoMenu();
  144. // const menuExample = exStore.getExampleMenu(repo.path);
  145. // const example = _.find(repo.examples, { slug: req.params.exampleSlug });
  146. // if(! example) {
  147. // return res.status(404).send('Example ' + req.params.repoSlug + '/' + req.params.exampleSlug + ' not found');
  148. // }
  149. // // console.log('#1');
  150. // // const title = 'Home';
  151. // // console.log(menuExample);
  152. // const { repoSlug, exampleSlug } = req.params;
  153. // readExampleFiles(repoSlug, exampleSlug, example)
  154. // .then(files => {
  155. // console.log('example files', files);
  156. // res.send(Mustache.render(indexTpml, {
  157. // // title,
  158. // menuRepo,
  159. // menuExample,
  160. // files,
  161. // filesJSON: JSON.stringify(files)
  162. // }));
  163. // });
  164. }
  165. };
  166. };