Files
liquidjs/src/fs/node.ts
T
Harttleandharttle 9cfa43b8ae fix: hardcoded '/' in normalized options.fs, fixes #412, #408
Changes including:
- will not call fs.resolve when normalizing `fs`
- removed hardcoded `/`
- mandatory `fs.dirname` for relative reference
- mandatory `fs.sep`, defaults to '/'
2021-10-16 12:10:55 +08:00

44 lines
1.2 KiB
TypeScript

import * as _ from '../util/underscore'
import { resolve as nodeResolve, extname, dirname as nodeDirname } from 'path'
import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileSync } from 'fs'
import { LiquidOptions } from '../liquid-options'
const statAsync = _.promisify(stat)
const readFileAsync = _.promisify<string, string, string>(nodeReadFile)
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')
}
export function existsSync (filepath: string) {
try {
statSync(filepath)
return true
} catch (err) {
return false
}
}
export function readFileSync (filepath: string) {
return nodeReadFileSync(filepath, 'utf8')
}
export function resolve (root: string, file: string, ext: string, opts: LiquidOptions) {
if (!extname(file)) file += ext
return nodeResolve(root, file)
}
export function fallback (file: string) {
try {
return require.resolve(file)
} catch (e) {}
}
export function dirname (filepath: string) {
return nodeDirname(filepath)
}
export { sep } from 'path'