123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- /* eslint-disable no-console */
- 'use strict';
-
- function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
-
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
-
- var fs = require('fs');
-
- var path = require('path');
-
- var Loader = require('./loader');
-
- var _require = require('./precompiled-loader.js'),
- PrecompiledLoader = _require.PrecompiledLoader;
-
- var chokidar;
-
- var FileSystemLoader = /*#__PURE__*/function (_Loader) {
- _inheritsLoose(FileSystemLoader, _Loader);
-
- function FileSystemLoader(searchPaths, opts) {
- var _this;
-
- _this = _Loader.call(this) || this;
-
- if (typeof opts === 'boolean') {
- console.log('[nunjucks] Warning: you passed a boolean as the second ' + 'argument to FileSystemLoader, but it now takes an options ' + 'object. See http://mozilla.github.io/nunjucks/api.html#filesystemloader');
- }
-
- opts = opts || {};
- _this.pathsToNames = {};
- _this.noCache = !!opts.noCache;
-
- if (searchPaths) {
- searchPaths = Array.isArray(searchPaths) ? searchPaths : [searchPaths]; // For windows, convert to forward slashes
-
- _this.searchPaths = searchPaths.map(path.normalize);
- } else {
- _this.searchPaths = ['.'];
- }
-
- if (opts.watch) {
- // Watch all the templates in the paths and fire an event when
- // they change
- try {
- chokidar = require('chokidar'); // eslint-disable-line global-require
- } catch (e) {
- throw new Error('watch requires chokidar to be installed');
- }
-
- var paths = _this.searchPaths.filter(fs.existsSync);
-
- var watcher = chokidar.watch(paths);
- watcher.on('all', function (event, fullname) {
- fullname = path.resolve(fullname);
-
- if (event === 'change' && fullname in _this.pathsToNames) {
- _this.emit('update', _this.pathsToNames[fullname], fullname);
- }
- });
- watcher.on('error', function (error) {
- console.log('Watcher error: ' + error);
- });
- }
-
- return _this;
- }
-
- var _proto = FileSystemLoader.prototype;
-
- _proto.getSource = function getSource(name) {
- var fullpath = null;
- var paths = this.searchPaths;
-
- for (var i = 0; i < paths.length; i++) {
- var basePath = path.resolve(paths[i]);
- var p = path.resolve(paths[i], name); // Only allow the current directory and anything
- // underneath it to be searched
-
- if (p.indexOf(basePath) === 0 && fs.existsSync(p)) {
- fullpath = p;
- break;
- }
- }
-
- if (!fullpath) {
- return null;
- }
-
- this.pathsToNames[fullpath] = name;
- var source = {
- src: fs.readFileSync(fullpath, 'utf-8'),
- path: fullpath,
- noCache: this.noCache
- };
- this.emit('load', name, source);
- return source;
- };
-
- return FileSystemLoader;
- }(Loader);
-
- var NodeResolveLoader = /*#__PURE__*/function (_Loader2) {
- _inheritsLoose(NodeResolveLoader, _Loader2);
-
- function NodeResolveLoader(opts) {
- var _this2;
-
- _this2 = _Loader2.call(this) || this;
- opts = opts || {};
- _this2.pathsToNames = {};
- _this2.noCache = !!opts.noCache;
-
- if (opts.watch) {
- try {
- chokidar = require('chokidar'); // eslint-disable-line global-require
- } catch (e) {
- throw new Error('watch requires chokidar to be installed');
- }
-
- _this2.watcher = chokidar.watch();
-
- _this2.watcher.on('change', function (fullname) {
- _this2.emit('update', _this2.pathsToNames[fullname], fullname);
- });
-
- _this2.watcher.on('error', function (error) {
- console.log('Watcher error: ' + error);
- });
-
- _this2.on('load', function (name, source) {
- _this2.watcher.add(source.path);
- });
- }
-
- return _this2;
- }
-
- var _proto2 = NodeResolveLoader.prototype;
-
- _proto2.getSource = function getSource(name) {
- // Don't allow file-system traversal
- if (/^\.?\.?(\/|\\)/.test(name)) {
- return null;
- }
-
- if (/^[A-Z]:/.test(name)) {
- return null;
- }
-
- var fullpath;
-
- try {
- fullpath = require.resolve(name);
- } catch (e) {
- return null;
- }
-
- this.pathsToNames[fullpath] = name;
- var source = {
- src: fs.readFileSync(fullpath, 'utf-8'),
- path: fullpath,
- noCache: this.noCache
- };
- this.emit('load', name, source);
- return source;
- };
-
- return NodeResolveLoader;
- }(Loader);
-
- module.exports = {
- FileSystemLoader: FileSystemLoader,
- PrecompiledLoader: PrecompiledLoader,
- NodeResolveLoader: NodeResolveLoader
- };
|