Skip to content

Commit

Permalink
fix: skip root check for renderFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Oct 6, 2021
1 parent a3455eb commit 822ba0b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/fs/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Loader {
public * lookup (file: string, type: LookupType, sync?: boolean, currentFile?: string) {
const { fs } = this.options
const dirs = this.options[type]
for (const filepath of this.candidates(file, dirs, currentFile)) {
for (const filepath of this.candidates(file, dirs, currentFile, type !== LookupType.Root)) {
if (sync ? fs.existsSync(filepath) : yield fs.exists(filepath)) return filepath
}
throw this.lookupError(file, dirs)
Expand All @@ -37,12 +37,12 @@ export class Loader {
return path.startsWith('./') || path.startsWith('../')
}

public * candidates (file: string, dirs: string[], currentFile?: string) {
public * candidates (file: string, dirs: string[], currentFile?: string, enforceRoot?: boolean) {
const { fs, extname } = this.options
if (this.shouldLoadRelative(file) && currentFile) {
const referenced = fs.resolve(this.dirname(currentFile), file, extname)
for (const dir of dirs) {
if (referenced.startsWith(dir)) {
if (!enforceRoot || referenced.startsWith(dir)) {
// the relatively referenced file is within one of root dirs
yield referenced
return
Expand All @@ -51,7 +51,7 @@ export class Loader {
}
for (const dir of dirs) {
const referenced = fs.resolve(dir, file, extname)
if (referenced.startsWith(dir)) {
if (!enforceRoot || referenced.startsWith(dir)) {
yield referenced
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/fs/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileS
const statAsync = _.promisify(stat)
const readFileAsync = _.promisify<string, string, string>(nodeReadFile)

export function exists (filepath: string) {
return statAsync(filepath).then(() => true).catch(() => false)
export async function exists (filepath: string) {
try {
await statAsync(filepath)
return true
} catch (err) {
return false
}
}
export function readFile (filepath: string) {
return readFileAsync(filepath, 'utf8')
Expand Down
15 changes: 8 additions & 7 deletions src/liquid-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ export const defaultOptions: NormalizedFullOptions = {
export function normalize (options?: LiquidOptions): NormalizedOptions {
options = options || {}
if (options.hasOwnProperty('root')) {
options.root = normalizeStringArray(options.root)
options.root = normalizeDirectoryList(options.root)
}
if (options.hasOwnProperty('partials')) {
options.partials = normalizeStringArray(options.partials)
options.partials = normalizeDirectoryList(options.partials)
}
if (options.hasOwnProperty('layouts')) {
options.layouts = normalizeStringArray(options.layouts)
options.layouts = normalizeDirectoryList(options.layouts)
}
if (options.hasOwnProperty('cache')) {
let cache: Cache<Template[]> | undefined
Expand All @@ -165,8 +165,9 @@ export function applyDefault (options: NormalizedOptions): NormalizedFullOptions
return fullOptions
}

export function normalizeStringArray (value: any): string[] {
if (_.isArray(value)) return value as string[]
if (_.isString(value)) return [value as string]
return []
export function normalizeDirectoryList (value: any): string[] {
let list: string[] = []
if (_.isArray(value)) list = value
if (_.isString(value)) list = [value]
return list.map(str => fs.resolve(str, '.', '')).map(str => str[str.length - 1] !== '/' ? str + '/' : str)
}
4 changes: 2 additions & 2 deletions src/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import builtinTags from './builtin/tags'
import * as builtinFilters from './builtin/filters'
import { TagMap } from './template/tag/tag-map'
import { FilterMap } from './template/filter/filter-map'
import { LiquidOptions, normalizeStringArray, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { LiquidOptions, normalizeDirectoryList, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImplOptions } from './template/filter/filter-impl-options'
import { toPromise, toValue } from './util/async'

Expand Down Expand Up @@ -116,7 +116,7 @@ export class Liquid {
return function (this: any, filePath: string, ctx: object, callback: (err: Error | null, rendered: string) => void) {
if (firstCall) {
firstCall = false
self.options.root.unshift(...normalizeStringArray(this.root))
self.options.root.unshift(...normalizeDirectoryList(this.root))
}
self.renderFile(filePath, ctx).then(html => callback(null, html) as any, callback as any)
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/render-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ describe('#renderFile()', function () {
extname: '.html'
})
return expect(engine.renderFile('/not/exist.html')).to
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
})
})
2 changes: 1 addition & 1 deletion test/integration/builtin/tags/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ describe('tags/render', function () {
})

describe('static partial', function () {
it('should support filename with extention', async function () {
it('should support filename with extension', async function () {
mock({
'/parent.html': 'X{% render child.html color:"red" %}Y',
'/child.html': 'child with {{color}}'
Expand Down
10 changes: 5 additions & 5 deletions test/integration/liquid/liquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Liquid', function () {
extname: '.html'
})
return expect(engine.renderFile('/not/exist.html')).to
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
})
})
describe('#parseFile', function () {
Expand All @@ -85,7 +85,7 @@ describe('Liquid', function () {
extname: '.html'
})
return expect(engine.parseFile('/not/exist.html')).to
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
})
it('should fallback to require.resolve in Node.js', async function () {
const engine = new Liquid({
Expand Down Expand Up @@ -120,15 +120,15 @@ describe('Liquid', function () {
extname: '.html'
})
return expect(() => engine.parseFileSync('/not/exist.html'))
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
})
it('should throw with lookup list when file not exist', function () {
const engine = new Liquid({
root: ['/boo', '/root/'],
extname: '.html'
})
return expect(() => engine.parseFileSync('/not/exist.html'))
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
.to.throw(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/)
})
})
describe('#enderToNodeStream', function () {
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('Liquid', function () {
expect(drainStream(stream)).to.be.eventually.equal('foo')
})
it('should throw RenderError when tag throws', async () => {
const stream = engine.renderFileToNodeStream('error.html')
const stream = await engine.renderFileToNodeStream('error.html')
expect(drainStream(stream)).to.be.rejectedWith(/intended render error/)
})
})
Expand Down
3 changes: 2 additions & 1 deletion test/integration/liquid/root.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { normalize } from '../../../src/liquid-options'
import { expect } from 'chai'
import { resolve } from 'path'

describe('LiquidOptions#root', function () {
describe('#normalize ()', function () {
it('should normalize string typed root array', function () {
const options = normalize({ root: 'foo' })
expect(options.root).to.eql(['foo'])
expect(options.root).to.eql([resolve('foo') + '/'])
})
it('should normalize null typed root as empty array', function () {
const options = normalize({ root: null } as any)
Expand Down
2 changes: 1 addition & 1 deletion test/unit/fs/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('fs/loader', function () {
describe('.candidates()', function () {
it('should break once found', async function () {
const loader = new Loader({ relativeReference: true, fs, extname: '' } as any)
const candidates = [...loader.candidates('./foo/bar', ['/root', '/root/foo'], '/root/current')]
const candidates = [...loader.candidates('./foo/bar', ['/root', '/root/foo'], '/root/current', true)]
expect(candidates.join()).to.equal('/root/foo/bar')
})
})
Expand Down

0 comments on commit 822ba0b

Please sign in to comment.