Expose fs as an option to override the default implementations

This commit is contained in:
Patrick Malouin
2019-06-27 09:25:17 +08:00
committed by Jun Yang
parent 2ae8c37a4f
commit edb2cc1f37
4 changed files with 33 additions and 4 deletions
+2
View File
@@ -178,6 +178,8 @@ Otherwise, undefined variables will cause an exception. Defaults to `false`.
* `greedy` is used to specify whether `trim*Left`/`trim*Right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`.
* `fs` is used to override the default file-system module with a custom implementation.
## Register Filters
```javascript
+4 -1
View File
@@ -1,4 +1,5 @@
import * as _ from './util/underscore'
import IFS from './fs/ifs';
export interface LiquidOptions {
/** `root` is a directory or an array of directories to resolve layouts and includes, as well as the filename passed in when calling `.renderFile()`. If an array, the files are looked up in the order they occur in the array. Defaults to `["."]` */
@@ -28,7 +29,9 @@ export interface LiquidOptions {
outputDelimiterLeft?: string,
outputDelimiterRight?: string,
/** `greedy` is used to specify whether `trim*Left`/`trim*Right` is greedy. When set to `true`, all consecutive blank characters including `\n` will be trimed regardless of line breaks. Defaults to `true`. */
greedy?: boolean
greedy?: boolean,
/** `fs` is used to override the default file-system module with a custom implementation */
fs?: IFS
}
interface NormalizedOptions extends LiquidOptions {
+6 -3
View File
@@ -15,6 +15,7 @@ import builtinTags from './builtin/tags'
import builtinFilters from './builtin/filters'
import { LiquidOptions, NormalizedFullOptions, applyDefault, normalize } from './liquid-options'
import { FilterImplOptions } from './template/filter/filter-impl-options'
import IFS from './fs/ifs';
export default class Liquid {
public options: NormalizedFullOptions
@@ -22,12 +23,14 @@ export default class Liquid {
public parser: Parser
private cache: object = {}
private tokenizer: Tokenizer
private fs: IFS
constructor (opts: LiquidOptions = {}) {
this.options = applyDefault(normalize(opts))
this.parser = new Parser(this)
this.renderer = new Render()
this.tokenizer = new Tokenizer(this.options)
this.fs = opts.fs || fs
_.forOwn(builtinTags, (conf, name) => this.registerTag(name, conf))
_.forOwn(builtinFilters, (handler, name) => this.registerFilter(name, handler))
@@ -48,14 +51,14 @@ export default class Liquid {
async getTemplate (file: string, opts?: LiquidOptions) {
const options = normalize(opts)
const roots = options.root ? [...options.root, ...this.options.root] : this.options.root
const paths = roots.map(root => fs.resolve(root, file, this.options.extname))
const paths = roots.map(root => this.fs.resolve(root, file, this.options.extname))
for (const filepath of paths) {
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]
if (!(await fs.exists(filepath))) continue
if (!(await this.fs.exists(filepath))) continue
const value = this.parse(await fs.readFile(filepath), filepath)
const value = this.parse(await this.fs.readFile(filepath), filepath)
if (this.options.cache) this.cache[filepath] = value
return value
}
+21
View File
@@ -0,0 +1,21 @@
import { expect } from 'chai'
import Liquid from '../../../src/liquid'
describe('LiquidOptions#fs', function () {
let engine: Liquid
const fs = {
exists: () => Promise.resolve(true),
readFile: () => Promise.resolve('test file content'),
resolve: () => 'resolved'
}
beforeEach(function () {
engine = new Liquid({
root: '/root/',
fs
})
})
it('should be used to read templates', function () {
return engine.renderFile('files/foo')
.then(x => expect(x).to.equal('test file content'))
})
})