Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

precompile.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var _require = require('./lib'),
  5. _prettifyError = _require._prettifyError;
  6. var compiler = require('./compiler');
  7. var _require2 = require('./environment'),
  8. Environment = _require2.Environment;
  9. var precompileGlobal = require('./precompile-global');
  10. function match(filename, patterns) {
  11. if (!Array.isArray(patterns)) {
  12. return false;
  13. }
  14. return patterns.some(function (pattern) {
  15. return filename.match(pattern);
  16. });
  17. }
  18. function precompileString(str, opts) {
  19. opts = opts || {};
  20. opts.isString = true;
  21. var env = opts.env || new Environment([]);
  22. var wrapper = opts.wrapper || precompileGlobal;
  23. if (!opts.name) {
  24. throw new Error('the "name" option is required when compiling a string');
  25. }
  26. return wrapper([_precompile(str, opts.name, env)], opts);
  27. }
  28. function precompile(input, opts) {
  29. // The following options are available:
  30. //
  31. // * name: name of the template (auto-generated when compiling a directory)
  32. // * isString: input is a string, not a file path
  33. // * asFunction: generate a callable function
  34. // * force: keep compiling on error
  35. // * env: the Environment to use (gets extensions and async filters from it)
  36. // * include: which file/folders to include (folders are auto-included, files are auto-excluded)
  37. // * exclude: which file/folders to exclude (folders are auto-included, files are auto-excluded)
  38. // * wrapper: function(templates, opts) {...}
  39. // Customize the output format to store the compiled template.
  40. // By default, templates are stored in a global variable used by the runtime.
  41. // A custom loader will be necessary to load your custom wrapper.
  42. opts = opts || {};
  43. var env = opts.env || new Environment([]);
  44. var wrapper = opts.wrapper || precompileGlobal;
  45. if (opts.isString) {
  46. return precompileString(input, opts);
  47. }
  48. var pathStats = fs.existsSync(input) && fs.statSync(input);
  49. var precompiled = [];
  50. var templates = [];
  51. function addTemplates(dir) {
  52. fs.readdirSync(dir).forEach(function (file) {
  53. var filepath = path.join(dir, file);
  54. var subpath = filepath.substr(path.join(input, '/').length);
  55. var stat = fs.statSync(filepath);
  56. if (stat && stat.isDirectory()) {
  57. subpath += '/';
  58. if (!match(subpath, opts.exclude)) {
  59. addTemplates(filepath);
  60. }
  61. } else if (match(subpath, opts.include)) {
  62. templates.push(filepath);
  63. }
  64. });
  65. }
  66. if (pathStats.isFile()) {
  67. precompiled.push(_precompile(fs.readFileSync(input, 'utf-8'), opts.name || input, env));
  68. } else if (pathStats.isDirectory()) {
  69. addTemplates(input);
  70. for (var i = 0; i < templates.length; i++) {
  71. var name = templates[i].replace(path.join(input, '/'), '');
  72. try {
  73. precompiled.push(_precompile(fs.readFileSync(templates[i], 'utf-8'), name, env));
  74. } catch (e) {
  75. if (opts.force) {
  76. // Don't stop generating the output if we're
  77. // forcing compilation.
  78. console.error(e); // eslint-disable-line no-console
  79. } else {
  80. throw e;
  81. }
  82. }
  83. }
  84. }
  85. return wrapper(precompiled, opts);
  86. }
  87. function _precompile(str, name, env) {
  88. env = env || new Environment([]);
  89. var asyncFilters = env.asyncFilters;
  90. var extensions = env.extensionsList;
  91. var template;
  92. name = name.replace(/\\/g, '/');
  93. try {
  94. template = compiler.compile(str, asyncFilters, extensions, name, env.opts);
  95. } catch (err) {
  96. throw _prettifyError(name, false, err);
  97. }
  98. return {
  99. name: name,
  100. template: template
  101. };
  102. }
  103. module.exports = {
  104. precompile: precompile,
  105. precompileString: precompileString
  106. };