UNPKG

16.4 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10var __generator = (this && this.__generator) || function (thisArg, body) {
11 var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
12 return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
13 function verb(n) { return function (v) { return step([n, v]); }; }
14 function step(op) {
15 if (f) throw new TypeError("Generator is already executing.");
16 while (_) try {
17 if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
18 if (y = 0, t) op = [op[0] & 2, t.value];
19 switch (op[0]) {
20 case 0: case 1: t = op; break;
21 case 4: _.label++; return { value: op[1], done: false };
22 case 5: _.label++; y = op[1]; op = [0]; continue;
23 case 7: op = _.ops.pop(); _.trys.pop(); continue;
24 default:
25 if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26 if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
27 if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
28 if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
29 if (t[2]) _.ops.pop();
30 _.trys.pop(); continue;
31 }
32 op = body.call(thisArg, _);
33 } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
34 if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
35 }
36};
37var __importDefault = (this && this.__importDefault) || function (mod) {
38 return (mod && mod.__esModule) ? mod : { "default": mod };
39};
40Object.defineProperty(exports, "__esModule", { value: true });
41var fs_1 = __importDefault(require("fs"));
42var glob_1 = __importDefault(require("glob"));
43var path_1 = __importDefault(require("path"));
44var mkdirp_1 = __importDefault(require("mkdirp"));
45exports.Constants = fs_1.default.constants;
46var FileSystem = /** @class */ (function () {
47 function FileSystem(fs) {
48 this.fs = fs;
49 }
50 FileSystem.prototype.append = function (path, data, throws) {
51 var _this = this;
52 return new Promise(function (resolve, reject) {
53 _this.fs.writeFile(path, data, function (error) {
54 if (error && throws) {
55 reject(error);
56 }
57 else {
58 resolve(error ? false : true);
59 }
60 });
61 });
62 };
63 FileSystem.prototype.basename = function (path, ext) {
64 if (ext === false) {
65 return path_1.default.basename(path, path_1.default.extname(path));
66 }
67 return path_1.default.basename(path, ext === true ? undefined : ext);
68 };
69 FileSystem.prototype.chmod = function (path, mode) {
70 return new Promise(function (resolve, reject) {
71 fs_1.default.chmod(path, mode, function (error) {
72 if (error) {
73 reject(error);
74 }
75 else {
76 resolve();
77 }
78 });
79 });
80 };
81 FileSystem.prototype.close = function (fd, throws) {
82 var _this = this;
83 return new Promise(function (resolve, reject) {
84 _this.fs.close(fd, function (error) {
85 if (error && throws) {
86 reject(error);
87 }
88 else {
89 resolve(error ? false : true);
90 }
91 });
92 });
93 };
94 FileSystem.prototype.delete = function (path, throws) {
95 var _this = this;
96 return new Promise(function (resolve, reject) {
97 _this.fs.unlink(path, function (error) {
98 if (error && throws) {
99 reject(error);
100 }
101 else if (error) {
102 resolve(false);
103 }
104 else {
105 resolve(true);
106 }
107 });
108 });
109 };
110 FileSystem.prototype.dirname = function (filepath) {
111 return path_1.default.dirname(filepath);
112 };
113 FileSystem.prototype.exists = function (path, throws, mode) {
114 var _this = this;
115 return new Promise(function (resolve, reject) {
116 _this.fs.access(path, mode, function (error) {
117 if (error && throws) {
118 reject(error);
119 }
120 else {
121 resolve(error ? false : true);
122 }
123 });
124 });
125 };
126 FileSystem.prototype.ext = function (filename) {
127 return path_1.default.extname(filename);
128 };
129 FileSystem.prototype.file = function (path, content, throws) {
130 return __awaiter(this, void 0, void 0, function () {
131 var dirname;
132 var _this = this;
133 return __generator(this, function (_a) {
134 switch (_a.label) {
135 case 0:
136 dirname = this.dirname(path);
137 return [4 /*yield*/, this.exists(dirname, throws)];
138 case 1:
139 if (!((_a.sent()) === false)) return [3 /*break*/, 3];
140 return [4 /*yield*/, this.mkdirp(dirname)];
141 case 2:
142 _a.sent();
143 _a.label = 3;
144 case 3: return [2 /*return*/, new Promise(function (resolve, reject) {
145 _this.fs.writeFile(path, content, function (error) {
146 if (error && throws) {
147 reject(false);
148 }
149 else {
150 resolve(error ? false : true);
151 }
152 });
153 })];
154 }
155 });
156 });
157 };
158 FileSystem.prototype.glob = function (pattern, cwd) {
159 var patternstr = cwd ? this.join(cwd, pattern) : pattern;
160 return new Promise(function (resolve, reject) {
161 glob_1.default(patternstr, function (error, matches) {
162 if (error) {
163 reject(error);
164 }
165 else {
166 resolve(matches);
167 }
168 });
169 });
170 };
171 FileSystem.prototype.globs = function (patterns, cwd) {
172 return __awaiter(this, void 0, void 0, function () {
173 var resolved;
174 var _this = this;
175 return __generator(this, function (_a) {
176 switch (_a.label) {
177 case 0: return [4 /*yield*/, Promise.all(patterns.map(function (pattern) { return _this.glob(pattern, cwd); }))];
178 case 1:
179 resolved = _a.sent();
180 return [2 /*return*/, resolved.reduce(function (results, current) { return results.concat(current); }, [])];
181 }
182 });
183 });
184 };
185 FileSystem.prototype.list = function (path) {
186 var _this = this;
187 return new Promise(function (resolve, reject) {
188 _this.fs.readdir(path, function (error, files) {
189 /** istanbul ignore next */
190 if (error) {
191 reject(error);
192 }
193 else {
194 resolve(files);
195 }
196 });
197 });
198 };
199 FileSystem.prototype.info = function (path) {
200 return this.stat(path);
201 };
202 FileSystem.prototype.join = function () {
203 var paths = [];
204 for (var _i = 0; _i < arguments.length; _i++) {
205 paths[_i] = arguments[_i];
206 }
207 return path_1.default.join.apply(path_1.default, paths);
208 };
209 FileSystem.prototype.json = function (path) {
210 return __awaiter(this, void 0, void 0, function () {
211 var text;
212 return __generator(this, function (_a) {
213 switch (_a.label) {
214 case 0: return [4 /*yield*/, this.text(path)];
215 case 1:
216 text = _a.sent();
217 if (text) {
218 return [2 /*return*/, JSON.parse(text)];
219 }
220 return [2 /*return*/, Promise.reject(text)];
221 }
222 });
223 });
224 };
225 FileSystem.prototype.mkdir = function (path, mode, throws) {
226 var _this = this;
227 return new Promise(function (resolve, reject) {
228 _this.fs.mkdir(path, mode, function (error) {
229 if (error && throws) {
230 reject(error);
231 }
232 else {
233 resolve(error ? false : true);
234 }
235 });
236 });
237 };
238 FileSystem.prototype.mkdirs = function (paths, mode, throws) {
239 var _this = this;
240 return Promise.all(paths.map(function (path) { return _this.mkdir(path, mode, throws); })).then(function (promises) {
241 return promises.reduce(function (result, current) { return (result ? result : current); }, false);
242 });
243 };
244 FileSystem.prototype.mkdirp = function (path, throws) {
245 return new Promise(function (resolve, reject) {
246 mkdirp_1.default(path, function (error) {
247 if (error && throws) {
248 reject(error);
249 }
250 else {
251 resolve(error ? false : true);
252 }
253 });
254 });
255 };
256 FileSystem.prototype.mkdirps = function (paths, throws) {
257 var _this = this;
258 return Promise.all(paths.map(function (path) { return _this.mkdirp(path, throws); })).then(function (promises) {
259 return promises.reduce(function (result, current) { return (result ? result : current); }, false);
260 });
261 };
262 FileSystem.prototype.open = function (path, flags, mode) {
263 var _this = this;
264 return new Promise(function (resolve, reject) {
265 _this.fs.open(path, flags, mode, function (error, fd) {
266 if (error) {
267 reject(error);
268 }
269 else {
270 resolve(fd);
271 }
272 });
273 });
274 };
275 FileSystem.prototype.read = function (fd, buffer, offset, length, position) {
276 var _this = this;
277 return new Promise(function (resolve, reject) {
278 _this.fs.read(fd, buffer, offset, length, position, function (error, data) {
279 if (error) {
280 reject(error);
281 }
282 else {
283 resolve(data);
284 }
285 });
286 });
287 };
288 FileSystem.prototype.readFile = function (path) {
289 var _this = this;
290 return new Promise(function (resolve, reject) {
291 _this.fs.readFile(path, function (error, buffer) {
292 if (error) {
293 reject(error);
294 }
295 else {
296 resolve(buffer);
297 }
298 });
299 });
300 };
301 FileSystem.prototype.relative = function (from, to) {
302 return path_1.default.relative(from, to);
303 };
304 FileSystem.prototype.relativeFrom = function (to) {
305 return path_1.default.relative(process.cwd(), to);
306 };
307 FileSystem.prototype.rename = function (original, filename, throws) {
308 var _this = this;
309 return new Promise(function (resolve, reject) {
310 _this.fs.rename(original, filename, function (error) {
311 if (error && throws) {
312 reject(error);
313 }
314 else {
315 resolve(error ? false : true);
316 }
317 });
318 });
319 };
320 FileSystem.prototype.resolve = function () {
321 var paths = [];
322 for (var _i = 0; _i < arguments.length; _i++) {
323 paths[_i] = arguments[_i];
324 }
325 return path_1.default.resolve.apply(path_1.default, paths);
326 };
327 FileSystem.prototype.save = function (path, object, throws) {
328 return __awaiter(this, void 0, void 0, function () {
329 var dirname;
330 var _this = this;
331 return __generator(this, function (_a) {
332 switch (_a.label) {
333 case 0:
334 dirname = this.dirname(path);
335 return [4 /*yield*/, this.exists(dirname, throws)];
336 case 1:
337 if (!((_a.sent()) === false)) return [3 /*break*/, 3];
338 return [4 /*yield*/, this.mkdirp(dirname)];
339 case 2:
340 _a.sent();
341 _a.label = 3;
342 case 3: return [2 /*return*/, new Promise(function (resolve, reject) {
343 _this.fs.writeFile(path, JSON.stringify(object), function (error) {
344 if (error && throws) {
345 reject(false);
346 }
347 else {
348 resolve(error ? false : true);
349 }
350 });
351 })];
352 }
353 });
354 });
355 };
356 FileSystem.prototype.stat = function (path) {
357 var _this = this;
358 return new Promise(function (resolve, reject) {
359 _this.fs.stat(path, function (error, stats) {
360 if (error) {
361 reject(error);
362 }
363 else {
364 resolve({ path: String(path), stats: stats });
365 }
366 });
367 });
368 };
369 FileSystem.prototype.stats = function () {
370 var _this = this;
371 var paths = [];
372 for (var _i = 0; _i < arguments.length; _i++) {
373 paths[_i] = arguments[_i];
374 }
375 return Promise.all(paths.map(function (path) { return _this.stat(path); }));
376 };
377 FileSystem.prototype.text = function (path) {
378 var _this = this;
379 return new Promise(function (resolve, reject) {
380 _this.fs.readFile(path, function (error, data) {
381 if (error) {
382 reject(error);
383 }
384 else if (data) {
385 resolve(data.toString());
386 }
387 else {
388 resolve(undefined);
389 }
390 });
391 });
392 };
393 FileSystem.prototype.write = function (fd, buffer, offset, length, position) {
394 var _this = this;
395 return new Promise(function (resolve, reject) {
396 _this.fs.write(fd, buffer, offset, length, position, function (error, written) {
397 if (error) {
398 reject(error);
399 }
400 else {
401 resolve(written);
402 }
403 });
404 });
405 };
406 FileSystem.prototype.writeFile = function (path, data) {
407 var _this = this;
408 return new Promise(function (resolve, reject) {
409 _this.fs.writeFile(path, data, function (error) {
410 if (error) {
411 reject(error);
412 }
413 else {
414 resolve();
415 }
416 });
417 });
418 };
419 return FileSystem;
420}());
421exports.FileSystem = FileSystem;
422exports.fs = new FileSystem(fs_1.default);
423//# sourceMappingURL=FileSystem.js.map
\No newline at end of file