index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var walk = require('pug-walk');
  5. var assign = require('object-assign');
  6. module.exports = load;
  7. function load(ast, options) {
  8. options = getOptions(options);
  9. // clone the ast
  10. ast = JSON.parse(JSON.stringify(ast));
  11. return walk(ast, function (node) {
  12. if (node.str === undefined) {
  13. if (node.type === 'Include' || node.type === 'RawInclude' || node.type === 'Extends') {
  14. var file = node.file;
  15. if (file.type !== 'FileReference') {
  16. throw new Error('Expected file.type to be "FileReference"');
  17. }
  18. var path, str;
  19. try {
  20. path = options.resolve(file.path, file.filename, options);
  21. file.fullPath = path;
  22. str = options.read(path, options);
  23. } catch (ex) {
  24. ex.message += '\n at ' + node.filename + ' line ' + node.line;
  25. throw ex;
  26. }
  27. file.str = str;
  28. if (node.type === 'Extends' || node.type === 'Include') {
  29. file.ast = load.string(str, assign({}, options, {
  30. filename: path
  31. }));
  32. }
  33. }
  34. }
  35. });
  36. }
  37. load.string = function loadString(src, options) {
  38. options = assign(getOptions(options), {
  39. src: src
  40. });
  41. var tokens = options.lex(src, options);
  42. var ast = options.parse(tokens, options);
  43. return load(ast, options);
  44. };
  45. load.file = function loadFile(filename, options) {
  46. options = assign(getOptions(options), {
  47. filename: filename
  48. });
  49. var str = options.read(filename);
  50. return load.string(str, options);
  51. };
  52. load.resolve = function resolve(filename, source, options) {
  53. filename = filename.trim();
  54. if (filename[0] !== '/' && !source)
  55. throw new Error('the "filename" option is required to use includes and extends with "relative" paths');
  56. if (filename[0] === '/' && !options.basedir)
  57. throw new Error('the "basedir" option is required to use includes and extends with "absolute" paths');
  58. filename = path.join(filename[0] === '/' ? options.basedir : path.dirname(source.trim()), filename);
  59. return filename;
  60. };
  61. load.read = function read(filename, options) {
  62. return fs.readFileSync(filename, 'utf8');
  63. };
  64. load.validateOptions = function validateOptions(options) {
  65. /* istanbul ignore if */
  66. if (typeof options !== 'object') {
  67. throw new TypeError('options must be an object');
  68. }
  69. /* istanbul ignore if */
  70. if (typeof options.lex !== 'function') {
  71. throw new TypeError('options.lex must be a function');
  72. }
  73. /* istanbul ignore if */
  74. if (typeof options.parse !== 'function') {
  75. throw new TypeError('options.parse must be a function');
  76. }
  77. /* istanbul ignore if */
  78. if (options.resolve && typeof options.resolve !== 'function') {
  79. throw new TypeError('options.resolve must be a function');
  80. }
  81. /* istanbul ignore if */
  82. if (options.read && typeof options.read !== 'function') {
  83. throw new TypeError('options.read must be a function');
  84. }
  85. };
  86. function getOptions(options) {
  87. load.validateOptions(options);
  88. return assign({
  89. resolve: load.resolve,
  90. read: load.read
  91. }, options);
  92. }