1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | var __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 | };
|
23 | Object.defineProperty(exports, "__esModule", { value: true });
|
24 | const fs = require("fs");
|
25 | const pathlib = require("path");
|
26 | const pathIsInside = require("path-is-inside");
|
27 | const vscode_uri_1 = require("vscode-uri");
|
28 |
|
29 |
|
30 |
|
31 | class 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 |
|
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 |
|
62 |
|
63 |
|
64 |
|
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 | }
|
108 | exports.FsUrlLoader = FsUrlLoader;
|
109 |
|
\ | No newline at end of file |