mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-18 14:00:39 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
+9
-3
@@ -8,8 +8,7 @@ interface FileDescriptor {
|
||||
}
|
||||
|
||||
let files: { [path: string]: FileDescriptor } = {}
|
||||
const readFile = fs.readFile
|
||||
const exists = fs.exists
|
||||
const { readFile, exists, readFileSync, existsSync } = fs
|
||||
|
||||
export function mock (options: { [path: string]: (string | FileDescriptor) }) {
|
||||
forOwn(options, (val, key) => {
|
||||
@@ -18,19 +17,26 @@ export function mock (options: { [path: string]: (string | FileDescriptor) }) {
|
||||
: val as FileDescriptor
|
||||
})
|
||||
fs.readFile = async function (path) {
|
||||
return fs.readFileSync(path)
|
||||
}
|
||||
fs.readFileSync = function (path) {
|
||||
const file = files[path]
|
||||
if (file === undefined) throw new Error('ENOENT')
|
||||
if (file.mode === '0000') throw new Error('EACCES')
|
||||
return file.content
|
||||
}
|
||||
|
||||
fs.exists = async function (path: string) {
|
||||
return fs.existsSync(path)
|
||||
}
|
||||
fs.existsSync = function (path: string) {
|
||||
return !!files[path]
|
||||
}
|
||||
}
|
||||
|
||||
export function restore () {
|
||||
files = {}
|
||||
fs.readFileSync = readFileSync
|
||||
fs.existsSync = existsSync
|
||||
fs.readFile = readFile
|
||||
fs.exists = exists
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ export const ctx = {
|
||||
foo: 'bar',
|
||||
arr: [-2, 'a'],
|
||||
obj: { foo: 'bar' },
|
||||
func: function () {},
|
||||
func: function () {}, // eslint-disable-line
|
||||
posts: [{ category: 'foo' }, { category: 'bar' }],
|
||||
products: [
|
||||
{ title: 'Vacuum', type: 'living room' },
|
||||
|
||||
Reference in New Issue
Block a user