dev.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. const logger = require('@alias/logger')('growi:crowi:dev');
  2. const path = require('path');
  3. const { listLocaleIds } = require('@commons/util/locale-utils');
  4. const swig = require('swig-templates');
  5. const onHeaders = require('on-headers');
  6. class CrowiDev {
  7. /**
  8. * Creates an instance of CrowiDev.
  9. * @param {Crowi} crowi
  10. *
  11. * @memberOf CrowiDev
  12. */
  13. constructor(crowi) {
  14. this.crowi = crowi;
  15. }
  16. init() {
  17. this.requireForAutoReloadServer();
  18. this.initPromiseRejectionWarningHandler();
  19. this.initSwig();
  20. }
  21. initPromiseRejectionWarningHandler() {
  22. // https://qiita.com/syuilo/items/0800d7e44e93203c7285
  23. process.on('unhandledRejection', console.dir); // eslint-disable-line no-console
  24. }
  25. initSwig() {
  26. swig.setDefaults({ cache: false });
  27. }
  28. /**
  29. * require files for node-dev auto reloading
  30. */
  31. requireForAutoReloadServer() {
  32. // load all json files for live reloading
  33. listLocaleIds()
  34. .forEach((localeId) => {
  35. require(path.join(this.crowi.localeDir, localeId, 'translation.json'));
  36. });
  37. }
  38. /**
  39. *
  40. * @param {any} app express
  41. */
  42. setupServer(app) {
  43. const port = this.crowi.port;
  44. let server = app;
  45. // for log
  46. let serverUrl = `http://localhost:${port}}`;
  47. if (this.crowi.env.DEV_HTTPS) {
  48. logger.info(`[${this.crowi.node_env}] Express server will start with HTTPS Self-Signed Certification`);
  49. serverUrl = `https://localhost:${port}}`;
  50. const fs = require('graceful-fs');
  51. const https = require('https');
  52. const options = {
  53. key: fs.readFileSync(path.join(this.crowi.rootDir, './resource/certs/localhost/key.pem')),
  54. cert: fs.readFileSync(path.join(this.crowi.rootDir, './resource/certs/localhost/cert.pem')),
  55. };
  56. server = https.createServer(options, app);
  57. }
  58. const eazyLogger = require('eazy-logger').Logger({
  59. prefix: '[{green:GROWI}] ',
  60. useLevelPrefixes: false,
  61. });
  62. eazyLogger.info('{bold:Server URLs:}');
  63. eazyLogger.unprefixed('info', '{grey:=======================================}');
  64. eazyLogger.unprefixed('info', ` APP: {magenta:${serverUrl}}`);
  65. eazyLogger.unprefixed('info', '{grey:=======================================}');
  66. return server;
  67. }
  68. /**
  69. *
  70. * @param {any} app express
  71. */
  72. setupExpressAfterListening(app) {
  73. this.setupHeaderDebugger(app);
  74. this.setupBrowserSync(app);
  75. }
  76. setupHeaderDebugger(app) {
  77. logger.debug('setupHeaderDebugger');
  78. app.use((req, res, next) => {
  79. onHeaders(res, () => {
  80. logger.debug('HEADERS GOING TO BE WRITTEN');
  81. });
  82. next();
  83. });
  84. }
  85. setupBrowserSync(app) {
  86. logger.debug('setupBrowserSync');
  87. const browserSync = require('browser-sync');
  88. const bs = browserSync.create().init({
  89. logSnippet: false,
  90. notify: false,
  91. files: [
  92. `${this.crowi.viewsDir}/**/*.html`,
  93. `${this.crowi.publicDir}/**/*.js`,
  94. `${this.crowi.publicDir}/**/*.css`,
  95. ],
  96. });
  97. app.use(require('connect-browser-sync')(bs));
  98. }
  99. }
  100. module.exports = CrowiDev;