UNPKG

4.48 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5 * This code may only be used under the BSD style license found at
6 * http://polymer.github.io/LICENSE.txt
7 * The complete set of authors may be found at
8 * http://polymer.github.io/AUTHORS.txt
9 * The complete set of contributors may be found at
10 * http://polymer.github.io/CONTRIBUTORS.txt
11 * Code distributed by Google as part of the polymer project is also
12 * subject to an additional IP rights grant found at
13 * http://polymer.github.io/PATENTS.txt
14 */
15var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16 return new (P || (P = Promise))(function (resolve, reject) {
17 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
18 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
19 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
20 step((generator = generator.apply(thisArg, _arguments || [])).next());
21 });
22};
23Object.defineProperty(exports, "__esModule", { value: true });
24const fs = require("fs");
25const pathlib = require("path");
26const pathIsInside = require("path-is-inside");
27const vscode_uri_1 = require("vscode-uri");
28/**
29 * Resolves requests via the file system.
30 */
31class FsUrlLoader {
32 constructor(root = '') {
33 this.root = pathlib.resolve(root);
34 }
35 canLoad(url) {
36 const parsed = vscode_uri_1.default.parse(url);
37 return parsed.scheme === 'file' && !parsed.authority &&
38 pathIsInside(parsed.fsPath, this.root);
39 }
40 load(url) {
41 return new Promise((resolve, reject) => {
42 const result = this.getFilePath(url);
43 if (!result.successful) {
44 throw new Error(`FsUrlLoader can not load url ${JSON.stringify(url)} - ${result.error}`);
45 }
46 fs.readFile(result.value, 'utf8', (error, contents) => {
47 if (error) {
48 // Improve the error message of the most common error.
49 if (error.code === 'ENOENT') {
50 reject(new Error(`No such file: ${error.path}`));
51 }
52 reject(error);
53 }
54 else {
55 resolve(contents);
56 }
57 });
58 });
59 }
60 /**
61 * If successful, result.value will be the filesystem path that we would load
62 * the given url from.
63 *
64 * If unsuccessful, result.value will be an error message as a string.
65 */
66 getFilePath(url) {
67 if (!url.startsWith('file://')) {
68 return { successful: false, error: 'Not a local file:// url.' };
69 }
70 if (!this.canLoad(url)) {
71 return {
72 successful: false,
73 error: `Path is not inside root directory: ${JSON.stringify(this.root)}`
74 };
75 }
76 const path = vscode_uri_1.default.parse(url).fsPath;
77 return { successful: true, value: path };
78 }
79 readDirectory(pathFromRoot, deep) {
80 return __awaiter(this, void 0, void 0, function* () {
81 const files = yield new Promise((resolve, reject) => {
82 fs.readdir(pathlib.join(this.root, pathFromRoot), (err, files) => err ? reject(err) : resolve(files));
83 });
84 const results = [];
85 const subDirResultPromises = [];
86 for (const basename of files) {
87 const file = pathlib.join(pathFromRoot, basename);
88 const stat = yield new Promise((resolve, reject) => fs.stat(pathlib.join(this.root, file), (err, stat) => err ? reject(err) : resolve(stat)));
89 if (stat.isDirectory()) {
90 if (deep) {
91 subDirResultPromises.push(this.readDirectory(file, deep));
92 }
93 }
94 else {
95 results.push(file);
96 }
97 }
98 const arraysOfFiles = yield Promise.all(subDirResultPromises);
99 for (const dirResults of arraysOfFiles) {
100 for (const file of dirResults) {
101 results.push(file);
102 }
103 }
104 return results;
105 });
106 }
107}
108exports.FsUrlLoader = FsUrlLoader;
109//# sourceMappingURL=fs-url-loader.js.map
\No newline at end of file