-
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
142 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { FS } from './fs' | ||
|
||
interface LoaderOptions { | ||
fs: FS; | ||
extname: string; | ||
root: string[]; | ||
partials: string[]; | ||
layouts: string[]; | ||
} | ||
export enum LookupType { | ||
Partials = 'partials', | ||
Layouts = 'layouts', | ||
Root = 'root' | ||
} | ||
export class Loader { | ||
private options: LoaderOptions | ||
|
||
constructor (options: LoaderOptions) { | ||
this.options = options | ||
} | ||
|
||
public * lookup (file: string, type: LookupType, sync?: boolean) { | ||
const { fs, root } = this.options | ||
for (const filepath of this.candidates(file, type)) { | ||
if (sync ? fs.existsSync(filepath) : yield fs.exists(filepath)) return filepath | ||
} | ||
throw this.lookupError(file, root) | ||
} | ||
|
||
private * candidates (file: string, type: LookupType) { | ||
const { fs, extname } = this.options | ||
const dirs = this.options[type] | ||
for (const dir of dirs) { | ||
yield fs.resolve(dir, file, extname) | ||
} | ||
if (fs.fallback !== undefined) { | ||
const filepath = fs.fallback(file) | ||
if (filepath !== undefined) yield filepath | ||
} | ||
} | ||
|
||
private lookupError (file: string, roots: string[]) { | ||
const err = new Error('ENOENT') as any | ||
err.message = `ENOENT: Failed to lookup "${file}" in "${roots}"` | ||
err.code = 'ENOENT' | ||
return err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters