fix: directory info in lookupError message, #395

This commit is contained in:
Harttle
2021-10-04 12:11:54 +08:00
parent 8348036d45
commit 92bfc65e0b
7 changed files with 15 additions and 9 deletions
+7 -2
View File
@@ -1,9 +1,14 @@
const { Liquid } = require('liquidjs') const { Liquid } = require('liquidjs')
const engine = new Liquid({ const engine = new Liquid({
root: __dirname,
extname: '.liquid', extname: '.liquid',
globals: { title: 'LiquidJS Demo' } globals: { title: 'LiquidJS Demo' },
// root files for `.render()` and `.parse()`
root: __dirname,
// layout files for `{% layout %}`
layouts: './layouts',
// partial files for `{% include %}` and `{% render %}`
partials: './partials'
}) })
engine.registerTag('header', { engine.registerTag('header', {
+1 -1
View File
@@ -7,6 +7,6 @@
"start": "node index.js" "start": "node index.js"
}, },
"dependencies": { "dependencies": {
"liquidjs": "latest" "liquidjs": "^9.27.0"
} }
} }
+1
View File
@@ -0,0 +1 @@
<li>{{index}} - {{todo}}</li>
+1 -1
View File
@@ -2,6 +2,6 @@
{%header content: title | capitalize%} {%header content: title | capitalize%}
<ul> <ul>
{% for todo in todos %} {% for todo in todos %}
<li>{{forloop.index}} - {{todo}}</li> {% render 'todo.liquid' with todo, index: forloop.index%}
{% endfor %} {% endfor %}
</ul> </ul>
+5 -5
View File
@@ -20,16 +20,16 @@ export class Loader {
} }
public * lookup (file: string, type: LookupType, sync?: boolean) { public * lookup (file: string, type: LookupType, sync?: boolean) {
const { fs, root } = this.options const { fs } = this.options
for (const filepath of this.candidates(file, type)) { const dirs = this.options[type]
for (const filepath of this.candidates(file, dirs)) {
if (sync ? fs.existsSync(filepath) : yield fs.exists(filepath)) return filepath if (sync ? fs.existsSync(filepath) : yield fs.exists(filepath)) return filepath
} }
throw this.lookupError(file, root) throw this.lookupError(file, dirs)
} }
private * candidates (file: string, type: LookupType) { private * candidates (file: string, dirs: string[]) {
const { fs, extname } = this.options const { fs, extname } = this.options
const dirs = this.options[type]
for (const dir of dirs) { for (const dir of dirs) {
yield fs.resolve(dir, file, extname) yield fs.resolve(dir, file, extname)
} }