Skip to content

Commit

Permalink
move all types related to options to opt-arg.js
Browse files Browse the repository at this point in the history
Fix: #279
  • Loading branch information
isaacs committed Sep 25, 2023
1 parent 5739c76 commit 92a4e2c
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
63 changes: 15 additions & 48 deletions src/index.ts
@@ -1,59 +1,26 @@
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<boolean>)
}

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'
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<boolean>) =>
async (
Expand Down
50 changes: 44 additions & 6 deletions 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<boolean>)
}

export interface RimrafSyncOptions extends RimrafAsyncOptions {
filter?: (path: string, ent: Dirent | Stats) => boolean
}

export type RimrafOptions = RimrafSyncOptions | RimrafAsyncOptions

const optArgT = <T extends RimrafOptions>(
opt: T
Expand Down
3 changes: 3 additions & 0 deletions test/index.ts
Expand Up @@ -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[] = []
Expand All @@ -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
Expand Down

0 comments on commit 92a4e2c

Please sign in to comment.