Skip to content

Commit 409d189

Browse files
sis0k0Alexander Vakrilov
authored and
Alexander Vakrilov
committed
refactor: resource loader (#968)
* chore(lint): use 4 spaces instead of 2 in ns-file-system * refactor: resource loader * test(xhr-paths): assert resource loader throws when resolving non-existing files
1 parent 9dea49a commit 409d189

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

Diff for: nativescript-angular/file-system/ns-file-system.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ import { knownFolders, Folder, File } from "tns-core-modules/file-system";
66

77
@Injectable()
88
export class NSFileSystem {
9-
public currentApp(): Folder {
10-
return knownFolders.currentApp();
11-
}
9+
public currentApp(): Folder {
10+
return knownFolders.currentApp();
11+
}
1212

13-
public fileFromPath(path: string): File {
14-
return File.fromPath(path);
15-
}
13+
public fileFromPath(path: string): File {
14+
return File.fromPath(path);
15+
}
1616

17-
public fileExists(path: string): boolean {
18-
return File.exists(path);
19-
}
17+
public fileExists(path: string): boolean {
18+
return File.exists(path);
19+
}
2020
}

Diff for: nativescript-angular/resource-loader.ts

+32-19
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,24 @@ export class FileSystemResourceLoader extends ResourceLoader {
2424
return templateFile.readText();
2525
}
2626

27-
resolveRelativeUrls(url: string): string {
27+
resolve(url: string): string {
28+
const normalizedUrl = this.resolveRelativeUrls(url);
29+
30+
if (this.fs.fileExists(normalizedUrl)) {
31+
return normalizedUrl;
32+
}
33+
34+
const { candidates: fallbackCandidates, resource: fallbackResource } =
35+
this.fallbackResolve(normalizedUrl);
36+
37+
if (fallbackResource) {
38+
return fallbackResource;
39+
}
40+
41+
throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`);
42+
}
43+
44+
private resolveRelativeUrls(url: string): string {
2845
// Angular assembles absolute URLs and prefixes them with //
2946
if (url.indexOf("/") !== 0) {
3047
// Resolve relative URLs based on the app root.
@@ -34,26 +51,22 @@ export class FileSystemResourceLoader extends ResourceLoader {
3451
}
3552
}
3653

37-
resolve(url: string) {
38-
const normalizedUrl = this.resolveRelativeUrls(url);
54+
private fallbackResolve(url: string):
55+
({ resource: string, candidates: string[] }) {
3956

40-
if (this.fs.fileExists(normalizedUrl)) {
41-
return normalizedUrl;
42-
}
57+
const candidates = extensionsFallbacks
58+
.filter(([extension]) => url.endsWith(extension))
59+
.map(([extension, fallback]) =>
60+
this.replaceExtension(url, extension, fallback));
4361

44-
const fallbackCandidates = [];
45-
extensionsFallbacks.forEach(([extension, fallback]) => {
46-
if (normalizedUrl.endsWith(extension)) {
47-
fallbackCandidates.push(normalizedUrl.substr(0, normalizedUrl.length - extension.length) + fallback);
48-
}
49-
});
50-
51-
for (let i = 0; i < fallbackCandidates.length; i++) {
52-
if (this.fs.fileExists(fallbackCandidates[i])) {
53-
return fallbackCandidates[i];
54-
}
55-
}
62+
const resource = candidates.find(candidate => this.fs.fileExists(candidate));
5663

57-
throw new Error(`Could not resolve ${url}. Looked for: ${normalizedUrl}, ${fallbackCandidates}`);
64+
return { candidates, resource };
65+
}
66+
67+
private replaceExtension(fileName: string, oldExtension: string, newExtension: string): string {
68+
const baseName = fileName.substr(0, fileName.length - oldExtension.length);
69+
return baseName + newExtension;
5870
}
5971
}
72+

Diff for: tests/app/tests/xhr-paths.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class NSFileSystemMock {
1515
}
1616

1717
public fileExists(path: string): boolean {
18-
// mycomponent.html aways exists
18+
// mycomponent.html always exists
1919
// mycomponent.css is the other file
2020
return path.indexOf("mycomponent.html") >= 0 || path === "/app/dir/mycomponent.css";
2121
}
@@ -65,4 +65,8 @@ describe("XHR name resolution", () => {
6565
"/app/dir/mycomponent.css",
6666
resourceLoader.resolve("mycomponent.less"));
6767
});
68+
69+
it("throws for non-existing file that has no fallbacks", () => {
70+
assert.throws(() => resourceLoader.resolve("does-not-exist.css"));
71+
});
6872
});

0 commit comments

Comments
 (0)