UNPKG

2.35 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.DetectorFilesystem = void 0;
4/**
5 * `DetectorFilesystem` is an abstract class that represents a virtual filesystem
6 * to perform read-only operations on in order to detect which framework is being
7 * used.
8 *
9 * Its abstract methods must be implemented by a subclass that perform the actual
10 * FS operations. Example subclasses could be implemented as:
11 *
12 * - Local filesystem, which proxies the FS operations to the equivalent `fs`
13 * module functions.
14 * - HTTP filesystem, which implements the FS operations over an HTTP server
15 * and does not require a local copy of the files.
16 * - `Files` filesystem, which operates on a virtual `Files` object (i.e. from
17 * the `glob()` function) which could include `FileFsRef`, `FileBlob`, etc.
18 *
19 * This base class implements various helper functions for common tasks (i.e.
20 * read and parse a JSON file). It also includes caching for all FS operations
21 * so that multiple detector functions de-dup read operations on the same file
22 * to reduce network/filesystem overhead.
23 *
24 * **NOTE:** It's important that all instance methods in this base class are
25 * bound to `this` so that the `fs` object may be destructured in the detector
26 * functions. The easiest way to do this is to use the `=` syntax when defining
27 * methods in this class definition.
28 */
29class DetectorFilesystem {
30 constructor() {
31 this.hasPath = async (path) => {
32 let p = this.pathCache.get(path);
33 if (!p) {
34 p = this._hasPath(path);
35 this.pathCache.set(path, p);
36 }
37 return p;
38 };
39 this.isFile = async (name) => {
40 let p = this.fileCache.get(name);
41 if (!p) {
42 p = this._isFile(name);
43 this.fileCache.set(name, p);
44 }
45 return p;
46 };
47 this.readFile = async (name) => {
48 let p = this.readFileCache.get(name);
49 if (!p) {
50 p = this._readFile(name);
51 this.readFileCache.set(name, p);
52 }
53 return p;
54 };
55 this.pathCache = new Map();
56 this.fileCache = new Map();
57 this.readFileCache = new Map();
58 }
59}
60exports.DetectorFilesystem = DetectorFilesystem;