mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 06:50:47 -07:00
refactor: remove mock-fs from dev dependency
This commit is contained in:
+30
-37
@@ -1,6 +1,6 @@
|
||||
import Scope from './scope/scope'
|
||||
import * as Types from './types'
|
||||
import * as template from 'template'
|
||||
import fs from 'src/fs'
|
||||
import * as _ from './util/underscore'
|
||||
import ITemplate from './template/itemplate'
|
||||
import Tokenizer from './parser/tokenizer'
|
||||
@@ -13,19 +13,17 @@ import Value from './template/value'
|
||||
import { isTruthy, isFalsy, evalExp, evalValue } from './render/syntax'
|
||||
import builtinTags from './builtin/tags'
|
||||
import builtinFilters from './builtin/filters'
|
||||
import { LiquidOptions, defaultOptions } from './liquid-options'
|
||||
import { LiquidOptions, NormalizedOptions, defaultOptions, normalize } from './liquid-options'
|
||||
|
||||
export default class Liquid {
|
||||
public options: LiquidOptions
|
||||
public options: NormalizedOptions
|
||||
private cache: object
|
||||
private parser: Parser
|
||||
private renderer: Render
|
||||
private tokenizer: Tokenizer
|
||||
|
||||
constructor (options: LiquidOptions = {}) {
|
||||
options = _.assign({}, defaultOptions, options)
|
||||
options.root = normalizeStringArray(options.root)
|
||||
|
||||
constructor (opts: LiquidOptions = {}) {
|
||||
const options = { ...defaultOptions, ...normalize(opts) }
|
||||
if (options.cache) {
|
||||
this.cache = {}
|
||||
}
|
||||
@@ -42,37 +40,38 @@ export default class Liquid {
|
||||
return this.parser.parse(tokens)
|
||||
}
|
||||
render (tpl: Array<ITemplate>, ctx?: object, opts?: LiquidOptions) {
|
||||
opts = _.assign({}, this.options, opts)
|
||||
const scope = new Scope(ctx, opts)
|
||||
const options = { ...this.options, ...normalize(opts) }
|
||||
const scope = new Scope(ctx, options)
|
||||
return this.renderer.renderTemplates(tpl, scope)
|
||||
}
|
||||
async parseAndRender (html: string, ctx?: object, opts?: LiquidOptions) {
|
||||
const tpl = await this.parse(html)
|
||||
return this.render(tpl, ctx, opts)
|
||||
}
|
||||
async getTemplate (file, root) {
|
||||
const filepath = await template.resolve(file, root, this.options)
|
||||
return this.respectCache(filepath, async () => {
|
||||
const str = await template.read(filepath)
|
||||
return this.parse(str, filepath)
|
||||
})
|
||||
async getTemplate (file, 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))
|
||||
|
||||
for (const filepath of paths) {
|
||||
if (!(await fs.exists(filepath))) continue
|
||||
|
||||
if (this.options.cache && this.cache[filepath]) return this.cache[filepath]
|
||||
const value = this.parse(await fs.readFile(filepath), filepath)
|
||||
if (this.options.cache) this.cache[filepath] = value
|
||||
return value
|
||||
}
|
||||
|
||||
const err = new Error('ENOENT') as any
|
||||
err.message = `ENOENT: Failed to lookup "${file}" in "${roots}"`
|
||||
err.code = 'ENOENT'
|
||||
throw err
|
||||
}
|
||||
async renderFile (file, ctx?: object, opts?: LiquidOptions) {
|
||||
opts = _.assign({}, opts)
|
||||
const templates = await this.getTemplate(file, opts.root)
|
||||
const options = normalize(opts)
|
||||
const templates = await this.getTemplate(file, options)
|
||||
return this.render(templates, ctx, opts)
|
||||
}
|
||||
async respectCache (key, getter) {
|
||||
const cacheEnabled = this.options.cache
|
||||
if (cacheEnabled && this.cache[key]) {
|
||||
return this.cache[key]
|
||||
}
|
||||
const value = await getter()
|
||||
if (cacheEnabled) {
|
||||
this.cache[key] = value
|
||||
}
|
||||
return value
|
||||
}
|
||||
evalValue (str: string, scope: Scope) {
|
||||
return new Value(str, this.options.strict_filters).value(scope)
|
||||
}
|
||||
@@ -85,10 +84,10 @@ export default class Liquid {
|
||||
plugin (plugin) {
|
||||
return plugin.call(this, Liquid)
|
||||
}
|
||||
express (opts: LiquidOptions = {}) {
|
||||
express () {
|
||||
const self = this
|
||||
return function (filePath, ctx, cb) {
|
||||
opts.root = this.root
|
||||
return function (filePath: string, ctx: object, cb: (err: Error, html?: string) => void) {
|
||||
const opts = { root: this.root }
|
||||
self.renderFile(filePath, ctx, opts).then(html => cb(null, html), cb)
|
||||
}
|
||||
}
|
||||
@@ -99,9 +98,3 @@ export default class Liquid {
|
||||
static evalValue = evalValue
|
||||
static Types = Types
|
||||
}
|
||||
|
||||
function normalizeStringArray (value) {
|
||||
if (_.isArray(value)) return value
|
||||
if (_.isString(value)) return [value]
|
||||
throw new TypeError('illegal root: ' + value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user