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").ReaddirStringCallback} ReaddirStringCallback */
|
---|
10 | /** @typedef {import("./Resolver").StringCallback} StringCallback */
|
---|
11 | /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @param {SyncFileSystem} fs file system implementation
|
---|
15 | * @constructor
|
---|
16 | */
|
---|
17 | function SyncAsyncFileSystemDecorator(fs) {
|
---|
18 | this.fs = fs;
|
---|
19 |
|
---|
20 | this.lstat = undefined;
|
---|
21 | this.lstatSync = undefined;
|
---|
22 | const lstatSync = fs.lstatSync;
|
---|
23 | if (lstatSync) {
|
---|
24 | this.lstat =
|
---|
25 | /** @type {FileSystem["lstat"]} */
|
---|
26 | (
|
---|
27 | (arg, options, callback) => {
|
---|
28 | let result;
|
---|
29 | try {
|
---|
30 | result = /** @type {Function | undefined} */ (callback)
|
---|
31 | ? lstatSync.call(fs, arg, options)
|
---|
32 | : lstatSync.call(fs, arg);
|
---|
33 | } catch (e) {
|
---|
34 | return (callback || options)(
|
---|
35 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
36 | );
|
---|
37 | }
|
---|
38 |
|
---|
39 | (callback || options)(null, /** @type {any} */ (result));
|
---|
40 | }
|
---|
41 | );
|
---|
42 | this.lstatSync =
|
---|
43 | /** @type {SyncFileSystem["lstatSync"]} */
|
---|
44 | ((arg, options) => lstatSync.call(fs, arg, options));
|
---|
45 | }
|
---|
46 |
|
---|
47 | this.stat =
|
---|
48 | /** @type {FileSystem["stat"]} */
|
---|
49 | (
|
---|
50 | (arg, options, callback) => {
|
---|
51 | let result;
|
---|
52 | try {
|
---|
53 | result = /** @type {Function | undefined} */ (callback)
|
---|
54 | ? fs.statSync(arg, options)
|
---|
55 | : fs.statSync(arg);
|
---|
56 | } catch (e) {
|
---|
57 | return (callback || options)(
|
---|
58 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
59 | );
|
---|
60 | }
|
---|
61 |
|
---|
62 | (callback || options)(null, /** @type {any} */ (result));
|
---|
63 | }
|
---|
64 | );
|
---|
65 | this.statSync =
|
---|
66 | /** @type {SyncFileSystem["statSync"]} */
|
---|
67 | ((arg, options) => fs.statSync(arg, options));
|
---|
68 |
|
---|
69 | this.readdir =
|
---|
70 | /** @type {FileSystem["readdir"]} */
|
---|
71 | (
|
---|
72 | (arg, options, callback) => {
|
---|
73 | let result;
|
---|
74 | try {
|
---|
75 | result = /** @type {Function | undefined} */ (callback)
|
---|
76 | ? fs.readdirSync(
|
---|
77 | arg,
|
---|
78 | /** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback>} */
|
---|
79 | (options)
|
---|
80 | )
|
---|
81 | : fs.readdirSync(arg);
|
---|
82 | } catch (e) {
|
---|
83 | return (callback || options)(
|
---|
84 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
85 | );
|
---|
86 | }
|
---|
87 |
|
---|
88 | (callback || options)(null, /** @type {any} */ (result));
|
---|
89 | }
|
---|
90 | );
|
---|
91 | this.readdirSync =
|
---|
92 | /** @type {SyncFileSystem["readdirSync"]} */
|
---|
93 | (
|
---|
94 | (arg, options) =>
|
---|
95 | fs.readdirSync(
|
---|
96 | arg,
|
---|
97 | /** @type {Parameters<SyncFileSystem["readdirSync"]>[1]} */ (options)
|
---|
98 | )
|
---|
99 | );
|
---|
100 |
|
---|
101 | this.readFile =
|
---|
102 | /** @type {FileSystem["readFile"]} */
|
---|
103 | (
|
---|
104 | (arg, options, callback) => {
|
---|
105 | let result;
|
---|
106 | try {
|
---|
107 | result = /** @type {Function | undefined} */ (callback)
|
---|
108 | ? fs.readFileSync(arg, options)
|
---|
109 | : fs.readFileSync(arg);
|
---|
110 | } catch (e) {
|
---|
111 | return (callback || options)(
|
---|
112 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
113 | );
|
---|
114 | }
|
---|
115 |
|
---|
116 | (callback || options)(null, /** @type {any} */ (result));
|
---|
117 | }
|
---|
118 | );
|
---|
119 | this.readFileSync =
|
---|
120 | /** @type {SyncFileSystem["readFileSync"]} */
|
---|
121 | ((arg, options) => fs.readFileSync(arg, options));
|
---|
122 |
|
---|
123 | this.readlink =
|
---|
124 | /** @type {FileSystem["readlink"]} */
|
---|
125 | (
|
---|
126 | (arg, options, callback) => {
|
---|
127 | let result;
|
---|
128 | try {
|
---|
129 | result = /** @type {Function | undefined} */ (callback)
|
---|
130 | ? fs.readlinkSync(
|
---|
131 | arg,
|
---|
132 | /** @type {Exclude<Parameters<FileSystem["readlink"]>[1], StringCallback>} */
|
---|
133 | (options)
|
---|
134 | )
|
---|
135 | : fs.readlinkSync(arg);
|
---|
136 | } catch (e) {
|
---|
137 | return (callback || options)(
|
---|
138 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
139 | );
|
---|
140 | }
|
---|
141 |
|
---|
142 | (callback || options)(null, /** @type {any} */ (result));
|
---|
143 | }
|
---|
144 | );
|
---|
145 | this.readlinkSync =
|
---|
146 | /** @type {SyncFileSystem["readlinkSync"]} */
|
---|
147 | (
|
---|
148 | (arg, options) =>
|
---|
149 | fs.readlinkSync(
|
---|
150 | arg,
|
---|
151 | /** @type {Parameters<SyncFileSystem["readlinkSync"]>[1]} */ (options)
|
---|
152 | )
|
---|
153 | );
|
---|
154 |
|
---|
155 | this.readJson = undefined;
|
---|
156 | this.readJsonSync = undefined;
|
---|
157 | const readJsonSync = fs.readJsonSync;
|
---|
158 | if (readJsonSync) {
|
---|
159 | this.readJson =
|
---|
160 | /** @type {FileSystem["readJson"]} */
|
---|
161 | (
|
---|
162 | (arg, callback) => {
|
---|
163 | let result;
|
---|
164 | try {
|
---|
165 | result = readJsonSync.call(fs, arg);
|
---|
166 | } catch (e) {
|
---|
167 | return callback(
|
---|
168 | /** @type {NodeJS.ErrnoException | Error | null} */ (e)
|
---|
169 | );
|
---|
170 | }
|
---|
171 |
|
---|
172 | callback(null, result);
|
---|
173 | }
|
---|
174 | );
|
---|
175 | this.readJsonSync =
|
---|
176 | /** @type {SyncFileSystem["readJsonSync"]} */
|
---|
177 | (arg => readJsonSync.call(fs, arg));
|
---|
178 | }
|
---|
179 |
|
---|
180 | this.realpath = undefined;
|
---|
181 | this.realpathSync = undefined;
|
---|
182 | const realpathSync = fs.realpathSync;
|
---|
183 | if (realpathSync) {
|
---|
184 | this.realpath =
|
---|
185 | /** @type {FileSystem["realpath"]} */
|
---|
186 | (
|
---|
187 | (arg, options, callback) => {
|
---|
188 | let result;
|
---|
189 | try {
|
---|
190 | result = /** @type {Function | undefined} */ (callback)
|
---|
191 | ? realpathSync.call(
|
---|
192 | fs,
|
---|
193 | arg,
|
---|
194 | /** @type {Exclude<Parameters<NonNullable<FileSystem["realpath"]>>[1], StringCallback>} */
|
---|
195 | (options)
|
---|
196 | )
|
---|
197 | : realpathSync.call(fs, arg);
|
---|
198 | } catch (e) {
|
---|
199 | return (callback || options)(
|
---|
200 | /** @type {NodeJS.ErrnoException | null} */ (e)
|
---|
201 | );
|
---|
202 | }
|
---|
203 |
|
---|
204 | (callback || options)(null, /** @type {any} */ (result));
|
---|
205 | }
|
---|
206 | );
|
---|
207 | this.realpathSync =
|
---|
208 | /** @type {SyncFileSystem["realpathSync"]} */
|
---|
209 | (
|
---|
210 | (arg, options) =>
|
---|
211 | realpathSync.call(
|
---|
212 | fs,
|
---|
213 | arg,
|
---|
214 | /** @type {Parameters<NonNullable<SyncFileSystem["realpathSync"]>>[1]} */
|
---|
215 | (options)
|
---|
216 | )
|
---|
217 | );
|
---|
218 | }
|
---|
219 | }
|
---|
220 | module.exports = SyncAsyncFileSystemDecorator;
|
---|