1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("./Resolver").FileSystem} FileSystem */
|
---|
9 | /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * @param {SyncFileSystem} fs file system implementation
|
---|
13 | * @constructor
|
---|
14 | */
|
---|
15 | function SyncAsyncFileSystemDecorator(fs) {
|
---|
16 | this.fs = fs;
|
---|
17 |
|
---|
18 | this.lstat = undefined;
|
---|
19 | this.lstatSync = undefined;
|
---|
20 | const lstatSync = fs.lstatSync;
|
---|
21 | if (lstatSync) {
|
---|
22 | this.lstat = (arg, options, callback) => {
|
---|
23 | let result;
|
---|
24 | try {
|
---|
25 | result = lstatSync.call(fs, arg);
|
---|
26 | } catch (e) {
|
---|
27 | return (callback || options)(e);
|
---|
28 | }
|
---|
29 | (callback || options)(null, result);
|
---|
30 | };
|
---|
31 | this.lstatSync = (arg, options) => lstatSync.call(fs, arg, options);
|
---|
32 | }
|
---|
33 |
|
---|
34 | this.stat = (arg, options, callback) => {
|
---|
35 | let result;
|
---|
36 | try {
|
---|
37 | result = callback ? fs.statSync(arg, options) : fs.statSync(arg);
|
---|
38 | } catch (e) {
|
---|
39 | return (callback || options)(e);
|
---|
40 | }
|
---|
41 | (callback || options)(null, result);
|
---|
42 | };
|
---|
43 | this.statSync = (arg, options) => fs.statSync(arg, options);
|
---|
44 |
|
---|
45 | this.readdir = (arg, options, callback) => {
|
---|
46 | let result;
|
---|
47 | try {
|
---|
48 | result = fs.readdirSync(arg);
|
---|
49 | } catch (e) {
|
---|
50 | return (callback || options)(e);
|
---|
51 | }
|
---|
52 | (callback || options)(null, result);
|
---|
53 | };
|
---|
54 | this.readdirSync = (arg, options) => fs.readdirSync(arg, options);
|
---|
55 |
|
---|
56 | this.readFile = (arg, options, callback) => {
|
---|
57 | let result;
|
---|
58 | try {
|
---|
59 | result = fs.readFileSync(arg);
|
---|
60 | } catch (e) {
|
---|
61 | return (callback || options)(e);
|
---|
62 | }
|
---|
63 | (callback || options)(null, result);
|
---|
64 | };
|
---|
65 | this.readFileSync = (arg, options) => fs.readFileSync(arg, options);
|
---|
66 |
|
---|
67 | this.readlink = (arg, options, callback) => {
|
---|
68 | let result;
|
---|
69 | try {
|
---|
70 | result = fs.readlinkSync(arg);
|
---|
71 | } catch (e) {
|
---|
72 | return (callback || options)(e);
|
---|
73 | }
|
---|
74 | (callback || options)(null, result);
|
---|
75 | };
|
---|
76 | this.readlinkSync = (arg, options) => fs.readlinkSync(arg, options);
|
---|
77 |
|
---|
78 | this.readJson = undefined;
|
---|
79 | this.readJsonSync = undefined;
|
---|
80 | const readJsonSync = fs.readJsonSync;
|
---|
81 | if (readJsonSync) {
|
---|
82 | this.readJson = (arg, options, callback) => {
|
---|
83 | let result;
|
---|
84 | try {
|
---|
85 | result = readJsonSync.call(fs, arg);
|
---|
86 | } catch (e) {
|
---|
87 | return (callback || options)(e);
|
---|
88 | }
|
---|
89 | (callback || options)(null, result);
|
---|
90 | };
|
---|
91 |
|
---|
92 | this.readJsonSync = (arg, options) => readJsonSync.call(fs, arg, options);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | module.exports = SyncAsyncFileSystemDecorator;
|
---|