indexHandlers.js 6.0 KB

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