Skip to content

Commit 92a4e2c

Browse files
committed
move all types related to options to opt-arg.js
Fix: #279
1 parent 5739c76 commit 92a4e2c

File tree

3 files changed

+62
-54
lines changed

3 files changed

+62
-54
lines changed

src/index.ts

+15-48
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,26 @@
1-
import { optArg, optArgSync } from './opt-arg.js'
1+
import { glob, globSync } from 'glob'
2+
import {
3+
optArg,
4+
optArgSync,
5+
RimrafAsyncOptions,
6+
RimrafSyncOptions,
7+
} from './opt-arg.js'
28
import pathArg from './path-arg.js'
3-
4-
import { glob, GlobOptions, globSync } from 'glob'
5-
6-
export interface RimrafAsyncOptions {
7-
preserveRoot?: boolean
8-
tmp?: string
9-
maxRetries?: number
10-
retryDelay?: number
11-
backoff?: number
12-
maxBackoff?: number
13-
signal?: AbortSignal
14-
glob?: boolean | GlobOptions
15-
filter?:
16-
| ((path: string, ent: Dirent | Stats) => boolean)
17-
| ((path: string, ent: Dirent | Stats) => Promise<boolean>)
18-
}
19-
20-
export interface RimrafSyncOptions extends RimrafAsyncOptions {
21-
filter?: (path: string, ent: Dirent | Stats) => boolean
22-
}
23-
24-
export type RimrafOptions = RimrafSyncOptions | RimrafAsyncOptions
25-
26-
const typeOrUndef = (val: any, t: string) =>
27-
typeof val === 'undefined' || typeof val === t
28-
29-
export const isRimrafOptions = (o: any): o is RimrafOptions =>
30-
!!o &&
31-
typeof o === 'object' &&
32-
typeOrUndef(o.preserveRoot, 'boolean') &&
33-
typeOrUndef(o.tmp, 'string') &&
34-
typeOrUndef(o.maxRetries, 'number') &&
35-
typeOrUndef(o.retryDelay, 'number') &&
36-
typeOrUndef(o.backoff, 'number') &&
37-
typeOrUndef(o.maxBackoff, 'number') &&
38-
(typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) &&
39-
typeOrUndef(o.filter, 'function')
40-
41-
export const assertRimrafOptions: (o: any) => void = (
42-
o: any
43-
): asserts o is RimrafOptions => {
44-
if (!isRimrafOptions(o)) {
45-
throw new Error('invalid rimraf options')
46-
}
47-
}
48-
49-
import { Dirent, Stats } from 'fs'
509
import { rimrafManual, rimrafManualSync } from './rimraf-manual.js'
5110
import { rimrafMoveRemove, rimrafMoveRemoveSync } from './rimraf-move-remove.js'
5211
import { rimrafNative, rimrafNativeSync } from './rimraf-native.js'
5312
import { rimrafPosix, rimrafPosixSync } from './rimraf-posix.js'
5413
import { rimrafWindows, rimrafWindowsSync } from './rimraf-windows.js'
5514
import { useNative, useNativeSync } from './use-native.js'
5615

16+
export {
17+
assertRimrafOptions,
18+
isRimrafOptions,
19+
RimrafAsyncOptions,
20+
RimrafOptions,
21+
RimrafSyncOptions,
22+
} from './opt-arg.js'
23+
5724
const wrap =
5825
(fn: (p: string, o: RimrafAsyncOptions) => Promise<boolean>) =>
5926
async (

src/opt-arg.ts

+44-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1+
import { Dirent, Stats } from 'fs'
12
import { GlobOptions } from 'glob'
2-
import {
3-
assertRimrafOptions,
4-
RimrafAsyncOptions,
5-
RimrafOptions,
6-
RimrafSyncOptions,
7-
} from './index.js'
3+
4+
const typeOrUndef = (val: any, t: string) =>
5+
typeof val === 'undefined' || typeof val === t
6+
7+
export const isRimrafOptions = (o: any): o is RimrafOptions =>
8+
!!o &&
9+
typeof o === 'object' &&
10+
typeOrUndef(o.preserveRoot, 'boolean') &&
11+
typeOrUndef(o.tmp, 'string') &&
12+
typeOrUndef(o.maxRetries, 'number') &&
13+
typeOrUndef(o.retryDelay, 'number') &&
14+
typeOrUndef(o.backoff, 'number') &&
15+
typeOrUndef(o.maxBackoff, 'number') &&
16+
(typeOrUndef(o.glob, 'boolean') || (o.glob && typeof o.glob === 'object')) &&
17+
typeOrUndef(o.filter, 'function')
18+
19+
export const assertRimrafOptions: (o: any) => void = (
20+
o: any
21+
): asserts o is RimrafOptions => {
22+
if (!isRimrafOptions(o)) {
23+
throw new Error('invalid rimraf options')
24+
}
25+
}
26+
27+
export interface RimrafAsyncOptions {
28+
preserveRoot?: boolean
29+
tmp?: string
30+
maxRetries?: number
31+
retryDelay?: number
32+
backoff?: number
33+
maxBackoff?: number
34+
signal?: AbortSignal
35+
glob?: boolean | GlobOptions
36+
filter?:
37+
| ((path: string, ent: Dirent | Stats) => boolean)
38+
| ((path: string, ent: Dirent | Stats) => Promise<boolean>)
39+
}
40+
41+
export interface RimrafSyncOptions extends RimrafAsyncOptions {
42+
filter?: (path: string, ent: Dirent | Stats) => boolean
43+
}
44+
45+
export type RimrafOptions = RimrafSyncOptions | RimrafAsyncOptions
846

947
const optArgT = <T extends RimrafOptions>(
1048
opt: T

test/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
rimrafSync
1010
} from '../src/index.js'
1111

12+
import * as OPTARG from '../dist/esm/opt-arg.js'
13+
1214
t.test('mocky unit tests to select the correct function', async t => {
1315
// don't mock rimrafManual, so we can test the platform switch
1416
const CALLS: any[] = []
@@ -29,6 +31,7 @@ t.test('mocky unit tests to select the correct function', async t => {
2931
return path
3032
},
3133
'../dist/esm/opt-arg.js': {
34+
...OPTARG,
3235
optArg: (opt: RimrafOptions) => {
3336
CALLS.push(['optArg', opt])
3437
return opt

0 commit comments

Comments
 (0)