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