From 6e2fb5132b384e76d3d18c10426f7442bb387c81 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Wed, 30 Aug 2017 14:07:24 +0300 Subject: [PATCH 1/3] chore(lint): use 4 spaces instead of 2 in ns-file-system --- .../file-system/ns-file-system.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/nativescript-angular/file-system/ns-file-system.ts b/nativescript-angular/file-system/ns-file-system.ts index 1cdc8c9e5..222a54c12 100644 --- a/nativescript-angular/file-system/ns-file-system.ts +++ b/nativescript-angular/file-system/ns-file-system.ts @@ -6,15 +6,15 @@ import { knownFolders, Folder, File } from "tns-core-modules/file-system"; @Injectable() export class NSFileSystem { - public currentApp(): Folder { - return knownFolders.currentApp(); - } + public currentApp(): Folder { + return knownFolders.currentApp(); + } - public fileFromPath(path: string): File { - return File.fromPath(path); - } + public fileFromPath(path: string): File { + return File.fromPath(path); + } - public fileExists(path: string): boolean { - return File.exists(path); - } + public fileExists(path: string): boolean { + return File.exists(path); + } } From 59cb8e3f4186c303a481749f8cc890525fe1a928 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Wed, 30 Aug 2017 14:47:13 +0300 Subject: [PATCH 2/3] refactor: resource loader --- nativescript-angular/resource-loader.ts | 51 ++++++++++++++++--------- tests/app/tests/xhr-paths.ts | 2 +- 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/nativescript-angular/resource-loader.ts b/nativescript-angular/resource-loader.ts index 10e6ffc47..43a5091bc 100644 --- a/nativescript-angular/resource-loader.ts +++ b/nativescript-angular/resource-loader.ts @@ -24,7 +24,24 @@ export class FileSystemResourceLoader extends ResourceLoader { return templateFile.readText(); } - resolveRelativeUrls(url: string): string { + resolve(url: string): string { + const normalizedUrl = this.resolveRelativeUrls(url); + + if (this.fs.fileExists(normalizedUrl)) { + return normalizedUrl; + } + + const { candidates: fallbackCandidates, resource: fallbackResource } = + this.fallbackResolve(normalizedUrl); + + if (fallbackResource) { + return fallbackResource; + } + + throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`); + } + + private resolveRelativeUrls(url: string): string { // Angular assembles absolute URLs and prefixes them with // if (url.indexOf("/") !== 0) { // Resolve relative URLs based on the app root. @@ -34,26 +51,22 @@ export class FileSystemResourceLoader extends ResourceLoader { } } - resolve(url: string) { - const normalizedUrl = this.resolveRelativeUrls(url); + private fallbackResolve(url: string): + ({ resource: string, candidates: string[] }) { - if (this.fs.fileExists(normalizedUrl)) { - return normalizedUrl; - } + const candidates = extensionsFallbacks + .filter(([extension]) => url.endsWith(extension)) + .map(([extension, fallback]) => + this.replaceExtension(url, extension, fallback)); - const fallbackCandidates = []; - extensionsFallbacks.forEach(([extension, fallback]) => { - if (normalizedUrl.endsWith(extension)) { - fallbackCandidates.push(normalizedUrl.substr(0, normalizedUrl.length - extension.length) + fallback); - } - }); - - for (let i = 0; i < fallbackCandidates.length; i++) { - if (this.fs.fileExists(fallbackCandidates[i])) { - return fallbackCandidates[i]; - } - } + const resource = candidates.find(candidate => this.fs.fileExists(candidate)); - throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`); + return { candidates, resource }; + } + + private replaceExtension(fileName: string, oldExtension: string, newExtension: string): string { + const baseName = fileName.substr(0, fileName.length - oldExtension.length); + return baseName + newExtension; } } + diff --git a/tests/app/tests/xhr-paths.ts b/tests/app/tests/xhr-paths.ts index 46545808d..a0b655b85 100644 --- a/tests/app/tests/xhr-paths.ts +++ b/tests/app/tests/xhr-paths.ts @@ -15,7 +15,7 @@ class NSFileSystemMock { } public fileExists(path: string): boolean { - // mycomponent.html aways exists + // mycomponent.html always exists // mycomponent.css is the other file return path.indexOf("mycomponent.html") >= 0 || path === "/app/dir/mycomponent.css"; } From 8eff22bd8505890e274e74493293d092639387b9 Mon Sep 17 00:00:00 2001 From: sis0k0 Date: Wed, 30 Aug 2017 15:16:37 +0300 Subject: [PATCH 3/3] test(xhr-paths): assert resource loader throws when resolving non-existing files --- tests/app/tests/xhr-paths.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/app/tests/xhr-paths.ts b/tests/app/tests/xhr-paths.ts index a0b655b85..c3de6a6dc 100644 --- a/tests/app/tests/xhr-paths.ts +++ b/tests/app/tests/xhr-paths.ts @@ -65,4 +65,8 @@ describe("XHR name resolution", () => { "/app/dir/mycomponent.css", resourceLoader.resolve("mycomponent.less")); }); + + it("throws for non-existing file that has no fallbacks", () => { + assert.throws(() => resourceLoader.resolve("does-not-exist.css")); + }); });