@file-services/utils
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -1,2 +0,2 @@ | ||
import { CallbackFn } from '@file-services/types'; | ||
import type { CallbackFn } from '@file-services/types'; | ||
export declare function callbackify<T1, TResult>(fn: (arg1: T1) => TResult): (arg1: T1, callback: CallbackFn<TResult>) => void; | ||
@@ -3,0 +3,0 @@ export declare function callbackify<T1, T2>(fn: (arg1: T1, arg2: T2) => void): (arg1: T1, arg2: T2, callback: (err: Error | undefined | null) => void) => void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.callbackify = void 0; | ||
function callbackify(fn) { | ||
@@ -4,0 +5,0 @@ return ((...args) => { |
@@ -1,2 +0,2 @@ | ||
import { IBaseFileSystemAsync, IBaseFileSystemSync, IFileSystemAsync, IFileSystemSync, IBaseFileSystem, IFileSystem, IFileSystemExtendedSyncActions, IFileSystemExtendedPromiseActions } from '@file-services/types'; | ||
import type { IBaseFileSystemAsync, IBaseFileSystemSync, IFileSystemAsync, IFileSystemSync, IBaseFileSystem, IFileSystem, IFileSystemExtendedSyncActions, IFileSystemExtendedPromiseActions } from '@file-services/types'; | ||
export declare function createFileSystem(baseFs: IBaseFileSystem): IFileSystem; | ||
@@ -3,0 +3,0 @@ export declare function createSyncFileSystem(baseFs: IBaseFileSystemSync): IFileSystemSync; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createExtendedFileSystemPromiseActions = exports.createAsyncFileSystem = exports.createExtendedSyncActions = exports.createSyncFileSystem = exports.createFileSystem = void 0; | ||
const returnsTrue = () => true; | ||
@@ -10,4 +11,4 @@ function createFileSystem(baseFs) { | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
@@ -19,3 +20,3 @@ } | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs) | ||
...createExtendedSyncActions(baseFs), | ||
}; | ||
@@ -25,3 +26,3 @@ } | ||
function createExtendedSyncActions(baseFs) { | ||
const { statSync, mkdirSync, writeFileSync, unlinkSync, rmdirSync, lstatSync, readdirSync, readFileSync, copyFileSync, dirname, join, resolve } = baseFs; | ||
const { statSync, mkdirSync, writeFileSync, unlinkSync, rmdirSync, lstatSync, readdirSync, readFileSync, copyFileSync, dirname, join, resolve, } = baseFs; | ||
function fileExistsSync(filePath, statFn = statSync) { | ||
@@ -51,3 +52,3 @@ try { | ||
catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
if (e && (e.code === 'EEXIST' || e.code === 'EISDIR')) { | ||
return; | ||
@@ -64,3 +65,3 @@ } | ||
catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
if (!e || (e.code !== 'EEXIST' && e.code !== 'EISDIR')) { | ||
throw e; | ||
@@ -120,2 +121,3 @@ } | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
@@ -171,5 +173,5 @@ } | ||
// resolve path once for recursive functions | ||
ensureDirectorySync: directoryPath => ensureDirectorySync(resolve(directoryPath)), | ||
ensureDirectorySync: (directoryPath) => ensureDirectorySync(resolve(directoryPath)), | ||
populateDirectorySync: (directoryPath, contents) => populateDirectorySync(resolve(directoryPath), contents), | ||
removeSync: entryPath => removeSync(resolve(entryPath)), | ||
removeSync: (entryPath) => removeSync(resolve(entryPath)), | ||
findFilesSync: (rootDirectory, options) => findFilesSync(resolve(rootDirectory), options), | ||
@@ -179,3 +181,3 @@ copyDirectorySync: (sourcePath, destinationPath) => copyDirectorySync(resolve(sourcePath), resolve(destinationPath)), | ||
findFilesInAncestorsSync, | ||
readJsonFileSync | ||
readJsonFileSync, | ||
}; | ||
@@ -189,4 +191,4 @@ } | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
@@ -196,3 +198,3 @@ } | ||
function createExtendedFileSystemPromiseActions(baseFs) { | ||
const { dirname, resolve, join, promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile } } = baseFs; | ||
const { dirname, resolve, join, promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile }, } = baseFs; | ||
async function fileExists(filePath, statFn = stat) { | ||
@@ -222,3 +224,3 @@ try { | ||
catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
if (e && (e.code === 'EEXIST' || e.code === 'EISDIR')) { | ||
return; | ||
@@ -235,3 +237,3 @@ } | ||
catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
if (!e || (e.code !== 'EEXIST' && e.code !== 'EISDIR')) { | ||
throw e; | ||
@@ -263,3 +265,3 @@ } | ||
const directoryItems = await readdir(entryPath); | ||
await Promise.all(directoryItems.map(entryName => remove(join(entryPath, entryName)))); | ||
await Promise.all(directoryItems.map((entryName) => remove(join(entryPath, entryName)))); | ||
await rmdir(entryPath); | ||
@@ -290,2 +292,3 @@ } | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
@@ -340,5 +343,5 @@ } | ||
directoryExists, | ||
ensureDirectory: directoryPath => ensureDirectory(resolve(directoryPath)), | ||
ensureDirectory: (directoryPath) => ensureDirectory(resolve(directoryPath)), | ||
populateDirectory: (directoryPath, contents) => populateDirectory(resolve(directoryPath), contents), | ||
remove: entryPath => remove(resolve(entryPath)), | ||
remove: (entryPath) => remove(resolve(entryPath)), | ||
findFiles: (rootDirectory, options) => findFiles(resolve(rootDirectory), options), | ||
@@ -348,3 +351,3 @@ copyDirectory: (sourcePath, destinationPath) => copyDirectory(resolve(sourcePath), resolve(destinationPath)), | ||
findFilesInAncestors, | ||
readJsonFile | ||
readJsonFile, | ||
}; | ||
@@ -351,0 +354,0 @@ } |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./callbackify")); | ||
__export(require("./create-extended-api")); | ||
__export(require("./set-multi-map")); | ||
__export(require("./sync-to-async-fs")); | ||
__exportStar(require("./callbackify"), exports); | ||
__exportStar(require("./create-extended-api"), exports); | ||
__exportStar(require("./set-multi-map"), exports); | ||
__exportStar(require("./sync-to-async-fs"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SetMultiMap = void 0; | ||
class SetMultiMap { | ||
@@ -4,0 +5,0 @@ constructor() { |
@@ -1,3 +0,3 @@ | ||
import { IBaseFileSystemSync, IBaseFileSystemAsync } from '@file-services/types'; | ||
import type { IBaseFileSystemSync, IBaseFileSystemAsync } from '@file-services/types'; | ||
export declare function syncToAsyncFs(syncFs: IBaseFileSystemSync): IBaseFileSystemAsync; | ||
//# sourceMappingURL=sync-to-async-fs.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.syncToAsyncFs = void 0; | ||
const callbackify_1 = require("./callbackify"); | ||
@@ -48,3 +49,3 @@ function syncToAsyncFs(syncFs) { | ||
return syncFs.readlinkSync(path); | ||
} | ||
}, | ||
}, | ||
@@ -65,3 +66,3 @@ exists(nodePath, callback) { | ||
rename: callbackify_1.callbackify(syncFs.renameSync), | ||
readlink: callbackify_1.callbackify(syncFs.readlinkSync) | ||
readlink: callbackify_1.callbackify(syncFs.readlinkSync), | ||
}; | ||
@@ -68,0 +69,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import { CallbackFn } from '@file-services/types'; | ||
import type { CallbackFn } from '@file-services/types'; | ||
export declare function callbackify<T1, TResult>(fn: (arg1: T1) => TResult): (arg1: T1, callback: CallbackFn<TResult>) => void; | ||
@@ -3,0 +3,0 @@ export declare function callbackify<T1, T2>(fn: (arg1: T1, arg2: T2) => void): (arg1: T1, arg2: T2, callback: (err: Error | undefined | null) => void) => void; |
@@ -1,2 +0,2 @@ | ||
import { IBaseFileSystemAsync, IBaseFileSystemSync, IFileSystemAsync, IFileSystemSync, IBaseFileSystem, IFileSystem, IFileSystemExtendedSyncActions, IFileSystemExtendedPromiseActions } from '@file-services/types'; | ||
import type { IBaseFileSystemAsync, IBaseFileSystemSync, IFileSystemAsync, IFileSystemSync, IBaseFileSystem, IFileSystem, IFileSystemExtendedSyncActions, IFileSystemExtendedPromiseActions } from '@file-services/types'; | ||
export declare function createFileSystem(baseFs: IBaseFileSystem): IFileSystem; | ||
@@ -3,0 +3,0 @@ export declare function createSyncFileSystem(baseFs: IBaseFileSystemSync): IFileSystemSync; |
@@ -8,4 +8,4 @@ const returnsTrue = () => true; | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
@@ -16,7 +16,7 @@ } | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs) | ||
...createExtendedSyncActions(baseFs), | ||
}; | ||
} | ||
export function createExtendedSyncActions(baseFs) { | ||
const { statSync, mkdirSync, writeFileSync, unlinkSync, rmdirSync, lstatSync, readdirSync, readFileSync, copyFileSync, dirname, join, resolve } = baseFs; | ||
const { statSync, mkdirSync, writeFileSync, unlinkSync, rmdirSync, lstatSync, readdirSync, readFileSync, copyFileSync, dirname, join, resolve, } = baseFs; | ||
function fileExistsSync(filePath, statFn = statSync) { | ||
@@ -46,3 +46,3 @@ try { | ||
catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
if (e && (e.code === 'EEXIST' || e.code === 'EISDIR')) { | ||
return; | ||
@@ -59,3 +59,3 @@ } | ||
catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
if (!e || (e.code !== 'EEXIST' && e.code !== 'EISDIR')) { | ||
throw e; | ||
@@ -115,2 +115,3 @@ } | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
@@ -166,5 +167,5 @@ } | ||
// resolve path once for recursive functions | ||
ensureDirectorySync: directoryPath => ensureDirectorySync(resolve(directoryPath)), | ||
ensureDirectorySync: (directoryPath) => ensureDirectorySync(resolve(directoryPath)), | ||
populateDirectorySync: (directoryPath, contents) => populateDirectorySync(resolve(directoryPath), contents), | ||
removeSync: entryPath => removeSync(resolve(entryPath)), | ||
removeSync: (entryPath) => removeSync(resolve(entryPath)), | ||
findFilesSync: (rootDirectory, options) => findFilesSync(resolve(rootDirectory), options), | ||
@@ -174,3 +175,3 @@ copyDirectorySync: (sourcePath, destinationPath) => copyDirectorySync(resolve(sourcePath), resolve(destinationPath)), | ||
findFilesInAncestorsSync, | ||
readJsonFileSync | ||
readJsonFileSync, | ||
}; | ||
@@ -183,8 +184,8 @@ } | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
} | ||
export function createExtendedFileSystemPromiseActions(baseFs) { | ||
const { dirname, resolve, join, promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile } } = baseFs; | ||
const { dirname, resolve, join, promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile }, } = baseFs; | ||
async function fileExists(filePath, statFn = stat) { | ||
@@ -214,3 +215,3 @@ try { | ||
catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
if (e && (e.code === 'EEXIST' || e.code === 'EISDIR')) { | ||
return; | ||
@@ -227,3 +228,3 @@ } | ||
catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
if (!e || (e.code !== 'EEXIST' && e.code !== 'EISDIR')) { | ||
throw e; | ||
@@ -255,3 +256,3 @@ } | ||
const directoryItems = await readdir(entryPath); | ||
await Promise.all(directoryItems.map(entryName => remove(join(entryPath, entryName)))); | ||
await Promise.all(directoryItems.map((entryName) => remove(join(entryPath, entryName)))); | ||
await rmdir(entryPath); | ||
@@ -282,2 +283,3 @@ } | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
@@ -332,5 +334,5 @@ } | ||
directoryExists, | ||
ensureDirectory: directoryPath => ensureDirectory(resolve(directoryPath)), | ||
ensureDirectory: (directoryPath) => ensureDirectory(resolve(directoryPath)), | ||
populateDirectory: (directoryPath, contents) => populateDirectory(resolve(directoryPath), contents), | ||
remove: entryPath => remove(resolve(entryPath)), | ||
remove: (entryPath) => remove(resolve(entryPath)), | ||
findFiles: (rootDirectory, options) => findFiles(resolve(rootDirectory), options), | ||
@@ -340,5 +342,5 @@ copyDirectory: (sourcePath, destinationPath) => copyDirectory(resolve(sourcePath), resolve(destinationPath)), | ||
findFilesInAncestors, | ||
readJsonFile | ||
readJsonFile, | ||
}; | ||
} | ||
//# sourceMappingURL=create-extended-api.js.map |
@@ -1,3 +0,3 @@ | ||
import { IBaseFileSystemSync, IBaseFileSystemAsync } from '@file-services/types'; | ||
import type { IBaseFileSystemSync, IBaseFileSystemAsync } from '@file-services/types'; | ||
export declare function syncToAsyncFs(syncFs: IBaseFileSystemSync): IBaseFileSystemAsync; | ||
//# sourceMappingURL=sync-to-async-fs.d.ts.map |
@@ -46,3 +46,3 @@ import { callbackify } from './callbackify'; | ||
return syncFs.readlinkSync(path); | ||
} | ||
}, | ||
}, | ||
@@ -63,5 +63,5 @@ exists(nodePath, callback) { | ||
rename: callbackify(syncFs.renameSync), | ||
readlink: callbackify(syncFs.readlinkSync) | ||
readlink: callbackify(syncFs.readlinkSync), | ||
}; | ||
} | ||
//# sourceMappingURL=sync-to-async-fs.js.map |
{ | ||
"name": "@file-services/utils", | ||
"description": "Common file system utility functions.", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"main": "cjs/index.js", | ||
@@ -14,3 +14,3 @@ "module": "esm/index.js", | ||
"dependencies": { | ||
"@file-services/types": "^2.2.0" | ||
"@file-services/types": "^3.0.0" | ||
}, | ||
@@ -17,0 +17,0 @@ "files": [ |
# @file-services/utils | ||
[![npm version](https://img.shields.io/npm/v/@file-services/utils.svg)](https://www.npmjs.com/package/@file-services/utils) | ||
@@ -3,0 +4,0 @@ [![package size](https://badgen.net/bundlephobia/minzip/@file-services/utils)](https://bundlephobia.com/result?p=@file-services/utils) |
@@ -1,2 +0,2 @@ | ||
import { CallbackFn, ErrorCallbackFn } from '@file-services/types'; | ||
import type { CallbackFn, ErrorCallbackFn } from '@file-services/types'; | ||
@@ -6,32 +6,32 @@ // ugly types until https://github.com/Microsoft/TypeScript/issues/5453 is resolved | ||
export function callbackify<T1, T2>( | ||
fn: (arg1: T1, arg2: T2) => void | ||
fn: (arg1: T1, arg2: T2) => void | ||
): (arg1: T1, arg2: T2, callback: (err: Error | undefined | null) => void) => void; | ||
export function callbackify<T1, T2, TResult>( | ||
fn: (arg1: T1, arg2: T2) => TResult | ||
fn: (arg1: T1, arg2: T2) => TResult | ||
): (arg1: T1, arg2: T2, callback: CallbackFn<TResult>) => void; | ||
export function callbackify<T1, T2, T3>( | ||
fn: (arg1: T1, arg2: T2, arg3: T3) => void | ||
fn: (arg1: T1, arg2: T2, arg3: T3) => void | ||
): (arg1: T1, arg2: T2, arg3: T3, callback: (err: Error | undefined | null) => void) => void; | ||
export function callbackify<T1, T2, T3, TResult>( | ||
fn: (arg1: T1, arg2: T2, arg3: T3) => TResult | ||
fn: (arg1: T1, arg2: T2, arg3: T3) => TResult | ||
): (arg1: T1, arg2: T2, arg3: T3, callback: CallbackFn<TResult>) => void; | ||
export function callbackify<T1, T2, T3, T4>( | ||
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => void | ||
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => void | ||
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: (err: Error | undefined | null) => void) => void; | ||
export function callbackify<T1, T2, T3, T4, TResult>( | ||
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult | ||
fn: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => TResult | ||
): (arg1: T1, arg2: T2, arg3: T3, arg4: T4, callback: CallbackFn<TResult>) => void; | ||
export function callbackify<F extends (...args: unknown[]) => unknown>(fn: F): unknown { | ||
return ((...args: unknown[]): void => { | ||
const callback = args.pop() as CallbackFn<unknown>; | ||
if (typeof callback !== 'function') { | ||
throw new Error('callback is not a function'); | ||
} | ||
try { | ||
const result = fn(...args); | ||
callback(undefined, result); | ||
} catch (e) { | ||
(callback as ErrorCallbackFn)(e); | ||
} | ||
}) as unknown; | ||
return ((...args: unknown[]): void => { | ||
const callback = args.pop() as CallbackFn<unknown>; | ||
if (typeof callback !== 'function') { | ||
throw new Error('callback is not a function'); | ||
} | ||
try { | ||
const result = fn(...args); | ||
callback(undefined, result); | ||
} catch (e) { | ||
(callback as ErrorCallbackFn)(e); | ||
} | ||
}) as unknown; | ||
} |
@@ -1,14 +0,14 @@ | ||
import { | ||
IBaseFileSystemAsync, | ||
IBaseFileSystemSync, | ||
IFileSystemAsync, | ||
IFileSystemSync, | ||
IDirectoryContents, | ||
IWalkOptions, | ||
IFileSystemDescriptor, | ||
BufferEncoding, | ||
IBaseFileSystem, | ||
IFileSystem, | ||
IFileSystemExtendedSyncActions, | ||
IFileSystemExtendedPromiseActions | ||
import type { | ||
IBaseFileSystemAsync, | ||
IBaseFileSystemSync, | ||
IFileSystemAsync, | ||
IFileSystemSync, | ||
IDirectoryContents, | ||
IWalkOptions, | ||
IFileSystemDescriptor, | ||
BufferEncoding, | ||
IBaseFileSystem, | ||
IFileSystem, | ||
IFileSystemExtendedSyncActions, | ||
IFileSystemExtendedPromiseActions, | ||
} from '@file-services/types'; | ||
@@ -19,377 +19,376 @@ | ||
export function createFileSystem(baseFs: IBaseFileSystem): IFileSystem { | ||
return { | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs), | ||
promises: { | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
}; | ||
return { | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs), | ||
promises: { | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
} | ||
export function createSyncFileSystem(baseFs: IBaseFileSystemSync): IFileSystemSync { | ||
return { | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs) | ||
}; | ||
return { | ||
...baseFs, | ||
...createExtendedSyncActions(baseFs), | ||
}; | ||
} | ||
export function createExtendedSyncActions(baseFs: IBaseFileSystemSync): IFileSystemExtendedSyncActions { | ||
const { | ||
statSync, | ||
mkdirSync, | ||
writeFileSync, | ||
unlinkSync, | ||
rmdirSync, | ||
lstatSync, | ||
readdirSync, | ||
readFileSync, | ||
copyFileSync, | ||
dirname, | ||
join, | ||
resolve | ||
} = baseFs; | ||
const { | ||
statSync, | ||
mkdirSync, | ||
writeFileSync, | ||
unlinkSync, | ||
rmdirSync, | ||
lstatSync, | ||
readdirSync, | ||
readFileSync, | ||
copyFileSync, | ||
dirname, | ||
join, | ||
resolve, | ||
} = baseFs; | ||
function fileExistsSync(filePath: string, statFn = statSync): boolean { | ||
try { | ||
return statFn(filePath).isFile(); | ||
} catch { | ||
return false; | ||
} | ||
function fileExistsSync(filePath: string, statFn = statSync): boolean { | ||
try { | ||
return statFn(filePath).isFile(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
function readJsonFileSync( | ||
filePath: string, | ||
options?: BufferEncoding | { encoding: BufferEncoding } | null | ||
): unknown { | ||
return JSON.parse(readFileSync(filePath, options || 'utf8')); | ||
} | ||
function readJsonFileSync(filePath: string, options?: BufferEncoding | { encoding: BufferEncoding } | null): unknown { | ||
return JSON.parse(readFileSync(filePath, options || 'utf8')) as unknown; | ||
} | ||
function directoryExistsSync(directoryPath: string, statFn = statSync): boolean { | ||
try { | ||
return statFn(directoryPath).isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
function directoryExistsSync(directoryPath: string, statFn = statSync): boolean { | ||
try { | ||
return statFn(directoryPath).isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
function ensureDirectorySync(directoryPath: string): void { | ||
try { | ||
mkdirSync(directoryPath); | ||
} catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
return; | ||
} | ||
const parentPath = dirname(directoryPath); | ||
if (parentPath === directoryPath) { | ||
throw e; | ||
} | ||
ensureDirectorySync(parentPath); | ||
try { | ||
mkdirSync(directoryPath); | ||
} catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
throw e; | ||
} | ||
} | ||
function ensureDirectorySync(directoryPath: string): void { | ||
try { | ||
mkdirSync(directoryPath); | ||
} catch (e) { | ||
if (e && ((e as NodeJS.ErrnoException).code === 'EEXIST' || (e as NodeJS.ErrnoException).code === 'EISDIR')) { | ||
return; | ||
} | ||
const parentPath = dirname(directoryPath); | ||
if (parentPath === directoryPath) { | ||
throw e; | ||
} | ||
ensureDirectorySync(parentPath); | ||
try { | ||
mkdirSync(directoryPath); | ||
} catch (e) { | ||
if (!e || ((e as NodeJS.ErrnoException).code !== 'EEXIST' && (e as NodeJS.ErrnoException).code !== 'EISDIR')) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
function populateDirectorySync(directoryPath: string, contents: IDirectoryContents): string[] { | ||
const filePaths: string[] = []; | ||
ensureDirectorySync(directoryPath); | ||
for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
const nodePath = join(directoryPath, nodeName); | ||
if (typeof nodeValue === 'string') { | ||
ensureDirectorySync(dirname(nodePath)); | ||
writeFileSync(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} else { | ||
populateDirectorySync(nodePath, nodeValue); | ||
} | ||
} | ||
return filePaths; | ||
function populateDirectorySync(directoryPath: string, contents: IDirectoryContents): string[] { | ||
const filePaths: string[] = []; | ||
ensureDirectorySync(directoryPath); | ||
for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
const nodePath = join(directoryPath, nodeName); | ||
if (typeof nodeValue === 'string') { | ||
ensureDirectorySync(dirname(nodePath)); | ||
writeFileSync(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} else { | ||
populateDirectorySync(nodePath, nodeValue); | ||
} | ||
} | ||
return filePaths; | ||
} | ||
// TODO: replace with rmdirSync(path, {recursive: true}) once Node 12+ | ||
function removeSync(entryPath: string): void { | ||
const stats = lstatSync(entryPath); | ||
if (stats.isDirectory()) { | ||
const directoryItems = readdirSync(entryPath); | ||
for (const entryName of directoryItems) { | ||
removeSync(join(entryPath, entryName)); | ||
} | ||
rmdirSync(entryPath); | ||
} else if (stats.isFile() || stats.isSymbolicLink()) { | ||
unlinkSync(entryPath); | ||
} else { | ||
throw new Error(`unknown node type, cannot delete ${entryPath}`); | ||
} | ||
// TODO: replace with rmdirSync(path, {recursive: true}) once Node 12+ | ||
function removeSync(entryPath: string): void { | ||
const stats = lstatSync(entryPath); | ||
if (stats.isDirectory()) { | ||
const directoryItems = readdirSync(entryPath); | ||
for (const entryName of directoryItems) { | ||
removeSync(join(entryPath, entryName)); | ||
} | ||
rmdirSync(entryPath); | ||
} else if (stats.isFile() || stats.isSymbolicLink()) { | ||
unlinkSync(entryPath); | ||
} else { | ||
throw new Error(`unknown node type, cannot delete ${entryPath}`); | ||
} | ||
} | ||
function findFilesSync(rootDirectory: string, options: IWalkOptions = {}, filePaths: string[] = []): string[] { | ||
const { filterFile = returnsTrue, filterDirectory = returnsTrue, printErrors } = options; | ||
function findFilesSync(rootDirectory: string, options: IWalkOptions = {}, filePaths: string[] = []): string[] { | ||
const { filterFile = returnsTrue, filterDirectory = returnsTrue, printErrors } = options; | ||
for (const nodeName of readdirSync(rootDirectory)) { | ||
const nodePath = join(rootDirectory, nodeName); | ||
try { | ||
const nodeStats = statSync(nodePath); | ||
const nodeDesc: IFileSystemDescriptor = { name: nodeName, path: nodePath, stats: nodeStats }; | ||
if (nodeStats.isFile() && filterFile(nodeDesc)) { | ||
filePaths.push(nodePath); | ||
} else if (nodeStats.isDirectory() && filterDirectory(nodeDesc)) { | ||
findFilesSync(nodePath, options, filePaths); | ||
} | ||
} catch (e) { | ||
if (printErrors) { | ||
console.error(e); | ||
} | ||
} | ||
for (const nodeName of readdirSync(rootDirectory)) { | ||
const nodePath = join(rootDirectory, nodeName); | ||
try { | ||
const nodeStats = statSync(nodePath); | ||
const nodeDesc: IFileSystemDescriptor = { name: nodeName, path: nodePath, stats: nodeStats }; | ||
if (nodeStats.isFile() && filterFile(nodeDesc)) { | ||
filePaths.push(nodePath); | ||
} else if (nodeStats.isDirectory() && filterDirectory(nodeDesc)) { | ||
findFilesSync(nodePath, options, filePaths); | ||
} | ||
return filePaths; | ||
} catch (e) { | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
} | ||
} | ||
} | ||
function findClosestFileSync(initialDirectoryPath: string, fileName: string): string | undefined { | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return filePaths; | ||
} | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
function findClosestFileSync(initialDirectoryPath: string, fileName: string): string | undefined { | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return undefined; | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
function findFilesInAncestorsSync(initialDirectoryPath: string, fileName: string): string[] { | ||
const filePaths: string[] = []; | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return undefined; | ||
} | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
function findFilesInAncestorsSync(initialDirectoryPath: string, fileName: string): string[] { | ||
const filePaths: string[] = []; | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return filePaths; | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (fileExistsSync(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
function copyDirectorySync(sourcePath: string, destinationPath: string): void { | ||
ensureDirectorySync(destinationPath); | ||
for (const item of readdirSync(sourcePath, { withFileTypes: true })) { | ||
const sourceItemPath = join(sourcePath, item.name); | ||
const destinationItemPath = join(destinationPath, item.name); | ||
if (item.isFile()) { | ||
copyFileSync(sourceItemPath, destinationItemPath); | ||
} else if (item.isDirectory()) { | ||
copyDirectorySync(sourceItemPath, destinationItemPath); | ||
} | ||
} | ||
return filePaths; | ||
} | ||
function copyDirectorySync(sourcePath: string, destinationPath: string): void { | ||
ensureDirectorySync(destinationPath); | ||
for (const item of readdirSync(sourcePath, { withFileTypes: true })) { | ||
const sourceItemPath = join(sourcePath, item.name); | ||
const destinationItemPath = join(destinationPath, item.name); | ||
if (item.isFile()) { | ||
copyFileSync(sourceItemPath, destinationItemPath); | ||
} else if (item.isDirectory()) { | ||
copyDirectorySync(sourceItemPath, destinationItemPath); | ||
} | ||
} | ||
} | ||
return { | ||
fileExistsSync, | ||
directoryExistsSync, | ||
// resolve path once for recursive functions | ||
ensureDirectorySync: directoryPath => ensureDirectorySync(resolve(directoryPath)), | ||
populateDirectorySync: (directoryPath, contents) => populateDirectorySync(resolve(directoryPath), contents), | ||
removeSync: entryPath => removeSync(resolve(entryPath)), | ||
findFilesSync: (rootDirectory, options) => findFilesSync(resolve(rootDirectory), options), | ||
copyDirectorySync: (sourcePath, destinationPath) => | ||
copyDirectorySync(resolve(sourcePath), resolve(destinationPath)), | ||
findClosestFileSync, | ||
findFilesInAncestorsSync, | ||
readJsonFileSync | ||
}; | ||
return { | ||
fileExistsSync, | ||
directoryExistsSync, | ||
// resolve path once for recursive functions | ||
ensureDirectorySync: (directoryPath) => ensureDirectorySync(resolve(directoryPath)), | ||
populateDirectorySync: (directoryPath, contents) => populateDirectorySync(resolve(directoryPath), contents), | ||
removeSync: (entryPath) => removeSync(resolve(entryPath)), | ||
findFilesSync: (rootDirectory, options) => findFilesSync(resolve(rootDirectory), options), | ||
copyDirectorySync: (sourcePath, destinationPath) => | ||
copyDirectorySync(resolve(sourcePath), resolve(destinationPath)), | ||
findClosestFileSync, | ||
findFilesInAncestorsSync, | ||
readJsonFileSync, | ||
}; | ||
} | ||
export function createAsyncFileSystem(baseFs: IBaseFileSystemAsync): IFileSystemAsync { | ||
return { | ||
...baseFs, | ||
promises: { | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs) | ||
} | ||
}; | ||
return { | ||
...baseFs, | ||
promises: { | ||
...baseFs.promises, | ||
...createExtendedFileSystemPromiseActions(baseFs), | ||
}, | ||
}; | ||
} | ||
export function createExtendedFileSystemPromiseActions( | ||
baseFs: IBaseFileSystemAsync | ||
baseFs: IBaseFileSystemAsync | ||
): IFileSystemExtendedPromiseActions { | ||
const { | ||
dirname, | ||
resolve, | ||
join, | ||
promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile } | ||
} = baseFs; | ||
const { | ||
dirname, | ||
resolve, | ||
join, | ||
promises: { stat, mkdir, writeFile, lstat, rmdir, unlink, readdir, readFile, copyFile }, | ||
} = baseFs; | ||
async function fileExists(filePath: string, statFn = stat): Promise<boolean> { | ||
try { | ||
return (await statFn(filePath)).isFile(); | ||
} catch { | ||
return false; | ||
} | ||
async function fileExists(filePath: string, statFn = stat): Promise<boolean> { | ||
try { | ||
return (await statFn(filePath)).isFile(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
async function directoryExists(directoryPath: string, statFn = stat): Promise<boolean> { | ||
try { | ||
return (await statFn(directoryPath)).isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
async function directoryExists(directoryPath: string, statFn = stat): Promise<boolean> { | ||
try { | ||
return (await statFn(directoryPath)).isDirectory(); | ||
} catch { | ||
return false; | ||
} | ||
} | ||
async function readJsonFile( | ||
filePath: string, | ||
options?: BufferEncoding | { encoding: BufferEncoding } | null | ||
): Promise<unknown> { | ||
return JSON.parse(await readFile(filePath, options || 'utf8')); | ||
} | ||
async function readJsonFile( | ||
filePath: string, | ||
options?: BufferEncoding | { encoding: BufferEncoding } | null | ||
): Promise<unknown> { | ||
return JSON.parse(await readFile(filePath, options || 'utf8')) as unknown; | ||
} | ||
async function ensureDirectory(directoryPath: string): Promise<void> { | ||
try { | ||
await mkdir(directoryPath); | ||
} catch (e) { | ||
if (e && e.code === 'EEXIST') { | ||
return; | ||
} | ||
const parentPath = dirname(directoryPath); | ||
if (parentPath === directoryPath) { | ||
throw e; | ||
} | ||
await ensureDirectory(parentPath); | ||
try { | ||
await mkdir(directoryPath); | ||
} catch (e) { | ||
if (!e || e.code !== 'EEXIST') { | ||
throw e; | ||
} | ||
} | ||
async function ensureDirectory(directoryPath: string): Promise<void> { | ||
try { | ||
await mkdir(directoryPath); | ||
} catch (e) { | ||
if (e && ((e as NodeJS.ErrnoException).code === 'EEXIST' || (e as NodeJS.ErrnoException).code === 'EISDIR')) { | ||
return; | ||
} | ||
const parentPath = dirname(directoryPath); | ||
if (parentPath === directoryPath) { | ||
throw e; | ||
} | ||
await ensureDirectory(parentPath); | ||
try { | ||
await mkdir(directoryPath); | ||
} catch (e) { | ||
if (!e || ((e as NodeJS.ErrnoException).code !== 'EEXIST' && (e as NodeJS.ErrnoException).code !== 'EISDIR')) { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
async function populateDirectory(directoryPath: string, contents: IDirectoryContents): Promise<string[]> { | ||
const filePaths: string[] = []; | ||
await ensureDirectory(directoryPath); | ||
for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
const nodePath = join(directoryPath, nodeName); | ||
if (typeof nodeValue === 'string') { | ||
await ensureDirectory(dirname(nodePath)); | ||
await writeFile(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} else { | ||
await populateDirectory(nodePath, nodeValue); | ||
} | ||
} | ||
return filePaths; | ||
async function populateDirectory(directoryPath: string, contents: IDirectoryContents): Promise<string[]> { | ||
const filePaths: string[] = []; | ||
await ensureDirectory(directoryPath); | ||
for (const [nodeName, nodeValue] of Object.entries(contents)) { | ||
const nodePath = join(directoryPath, nodeName); | ||
if (typeof nodeValue === 'string') { | ||
await ensureDirectory(dirname(nodePath)); | ||
await writeFile(nodePath, nodeValue); | ||
filePaths.push(nodePath); | ||
} else { | ||
await populateDirectory(nodePath, nodeValue); | ||
} | ||
} | ||
return filePaths; | ||
} | ||
// TODO: replace with rmdir(path, {recursive: true}) once Node 12+ | ||
async function remove(entryPath: string): Promise<void> { | ||
const stats = await lstat(entryPath); | ||
if (stats.isDirectory()) { | ||
const directoryItems = await readdir(entryPath); | ||
await Promise.all(directoryItems.map(entryName => remove(join(entryPath, entryName)))); | ||
await rmdir(entryPath); | ||
} else if (stats.isFile() || stats.isSymbolicLink()) { | ||
await unlink(entryPath); | ||
} else { | ||
throw new Error(`unknown node type, cannot delete ${entryPath}`); | ||
} | ||
// TODO: replace with rmdir(path, {recursive: true}) once Node 12+ | ||
async function remove(entryPath: string): Promise<void> { | ||
const stats = await lstat(entryPath); | ||
if (stats.isDirectory()) { | ||
const directoryItems = await readdir(entryPath); | ||
await Promise.all(directoryItems.map((entryName) => remove(join(entryPath, entryName)))); | ||
await rmdir(entryPath); | ||
} else if (stats.isFile() || stats.isSymbolicLink()) { | ||
await unlink(entryPath); | ||
} else { | ||
throw new Error(`unknown node type, cannot delete ${entryPath}`); | ||
} | ||
} | ||
async function findFiles( | ||
rootDirectory: string, | ||
options: IWalkOptions = {}, | ||
filePaths: string[] = [] | ||
): Promise<string[]> { | ||
const { filterFile = returnsTrue, filterDirectory = returnsTrue, printErrors } = options; | ||
async function findFiles( | ||
rootDirectory: string, | ||
options: IWalkOptions = {}, | ||
filePaths: string[] = [] | ||
): Promise<string[]> { | ||
const { filterFile = returnsTrue, filterDirectory = returnsTrue, printErrors } = options; | ||
for (const nodeName of await readdir(rootDirectory)) { | ||
const nodePath = join(rootDirectory, nodeName); | ||
try { | ||
const nodeStats = await stat(nodePath); | ||
const nodeDesc: IFileSystemDescriptor = { name: nodeName, path: nodePath, stats: nodeStats }; | ||
if (nodeStats.isFile() && filterFile(nodeDesc)) { | ||
filePaths.push(nodePath); | ||
} else if (nodeStats.isDirectory() && filterDirectory(nodeDesc)) { | ||
await findFiles(nodePath, options, filePaths); | ||
} | ||
} catch (e) { | ||
if (printErrors) { | ||
console.error(e); | ||
} | ||
} | ||
for (const nodeName of await readdir(rootDirectory)) { | ||
const nodePath = join(rootDirectory, nodeName); | ||
try { | ||
const nodeStats = await stat(nodePath); | ||
const nodeDesc: IFileSystemDescriptor = { name: nodeName, path: nodePath, stats: nodeStats }; | ||
if (nodeStats.isFile() && filterFile(nodeDesc)) { | ||
filePaths.push(nodePath); | ||
} else if (nodeStats.isDirectory() && filterDirectory(nodeDesc)) { | ||
await findFiles(nodePath, options, filePaths); | ||
} | ||
return filePaths; | ||
} catch (e) { | ||
if (printErrors) { | ||
// eslint-disable-next-line no-console | ||
console.error(e); | ||
} | ||
} | ||
} | ||
async function findClosestFile(initialDirectoryPath: string, fileName: string): Promise<string | undefined> { | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return filePaths; | ||
} | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
async function findClosestFile(initialDirectoryPath: string, fileName: string): Promise<string | undefined> { | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return undefined; | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
return filePath; | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
async function findFilesInAncestors(initialDirectoryPath: string, fileName: string): Promise<string[]> { | ||
const filePaths: string[] = []; | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return undefined; | ||
} | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
async function findFilesInAncestors(initialDirectoryPath: string, fileName: string): Promise<string[]> { | ||
const filePaths: string[] = []; | ||
let currentPath = resolve(initialDirectoryPath); | ||
let lastPath: string | undefined; | ||
return filePaths; | ||
while (currentPath !== lastPath) { | ||
const filePath = join(currentPath, fileName); | ||
if (await fileExists(filePath)) { | ||
filePaths.push(filePath); | ||
} | ||
lastPath = currentPath; | ||
currentPath = dirname(currentPath); | ||
} | ||
async function copyDirectory(sourcePath: string, destinationPath: string): Promise<void> { | ||
await ensureDirectory(destinationPath); | ||
for (const item of await readdir(sourcePath, { withFileTypes: true })) { | ||
const sourceItemPath = join(sourcePath, item.name); | ||
const destinationItemPath = join(destinationPath, item.name); | ||
if (item.isFile()) { | ||
await copyFile(sourceItemPath, destinationItemPath); | ||
} else if (item.isDirectory()) { | ||
await copyDirectory(sourceItemPath, destinationItemPath); | ||
} | ||
} | ||
return filePaths; | ||
} | ||
async function copyDirectory(sourcePath: string, destinationPath: string): Promise<void> { | ||
await ensureDirectory(destinationPath); | ||
for (const item of await readdir(sourcePath, { withFileTypes: true })) { | ||
const sourceItemPath = join(sourcePath, item.name); | ||
const destinationItemPath = join(destinationPath, item.name); | ||
if (item.isFile()) { | ||
await copyFile(sourceItemPath, destinationItemPath); | ||
} else if (item.isDirectory()) { | ||
await copyDirectory(sourceItemPath, destinationItemPath); | ||
} | ||
} | ||
} | ||
return { | ||
fileExists, | ||
directoryExists, | ||
ensureDirectory: directoryPath => ensureDirectory(resolve(directoryPath)), | ||
populateDirectory: (directoryPath, contents) => populateDirectory(resolve(directoryPath), contents), | ||
remove: entryPath => remove(resolve(entryPath)), | ||
findFiles: (rootDirectory, options) => findFiles(resolve(rootDirectory), options), | ||
copyDirectory: (sourcePath, destinationPath) => copyDirectory(resolve(sourcePath), resolve(destinationPath)), | ||
findClosestFile, | ||
findFilesInAncestors, | ||
readJsonFile | ||
}; | ||
return { | ||
fileExists, | ||
directoryExists, | ||
ensureDirectory: (directoryPath) => ensureDirectory(resolve(directoryPath)), | ||
populateDirectory: (directoryPath, contents) => populateDirectory(resolve(directoryPath), contents), | ||
remove: (entryPath) => remove(resolve(entryPath)), | ||
findFiles: (rootDirectory, options) => findFiles(resolve(rootDirectory), options), | ||
copyDirectory: (sourcePath, destinationPath) => copyDirectory(resolve(sourcePath), resolve(destinationPath)), | ||
findClosestFile, | ||
findFilesInAncestors, | ||
readJsonFile, | ||
}; | ||
} |
export class SetMultiMap<K, V> implements Iterable<[K, V]> { | ||
private map: Map<K, Set<V>> = new Map(); | ||
private map = new Map<K, Set<V>>(); | ||
public get size(): number { | ||
return Array.from(this.map.values()) | ||
.map(({ size }) => size) | ||
.reduce((sum, size) => sum + size, 0); | ||
} | ||
public get size(): number { | ||
return Array.from(this.map.values()) | ||
.map(({ size }) => size) | ||
.reduce((sum, size) => sum + size, 0); | ||
} | ||
public get(key: K): ReadonlySet<V> | undefined { | ||
return this.map.get(key); | ||
} | ||
public get(key: K): ReadonlySet<V> | undefined { | ||
return this.map.get(key); | ||
} | ||
public add(key: K, value: V): this { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
valueSet.add(value); | ||
} else { | ||
this.map.set(key, new Set([value])); | ||
} | ||
return this; | ||
public add(key: K, value: V): this { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
valueSet.add(value); | ||
} else { | ||
this.map.set(key, new Set([value])); | ||
} | ||
return this; | ||
} | ||
public clear(): void { | ||
this.map.clear(); | ||
} | ||
public clear(): void { | ||
this.map.clear(); | ||
} | ||
public delete(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
const wasInSet = valueSet.delete(value); | ||
if (valueSet.size === 0) { | ||
this.map.delete(key); | ||
} | ||
return wasInSet; | ||
} | ||
return false; | ||
public delete(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
if (valueSet) { | ||
const wasInSet = valueSet.delete(value); | ||
if (valueSet.size === 0) { | ||
this.map.delete(key); | ||
} | ||
return wasInSet; | ||
} | ||
return false; | ||
} | ||
public deleteKey(key: K): boolean { | ||
return this.map.delete(key); | ||
} | ||
public deleteKey(key: K): boolean { | ||
return this.map.delete(key); | ||
} | ||
public has(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
return valueSet ? valueSet.has(value) : false; | ||
} | ||
public has(key: K, value: V): boolean { | ||
const valueSet = this.map.get(key); | ||
return valueSet ? valueSet.has(value) : false; | ||
} | ||
public hasKey(key: K): boolean { | ||
const existingSet = this.map.get(key); | ||
return !!existingSet && existingSet.size > 0; | ||
} | ||
public hasKey(key: K): boolean { | ||
const existingSet = this.map.get(key); | ||
return !!existingSet && existingSet.size > 0; | ||
} | ||
public [Symbol.iterator](): IterableIterator<[K, V]> { | ||
return this.entries(); | ||
} | ||
public [Symbol.iterator](): IterableIterator<[K, V]> { | ||
return this.entries(); | ||
} | ||
public *entries(): IterableIterator<[K, V]> { | ||
const { map } = this; | ||
for (const [key, valueSet] of map.entries()) { | ||
for (const value of valueSet) { | ||
yield [key, value]; | ||
} | ||
} | ||
public *entries(): IterableIterator<[K, V]> { | ||
const { map } = this; | ||
for (const [key, valueSet] of map.entries()) { | ||
for (const value of valueSet) { | ||
yield [key, value]; | ||
} | ||
} | ||
} | ||
public *values(): IterableIterator<V> { | ||
const { map } = this; | ||
for (const valueSet of map.values()) { | ||
for (const value of valueSet) { | ||
yield value; | ||
} | ||
} | ||
public *values(): IterableIterator<V> { | ||
const { map } = this; | ||
for (const valueSet of map.values()) { | ||
for (const value of valueSet) { | ||
yield value; | ||
} | ||
} | ||
} | ||
public keys(): IterableIterator<K> { | ||
return this.map.keys(); | ||
} | ||
public keys(): IterableIterator<K> { | ||
return this.map.keys(); | ||
} | ||
} |
@@ -1,79 +0,79 @@ | ||
import { IBaseFileSystemSync, IBaseFileSystemAsync, IBaseFileSystemPromiseActions } from '@file-services/types'; | ||
import type { IBaseFileSystemSync, IBaseFileSystemAsync, IBaseFileSystemPromiseActions } from '@file-services/types'; | ||
import { callbackify } from './callbackify'; | ||
export function syncToAsyncFs(syncFs: IBaseFileSystemSync): IBaseFileSystemAsync { | ||
return { | ||
...syncFs, | ||
watchService: syncFs.watchService, | ||
caseSensitive: syncFs.caseSensitive, | ||
return { | ||
...syncFs, | ||
watchService: syncFs.watchService, | ||
caseSensitive: syncFs.caseSensitive, | ||
promises: { | ||
readFile: async function readFile(...args: [string]) { | ||
return syncFs.readFileSync(...args); | ||
} as IBaseFileSystemPromiseActions['readFile'], | ||
promises: { | ||
readFile: async function readFile(...args: [string]) { | ||
return syncFs.readFileSync(...args); | ||
} as IBaseFileSystemPromiseActions['readFile'], | ||
async writeFile(...args) { | ||
return syncFs.writeFileSync(...args); | ||
}, | ||
async writeFile(...args) { | ||
return syncFs.writeFileSync(...args); | ||
}, | ||
async unlink(filePath) { | ||
return syncFs.unlinkSync(filePath); | ||
}, | ||
async unlink(filePath) { | ||
return syncFs.unlinkSync(filePath); | ||
}, | ||
readdir: async function readdir(...args: [string]) { | ||
return syncFs.readdirSync(...args); | ||
} as IBaseFileSystemPromiseActions['readdir'], | ||
readdir: async function readdir(...args: [string]) { | ||
return syncFs.readdirSync(...args); | ||
} as IBaseFileSystemPromiseActions['readdir'], | ||
async mkdir(directoryPath) { | ||
return syncFs.mkdirSync(directoryPath); | ||
}, | ||
async mkdir(directoryPath) { | ||
return syncFs.mkdirSync(directoryPath); | ||
}, | ||
async rmdir(directoryPath) { | ||
return syncFs.rmdirSync(directoryPath); | ||
}, | ||
async rmdir(directoryPath) { | ||
return syncFs.rmdirSync(directoryPath); | ||
}, | ||
async exists(nodePath) { | ||
return syncFs.existsSync(nodePath); | ||
}, | ||
async exists(nodePath) { | ||
return syncFs.existsSync(nodePath); | ||
}, | ||
async stat(nodePath) { | ||
return syncFs.statSync(nodePath); | ||
}, | ||
async stat(nodePath) { | ||
return syncFs.statSync(nodePath); | ||
}, | ||
async lstat(nodePath) { | ||
return syncFs.lstatSync(nodePath); | ||
}, | ||
async lstat(nodePath) { | ||
return syncFs.lstatSync(nodePath); | ||
}, | ||
async realpath(nodePath) { | ||
return syncFs.realpathSync(nodePath); | ||
}, | ||
async realpath(nodePath) { | ||
return syncFs.realpathSync(nodePath); | ||
}, | ||
async rename(srcPath, destPath) { | ||
return syncFs.renameSync(srcPath, destPath); | ||
}, | ||
async rename(srcPath, destPath) { | ||
return syncFs.renameSync(srcPath, destPath); | ||
}, | ||
async copyFile(...args) { | ||
return syncFs.copyFileSync(...args); | ||
}, | ||
async copyFile(...args) { | ||
return syncFs.copyFileSync(...args); | ||
}, | ||
async readlink(path) { | ||
return syncFs.readlinkSync(path); | ||
} | ||
}, | ||
exists(nodePath, callback) { | ||
callback(syncFs.existsSync(nodePath)); | ||
}, | ||
readFile: callbackify(syncFs.readFileSync) as IBaseFileSystemAsync['readFile'], | ||
writeFile: callbackify(syncFs.writeFileSync) as IBaseFileSystemAsync['writeFile'], | ||
copyFile: callbackify(syncFs.copyFileSync) as IBaseFileSystemAsync['copyFile'], | ||
unlink: callbackify(syncFs.unlinkSync), | ||
readdir: callbackify(syncFs.readdirSync) as IBaseFileSystemAsync['readdir'], | ||
mkdir: callbackify(syncFs.mkdirSync), | ||
rmdir: callbackify(syncFs.rmdirSync), | ||
stat: callbackify(syncFs.statSync), | ||
lstat: callbackify(syncFs.lstatSync), | ||
realpath: callbackify(syncFs.realpathSync), | ||
rename: callbackify(syncFs.renameSync), | ||
readlink: callbackify(syncFs.readlinkSync) | ||
}; | ||
async readlink(path) { | ||
return syncFs.readlinkSync(path); | ||
}, | ||
}, | ||
exists(nodePath, callback) { | ||
callback(syncFs.existsSync(nodePath)); | ||
}, | ||
readFile: callbackify(syncFs.readFileSync) as IBaseFileSystemAsync['readFile'], | ||
writeFile: callbackify(syncFs.writeFileSync) as IBaseFileSystemAsync['writeFile'], | ||
copyFile: callbackify(syncFs.copyFileSync) as IBaseFileSystemAsync['copyFile'], | ||
unlink: callbackify(syncFs.unlinkSync), | ||
readdir: callbackify(syncFs.readdirSync) as IBaseFileSystemAsync['readdir'], | ||
mkdir: callbackify(syncFs.mkdirSync), | ||
rmdir: callbackify(syncFs.rmdirSync), | ||
stat: callbackify(syncFs.statSync), | ||
lstat: callbackify(syncFs.lstatSync), | ||
realpath: callbackify(syncFs.realpathSync), | ||
rename: callbackify(syncFs.renameSync), | ||
readlink: callbackify(syncFs.readlinkSync), | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
1594
15
0
101516
+ Added@file-services/types@3.1.1(transitive)
- Removed@file-services/types@2.2.0(transitive)
Updated@file-services/types@^3.0.0