mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-16 21:00:40 -07:00
feat: renderSync, parseAndRenderSync and renderFileSync, see #48
This commit is contained in:
+15
-1
@@ -44,8 +44,22 @@ async function readFile (url: string): Promise<string> {
|
||||
})
|
||||
}
|
||||
|
||||
function readFileSync (url: string): string {
|
||||
const xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', url, false)
|
||||
xhr.send()
|
||||
if (xhr.status < 200 || xhr.status >= 300) {
|
||||
throw new Error(xhr.statusText)
|
||||
}
|
||||
return xhr.responseText as string
|
||||
}
|
||||
|
||||
async function exists () {
|
||||
return true
|
||||
}
|
||||
|
||||
export default { readFile, resolve, exists } as IFS
|
||||
function existsSync () {
|
||||
return true
|
||||
}
|
||||
|
||||
export default { readFile, resolve, exists, existsSync, readFileSync } as IFS
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
export default interface IFS {
|
||||
exists: (filepath: string) => Promise<boolean>;
|
||||
readFile: (filepath: string) => Promise<string>;
|
||||
existsSync: (filepath: string) => boolean;
|
||||
readFileSync: (filepath: string) => string;
|
||||
resolve: (root: string, file: string, ext: string) => string;
|
||||
}
|
||||
|
||||
+12
-1
@@ -1,6 +1,6 @@
|
||||
import * as _ from '../util/underscore'
|
||||
import { resolve, extname } from 'path'
|
||||
import { stat, readFile } from 'fs'
|
||||
import { stat, statSync, readFile, readFileSync } from 'fs'
|
||||
import IFS from './ifs'
|
||||
|
||||
const statAsync = _.promisify(stat)
|
||||
@@ -13,6 +13,17 @@ const fs: IFS = {
|
||||
readFile: filepath => {
|
||||
return readFileAsync(filepath, 'utf8')
|
||||
},
|
||||
existsSync: (filepath: string) => {
|
||||
try {
|
||||
statSync(filepath)
|
||||
return true
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
},
|
||||
readFileSync: filepath => {
|
||||
return readFileSync(filepath, 'utf8')
|
||||
},
|
||||
resolve: (root: string, file: string, ext: string) => {
|
||||
if (!extname(file)) file += ext
|
||||
return resolve(root, file)
|
||||
|
||||
Reference in New Issue
Block a user