refactor: make loader.shouldLoadRelative public

This commit is contained in:
Harttle
2021-10-16 12:30:09 +08:00
committed by harttle
parent 9cfa43b8ae
commit d4df3a0b53
4 changed files with 8 additions and 11 deletions
+1 -3
View File
@@ -1,5 +1,3 @@
import { LoaderOptions } from './loader'
export interface FS {
/** check if a file exists asynchronously */
exists: (filepath: string) => Promise<boolean>;
@@ -10,7 +8,7 @@ export interface FS {
/** read a file synchronously */
readFileSync: (filepath: string) => string;
/** resolve a file against directory, for given `ext` option */
resolve: (dir: string, file: string, ext: string, options?: LoaderOptions) => string;
resolve: (dir: string, file: string, ext: string) => string;
/** defaults to "/", will be used for "within roots" check */
sep?: string;
/** dirname for a filepath, used when resolving relative path */
+5 -5
View File
@@ -35,10 +35,14 @@ export class Loader {
throw this.lookupError(file, dirs)
}
public shouldLoadRelative (referencedFile: string) {
return this.options.relativeReference && this.rRelativePath.test(referencedFile)
}
public * candidates (file: string, dirs: string[], currentFile?: string, enforceRoot?: boolean) {
const { fs, extname } = this.options
if (this.shouldLoadRelative(file) && currentFile) {
const referenced = fs.resolve(this.dirname(currentFile), file, extname, this.options)
const referenced = fs.resolve(this.dirname(currentFile), file, extname)
for (const dir of dirs) {
if (!enforceRoot || this.withinDir(referenced, dir)) {
// the relatively referenced file is within one of root dirs
@@ -64,10 +68,6 @@ export class Loader {
return file.startsWith(dir)
}
private shouldLoadRelative (referencedFile: string) {
return this.options.relativeReference && this.rRelativePath.test(referencedFile)
}
private dirname (path: string) {
const fs = this.options.fs
assert(fs.dirname, '`fs.dirname` is required for relative reference')
+1 -2
View File
@@ -1,7 +1,6 @@
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)
@@ -28,7 +27,7 @@ export function existsSync (filepath: string) {
export function readFileSync (filepath: string) {
return nodeReadFileSync(filepath, 'utf8')
}
export function resolve (root: string, file: string, ext: string, opts: LiquidOptions) {
export function resolve (root: string, file: string, ext: string) {
if (!extname(file)) file += ext
return nodeResolve(root, file)
}