UNPKG

5.4 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var path_1 = require("path");
4var virtual_file_utils_1 = require("./virtual-file-utils");
5var HybridFileSystem = (function () {
6 function HybridFileSystem(fileCache) {
7 this.fileCache = fileCache;
8 this.filesStats = {};
9 this.directoryStats = {};
10 }
11 HybridFileSystem.prototype.setInputFileSystem = function (fs) {
12 this.inputFileSystem = fs;
13 };
14 HybridFileSystem.prototype.setOutputFileSystem = function (fs) {
15 this.outputFileSystem = fs;
16 };
17 HybridFileSystem.prototype.setWriteToDisk = function (write) {
18 this.writeToDisk = write;
19 };
20 HybridFileSystem.prototype.isSync = function () {
21 return this.inputFileSystem.isSync();
22 };
23 HybridFileSystem.prototype.stat = function (path, callback) {
24 // first check the fileStats
25 var fileStat = this.filesStats[path];
26 if (fileStat) {
27 return callback(null, fileStat);
28 }
29 // then check the directory stats
30 var directoryStat = this.directoryStats[path];
31 if (directoryStat) {
32 return callback(null, directoryStat);
33 }
34 // fallback to list
35 return this.inputFileSystem.stat(path, callback);
36 };
37 HybridFileSystem.prototype.readdir = function (path, callback) {
38 return this.inputFileSystem.readdir(path, callback);
39 };
40 HybridFileSystem.prototype.readJson = function (path, callback) {
41 return this.inputFileSystem.readJson(path, callback);
42 };
43 HybridFileSystem.prototype.readlink = function (path, callback) {
44 return this.inputFileSystem.readlink(path, function (err, response) {
45 callback(err, response);
46 });
47 };
48 HybridFileSystem.prototype.purge = function (pathsToPurge) {
49 if (this.fileCache) {
50 for (var _i = 0, pathsToPurge_1 = pathsToPurge; _i < pathsToPurge_1.length; _i++) {
51 var path = pathsToPurge_1[_i];
52 this.fileCache.remove(path);
53 }
54 }
55 };
56 HybridFileSystem.prototype.readFile = function (path, callback) {
57 var file = this.fileCache.get(path);
58 if (file) {
59 callback(null, new Buffer(file.content));
60 return;
61 }
62 return this.inputFileSystem.readFile(path, callback);
63 };
64 HybridFileSystem.prototype.addVirtualFile = function (filePath, fileContent) {
65 this.fileCache.set(filePath, { path: filePath, content: fileContent });
66 var fileStats = new virtual_file_utils_1.VirtualFileStats(filePath, fileContent);
67 this.filesStats[filePath] = fileStats;
68 var directoryPath = path_1.dirname(filePath);
69 var directoryStats = new virtual_file_utils_1.VirtualDirStats(directoryPath);
70 this.directoryStats[directoryPath] = directoryStats;
71 };
72 HybridFileSystem.prototype.getFileContent = function (filePath) {
73 var file = this.fileCache.get(filePath);
74 if (file) {
75 return file.content;
76 }
77 return null;
78 };
79 HybridFileSystem.prototype.getDirectoryStats = function (path) {
80 return this.directoryStats[path];
81 };
82 HybridFileSystem.prototype.getSubDirs = function (directoryPath) {
83 return Object.keys(this.directoryStats)
84 .filter(function (filePath) { return path_1.dirname(filePath) === directoryPath; })
85 .map(function (filePath) { return path_1.basename(directoryPath); });
86 };
87 HybridFileSystem.prototype.getFileNamesInDirectory = function (directoryPath) {
88 return Object.keys(this.filesStats).filter(function (filePath) { return path_1.dirname(filePath) === directoryPath; }).map(function (filePath) { return path_1.basename(filePath); });
89 };
90 HybridFileSystem.prototype.getAllFileStats = function () {
91 return this.filesStats;
92 };
93 HybridFileSystem.prototype.getAllDirStats = function () {
94 return this.directoryStats;
95 };
96 HybridFileSystem.prototype.mkdirp = function (filePath, callback) {
97 if (this.writeToDisk) {
98 return this.outputFileSystem.mkdirp(filePath, callback);
99 }
100 callback();
101 };
102 HybridFileSystem.prototype.mkdir = function (filePath, callback) {
103 if (this.writeToDisk) {
104 return this.outputFileSystem.mkdir(filePath, callback);
105 }
106 callback();
107 };
108 HybridFileSystem.prototype.rmdir = function (filePath, callback) {
109 if (this.writeToDisk) {
110 return this.outputFileSystem.rmdir(filePath, callback);
111 }
112 callback();
113 };
114 HybridFileSystem.prototype.unlink = function (filePath, callback) {
115 if (this.writeToDisk) {
116 return this.outputFileSystem.unlink(filePath, callback);
117 }
118 callback();
119 };
120 HybridFileSystem.prototype.join = function (dirPath, fileName) {
121 return path_1.join(dirPath, fileName);
122 };
123 HybridFileSystem.prototype.writeFile = function (filePath, fileContent, callback) {
124 var stringContent = fileContent.toString();
125 this.addVirtualFile(filePath, stringContent);
126 if (this.writeToDisk) {
127 return this.outputFileSystem.writeFile(filePath, fileContent, callback);
128 }
129 callback();
130 };
131 return HybridFileSystem;
132}());
133exports.HybridFileSystem = HybridFileSystem;