diff --git a/src/index.ts b/src/index.ts index d654f3e..3a46790 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,52 +1,11 @@ -import { optArg, optArgSync } from './opt-arg.js' +import { glob, globSync } from 'glob' +import { + optArg, + optArgSync, + RimrafAsyncOptions, + RimrafSyncOptions, +} from './opt-arg.js' import pathArg from './path-arg.js' - -import { glob, GlobOptions, globSync } from 'glob' - -export interface RimrafAsyncOptions { - preserveRoot?: boolean - tmp?: string - maxRetries?: number - retryDelay?: number - backoff?: number - maxBackoff?: number - signal?: AbortSignal - glob?: boolean | GlobOptions - filter?: - | ((path: string, ent: Dirent | Stats) => boolean) - | ((path: string, ent: Dirent | Stats) => Promise) -} - -export interface RimrafSyncOptions extends RimrafAsyncOptions { - filter?: (path: string, ent: Dirent | Stats) => boolean -} - -export type RimrafOptions = RimrafSyncOptions | RimrafAsyncOptions - -const typeOrUndef = (val: any, t: string) => - typeof val === 'undefined' || typeof val === t - -export const isRimrafOptions = (o: any): o is RimrafOptions => - !!o && - typeof o === 'object' && - typeOrUndef(o.preserveRoot, 'boolean') && - typeOrUndef(o.tmp, 'string') && - typeOrUndef(o.maxRetries, 'number') && - typeOrUndef(o.retryDelay, 'number') && - typeOrUndef(o.backoff, 'number') && - typeOrUndef(o.maxBackoff, 'number') && - (typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) && - typeOrUndef(o.filter, 'function') - -export const assertRimrafOptions: (o: any) => void = ( - o: any -): asserts o is RimrafOptions => { - if (!isRimrafOptions(o)) { - throw new Error('invalid rimraf options') - } -} - -import { Dirent, Stats } from 'fs' import { rimrafManual, rimrafManualSync } from './rimraf-manual.js' import { rimrafMoveRemove, rimrafMoveRemoveSync } from './rimraf-move-remove.js' import { rimrafNative, rimrafNativeSync } from './rimraf-native.js' @@ -54,6 +13,14 @@ import { rimrafPosix, rimrafPosixSync } from './rimraf-posix.js' import { rimrafWindows, rimrafWindowsSync } from './rimraf-windows.js' import { useNative, useNativeSync } from './use-native.js' +export { + assertRimrafOptions, + isRimrafOptions, + RimrafAsyncOptions, + RimrafOptions, + RimrafSyncOptions, +} from './opt-arg.js' + const wrap = (fn: (p: string, o: RimrafAsyncOptions) => Promise) => async ( diff --git a/src/opt-arg.ts b/src/opt-arg.ts index a9473c5..8541a87 100644 --- a/src/opt-arg.ts +++ b/src/opt-arg.ts @@ -1,10 +1,48 @@ +import { Dirent, Stats } from 'fs' import { GlobOptions } from 'glob' -import { - assertRimrafOptions, - RimrafAsyncOptions, - RimrafOptions, - RimrafSyncOptions, -} from './index.js' + +const typeOrUndef = (val: any, t: string) => + typeof val === 'undefined' || typeof val === t + +export const isRimrafOptions = (o: any): o is RimrafOptions => + !!o && + typeof o === 'object' && + typeOrUndef(o.preserveRoot, 'boolean') && + typeOrUndef(o.tmp, 'string') && + typeOrUndef(o.maxRetries, 'number') && + typeOrUndef(o.retryDelay, 'number') && + typeOrUndef(o.backoff, 'number') && + typeOrUndef(o.maxBackoff, 'number') && + (typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) && + typeOrUndef(o.filter, 'function') + +export const assertRimrafOptions: (o: any) => void = ( + o: any +): asserts o is RimrafOptions => { + if (!isRimrafOptions(o)) { + throw new Error('invalid rimraf options') + } +} + +export interface RimrafAsyncOptions { + preserveRoot?: boolean + tmp?: string + maxRetries?: number + retryDelay?: number + backoff?: number + maxBackoff?: number + signal?: AbortSignal + glob?: boolean | GlobOptions + filter?: + | ((path: string, ent: Dirent | Stats) => boolean) + | ((path: string, ent: Dirent | Stats) => Promise) +} + +export interface RimrafSyncOptions extends RimrafAsyncOptions { + filter?: (path: string, ent: Dirent | Stats) => boolean +} + +export type RimrafOptions = RimrafSyncOptions | RimrafAsyncOptions const optArgT = ( opt: T diff --git a/test/index.ts b/test/index.ts index 6cd1d83..aedca78 100644 --- a/test/index.ts +++ b/test/index.ts @@ -9,6 +9,8 @@ import { rimrafSync } from '../src/index.js' +import * as OPTARG from '../dist/esm/opt-arg.js' + t.test('mocky unit tests to select the correct function', async t => { // don't mock rimrafManual, so we can test the platform switch const CALLS: any[] = [] @@ -29,6 +31,7 @@ t.test('mocky unit tests to select the correct function', async t => { return path }, '../dist/esm/opt-arg.js': { + ...OPTARG, optArg: (opt: RimrafOptions) => { CALLS.push(['optArg', opt]) return opt