indexHandlers.js 5.9 KB

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