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 { export interface FS {
/** check if a file exists asynchronously */ /** check if a file exists asynchronously */
exists: (filepath: string) => Promise<boolean>; exists: (filepath: string) => Promise<boolean>;
@@ -10,7 +8,7 @@ export interface FS {
/** read a file synchronously */ /** read a file synchronously */
readFileSync: (filepath: string) => string; readFileSync: (filepath: string) => string;
/** resolve a file against directory, for given `ext` option */ /** 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 */ /** defaults to "/", will be used for "within roots" check */
sep?: string; sep?: string;
/** dirname for a filepath, used when resolving relative path */ /** 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) 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) { public * candidates (file: string, dirs: string[], currentFile?: string, enforceRoot?: boolean) {
const { fs, extname } = this.options const { fs, extname } = this.options
if (this.shouldLoadRelative(file) && currentFile) { 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) { for (const dir of dirs) {
if (!enforceRoot || this.withinDir(referenced, dir)) { if (!enforceRoot || this.withinDir(referenced, dir)) {
// the relatively referenced file is within one of root dirs // the relatively referenced file is within one of root dirs
@@ -64,10 +68,6 @@ export class Loader {
return file.startsWith(dir) return file.startsWith(dir)
} }
private shouldLoadRelative (referencedFile: string) {
return this.options.relativeReference && this.rRelativePath.test(referencedFile)
}
private dirname (path: string) { private dirname (path: string) {
const fs = this.options.fs const fs = this.options.fs
assert(fs.dirname, '`fs.dirname` is required for relative reference') assert(fs.dirname, '`fs.dirname` is required for relative reference')
+1 -2
View File
@@ -1,7 +1,6 @@
import * as _ from '../util/underscore' import * as _ from '../util/underscore'
import { resolve as nodeResolve, extname, dirname as nodeDirname } from 'path' import { resolve as nodeResolve, extname, dirname as nodeDirname } from 'path'
import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileSync } from 'fs' import { stat, statSync, readFile as nodeReadFile, readFileSync as nodeReadFileSync } from 'fs'
import { LiquidOptions } from '../liquid-options'
const statAsync = _.promisify(stat) const statAsync = _.promisify(stat)
const readFileAsync = _.promisify<string, string, string>(nodeReadFile) const readFileAsync = _.promisify<string, string, string>(nodeReadFile)
@@ -28,7 +27,7 @@ export function existsSync (filepath: string) {
export function readFileSync (filepath: string) { export function readFileSync (filepath: string) {
return nodeReadFileSync(filepath, 'utf8') 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 if (!extname(file)) file += ext
return nodeResolve(root, file) return nodeResolve(root, file)
} }
+1 -1
View File
@@ -55,6 +55,6 @@ describe('#renderFile()', function () {
extname: '.html' extname: '.html'
}) })
return expect(engine.renderFile('/not/exist.html')).to return expect(engine.renderFile('/not/exist.html')).to
.be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo\/,\/root\/"/) .be.rejectedWith(/Failed to lookup "\/not\/exist.html" in "\/boo,\/root\/"/)
}) })
}) })