mirror of
https://gitlab.com/nightlycommit/twing.git
synced 2025-01-18 08:46:50 +02:00
Merge branch 'issue-605' into 'main'
Resolve issue #605 - FilesystemLoader in 6.0.1 Closes #605 See merge request nightlycommit/twing!599
This commit is contained in:
commit
77727ba1ad
@ -27,24 +27,24 @@ export interface TwingFilesystemLoader extends TwingLoader {
|
||||
/**
|
||||
* Adds a path where templates are stored.
|
||||
*
|
||||
* @param {string} path A path where to look for templates
|
||||
* @param {string} namespace A path namespace
|
||||
* @param path A path where to look for templates
|
||||
* @param namespace A path namespace
|
||||
*/
|
||||
addPath(path: string, namespace: string): void;
|
||||
addPath(path: string, namespace?: string | null): void;
|
||||
|
||||
/**
|
||||
* Prepends a path where templates are stored.
|
||||
*
|
||||
* @param {string} path A path where to look for templates
|
||||
* @param {string} namespace A path namespace
|
||||
* @param path A path where to look for templates
|
||||
* @param namespace A path namespace
|
||||
*/
|
||||
prependPath(path: string, namespace: string): void;
|
||||
prependPath(path: string, namespace?: string | null): void;
|
||||
}
|
||||
|
||||
export const createFilesystemLoader = (
|
||||
filesystem: TwingFilesystemLoaderFilesystem
|
||||
): TwingFilesystemLoader => {
|
||||
const namespacedPaths: Map<string, Array<string>> = new Map();
|
||||
const namespacedPaths: Map<string | null, Array<string>> = new Map();
|
||||
|
||||
const stat = (path: string): Promise<TwingFilesystemLoaderFilesystemStats | null> => {
|
||||
return new Promise((resolve) => {
|
||||
@ -73,7 +73,7 @@ export const createFilesystemLoader = (
|
||||
// * if not found yet, resolve from "from"
|
||||
const resolve = (name: string, from: string | null): Promise<string | null> => {
|
||||
name = normalize(from ? resolvePathFromSource(name, from) : name);
|
||||
|
||||
|
||||
const findTemplateInPath = async (path: string): Promise<string | null> => {
|
||||
const stats = await stat(path);
|
||||
|
||||
@ -93,7 +93,7 @@ export const createFilesystemLoader = (
|
||||
// then, search for the template from its namespaced name
|
||||
const [namespace, shortname] = parseName(name);
|
||||
|
||||
const paths = (namespace && namespacedPaths.get(namespace)) || ['.'];
|
||||
const paths = namespacedPaths.get(namespace) || ['.'];
|
||||
|
||||
const findTemplateInPathAtIndex = async (index: number): Promise<string | null> => {
|
||||
if (index < paths.length) {
|
||||
@ -132,7 +132,7 @@ export const createFilesystemLoader = (
|
||||
return [null, name];
|
||||
};
|
||||
|
||||
const addPath: TwingFilesystemLoader["addPath"] = (path, namespace) => {
|
||||
const addPath: TwingFilesystemLoader["addPath"] = (path, namespace = null) => {
|
||||
let namespacePaths = namespacedPaths.get(namespace);
|
||||
|
||||
if (!namespacePaths) {
|
||||
@ -144,7 +144,7 @@ export const createFilesystemLoader = (
|
||||
namespacePaths.push(rtrim(path, '\/\\\\'));
|
||||
}
|
||||
|
||||
const prependPath: TwingFilesystemLoader["prependPath"] = (path, namespace) => {
|
||||
const prependPath: TwingFilesystemLoader["prependPath"] = (path, namespace = null) => {
|
||||
path = rtrim(path, '\/\\\\');
|
||||
|
||||
const namespacePaths = namespacedPaths.get(namespace);
|
||||
|
@ -7,7 +7,15 @@ const fileSystemContent: Array<[path: string, content: string]> = [
|
||||
['index.html', 'index.html content'],
|
||||
['foo/index.html','foo/index.html content'],
|
||||
['bar/index.html', 'bar/index.html content'],
|
||||
['/foo/index.html', '/foo/index.html content']
|
||||
['/foo/index.html', '/foo/index.html content'],
|
||||
['views/index.html', 'views/index.html content'],
|
||||
['views/index2.html', 'views/index2.html content'],
|
||||
['views/foo.html', 'views/foo.html content'],
|
||||
['views/foo2.html', 'views/foo2.html content'],
|
||||
['templates/bar.html', 'templates/bar.html content'],
|
||||
['templates/bar2.html', 'templates/bar2.html content'],
|
||||
['views2/foo2.html', 'views2/foo2.html content'],
|
||||
['templates2/bar2.html', 'templates2/bar2.html content']
|
||||
];
|
||||
|
||||
const createFilesystem = (): TwingFilesystemLoaderFilesystem => {
|
||||
@ -45,7 +53,8 @@ const createFilesystem = (): TwingFilesystemLoaderFilesystem => {
|
||||
tape('createFilesystemLoader', ({test}) => {
|
||||
test('resolve', async ({same, end}) => {
|
||||
const filesystem = createFilesystem();
|
||||
const loader = createFilesystemLoader(filesystem);
|
||||
|
||||
let loader = createFilesystemLoader(filesystem);
|
||||
|
||||
loader.addPath('foo', 'Foo');
|
||||
loader.addPath('foo', '@Foo');
|
||||
@ -67,6 +76,20 @@ tape('createFilesystemLoader', ({test}) => {
|
||||
same(await loader.resolve('./missing.html', null), null);
|
||||
same(await loader.resolve('./foo/missing.html', null), null);
|
||||
same(await loader.resolve('../foo/index.html', 'index.html'), null);
|
||||
|
||||
loader = createFilesystemLoader(filesystem);
|
||||
|
||||
loader.addPath('views');
|
||||
loader.prependPath('views2');
|
||||
loader.addPath('templates', null);
|
||||
loader.prependPath('templates2', null);
|
||||
|
||||
same(await loader.resolve('index2.html', null), 'views/index2.html');
|
||||
same(await loader.resolve('foo.html', null), 'views/foo.html');
|
||||
same(await loader.resolve('bar.html', null), 'templates/bar.html');
|
||||
same(await loader.resolve('foo2.html', null), 'views2/foo2.html');
|
||||
same(await loader.resolve('bar2.html', null), 'templates2/bar2.html');
|
||||
same(await loader.resolve('index.html', null), 'index.html');
|
||||
|
||||
end();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user