123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- /*
- MIT License http://www.opensource.org/licenses/mit-license.php
- Author Tobias Koppers @sokra
- */
- "use strict";
- /** @typedef {import("./Resolver").FileSystem} FileSystem */
- /** @typedef {import("./Resolver").ReaddirStringCallback} ReaddirStringCallback */
- /** @typedef {import("./Resolver").StringCallback} StringCallback */
- /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
- /**
- * @param {SyncFileSystem} fs file system implementation
- * @constructor
- */
- function SyncAsyncFileSystemDecorator(fs) {
- this.fs = fs;
- this.lstat = undefined;
- this.lstatSync = undefined;
- const lstatSync = fs.lstatSync;
- if (lstatSync) {
- this.lstat =
- /** @type {FileSystem["lstat"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? lstatSync.call(fs, arg, options)
- : lstatSync.call(fs, arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.lstatSync =
- /** @type {SyncFileSystem["lstatSync"]} */
- ((arg, options) => lstatSync.call(fs, arg, options));
- }
- this.stat =
- /** @type {FileSystem["stat"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? fs.statSync(arg, options)
- : fs.statSync(arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.statSync =
- /** @type {SyncFileSystem["statSync"]} */
- ((arg, options) => fs.statSync(arg, options));
- this.readdir =
- /** @type {FileSystem["readdir"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? fs.readdirSync(
- arg,
- /** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback>} */
- (options)
- )
- : fs.readdirSync(arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.readdirSync =
- /** @type {SyncFileSystem["readdirSync"]} */
- (
- (arg, options) =>
- fs.readdirSync(
- arg,
- /** @type {Parameters<SyncFileSystem["readdirSync"]>[1]} */ (options)
- )
- );
- this.readFile =
- /** @type {FileSystem["readFile"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? fs.readFileSync(arg, options)
- : fs.readFileSync(arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.readFileSync =
- /** @type {SyncFileSystem["readFileSync"]} */
- ((arg, options) => fs.readFileSync(arg, options));
- this.readlink =
- /** @type {FileSystem["readlink"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? fs.readlinkSync(
- arg,
- /** @type {Exclude<Parameters<FileSystem["readlink"]>[1], StringCallback>} */
- (options)
- )
- : fs.readlinkSync(arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.readlinkSync =
- /** @type {SyncFileSystem["readlinkSync"]} */
- (
- (arg, options) =>
- fs.readlinkSync(
- arg,
- /** @type {Parameters<SyncFileSystem["readlinkSync"]>[1]} */ (options)
- )
- );
- this.readJson = undefined;
- this.readJsonSync = undefined;
- const readJsonSync = fs.readJsonSync;
- if (readJsonSync) {
- this.readJson =
- /** @type {FileSystem["readJson"]} */
- (
- (arg, callback) => {
- let result;
- try {
- result = readJsonSync.call(fs, arg);
- } catch (e) {
- return callback(
- /** @type {NodeJS.ErrnoException | Error | null} */ (e)
- );
- }
- callback(null, result);
- }
- );
- this.readJsonSync =
- /** @type {SyncFileSystem["readJsonSync"]} */
- (arg => readJsonSync.call(fs, arg));
- }
- this.realpath = undefined;
- this.realpathSync = undefined;
- const realpathSync = fs.realpathSync;
- if (realpathSync) {
- this.realpath =
- /** @type {FileSystem["realpath"]} */
- (
- (arg, options, callback) => {
- let result;
- try {
- result = /** @type {Function | undefined} */ (callback)
- ? realpathSync.call(
- fs,
- arg,
- /** @type {Exclude<Parameters<NonNullable<FileSystem["realpath"]>>[1], StringCallback>} */
- (options)
- )
- : realpathSync.call(fs, arg);
- } catch (e) {
- return (callback || options)(
- /** @type {NodeJS.ErrnoException | null} */ (e)
- );
- }
- (callback || options)(null, /** @type {any} */ (result));
- }
- );
- this.realpathSync =
- /** @type {SyncFileSystem["realpathSync"]} */
- (
- (arg, options) =>
- realpathSync.call(
- fs,
- arg,
- /** @type {Parameters<NonNullable<SyncFileSystem["realpathSync"]>>[1]} */
- (options)
- )
- );
- }
- }
- module.exports = SyncAsyncFileSystemDecorator;
|