Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move require path to function #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const assert = require('./src/util/assert.js')
const tokenizer = require('./src/tokenizer.js')
const statFileAsync = require('./src/util/fs.js').statFileAsync
const readFileAsync = require('./src/util/fs.js').readFileAsync
const path = require('path')
const url = require('./src/util/url.js')
const Render = require('./src/render.js')
const lexical = require('./src/lexical.js')
Expand Down Expand Up @@ -63,41 +62,43 @@ var _engine = {
registerTag: function (name, tag) {
return this.tag.register(name, tag)
},
lookup: function (filepath, root) {
root = this.options.root.concat(root || [])
root = _.uniq(root)
var paths = root.map(root => path.resolve(root, filepath))
return anySeries(paths, path => statFileAsync(path).then(() => path))
.catch((e) => {
e.message = `${e.code}: Failed to lookup ${filepath} in: ${root}`
throw e
})
},
// lookup: function (filepath, root) {
// const path = require('path')
// root = this.options.root.concat(root || [])
// root = _.uniq(root)
// var paths = root.map(root => path.resolve(root, filepath))
// return anySeries(paths, path => statFileAsync(path).then(() => path))
// .catch((e) => {
// e.message = `${e.code}: Failed to lookup ${filepath} in: ${root}`
// throw e
// })
// },
getTemplate: function (filepath, root) {
return typeof XMLHttpRequest === 'undefined'
? this.getTemplateFromFile(filepath, root)
: this.getTemplateFromUrl(filepath, root)
},
getTemplateFromFile: function (filepath, root) {
if (!path.extname(filepath)) {
filepath += this.options.extname
}
return this
.lookup(filepath, root)
.then(filepath => {
if (this.options.cache) {
var tpl = this.cache[filepath]
if (tpl) {
return Promise.resolve(tpl)
}
return readFileAsync(filepath)
.then(str => this.parse(str))
.then(tpl => (this.cache[filepath] = tpl))
} else {
return readFileAsync(filepath).then(str => this.parse(str, filepath))
}
})
},
// getTemplateFromFile: function (filepath, root) {
// const path = require('path')
// if (!path.extname(filepath)) {
// filepath += this.options.extname
// }
// return this
// .lookup(filepath, root)
// .then(filepath => {
// if (this.options.cache) {
// var tpl = this.cache[filepath]
// if (tpl) {
// return Promise.resolve(tpl)
// }
// return readFileAsync(filepath)
// .then(str => this.parse(str))
// .then(tpl => (this.cache[filepath] = tpl))
// } else {
// return readFileAsync(filepath).then(str => this.parse(str, filepath))
// }
// })
// },
getTemplateFromUrl: function (filepath, root) {
var fullUrl
if (url.valid(filepath)) {
Expand Down