Skip to content

Commit

Permalink
use NodeJS.ErrnoException instead of FsError
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Mar 2, 2023
1 parent 450e3d2 commit 20e3799
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 27 deletions.
10 changes: 5 additions & 5 deletions src/fix-eperm.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { promises, chmodSync, FsError } from './fs.js'
import { chmodSync, promises } from './fs.js'
const { chmod } = promises

export const fixEPERM =
(fn: (path: string) => Promise<any>) => async (path: string) => {
try {
return await fn(path)
} catch (er) {
const fer = er as FsError
const fer = er as NodeJS.ErrnoException
if (fer?.code === 'ENOENT') {
return
}
if (fer?.code === 'EPERM') {
try {
await chmod(path, 0o666)
} catch (er2) {
const fer2 = er2 as FsError
const fer2 = er2 as NodeJS.ErrnoException
if (fer2?.code === 'ENOENT') {
return
}
Expand All @@ -30,15 +30,15 @@ export const fixEPERMSync = (fn: (path: string) => any) => (path: string) => {
try {
return fn(path)
} catch (er) {
const fer = er as FsError
const fer = er as NodeJS.ErrnoException
if (fer?.code === 'ENOENT') {
return
}
if (fer?.code === 'EPERM') {
try {
chmodSync(path, 0o666)
} catch (er2) {
const fer2 = er2 as FsError
const fer2 = er2 as NodeJS.ErrnoException
if (fer2?.code === 'ENOENT') {
return
}
Expand Down
5 changes: 0 additions & 5 deletions src/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@

import fs from 'fs'

export type FsError = Error & {
code?: string
path?: string
}

// sync ones just take the sync version from node
export {
chmodSync,
Expand Down
3 changes: 1 addition & 2 deletions src/ignore-enoent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { FsError } from './fs.js'

export const ignoreENOENT = async (p: Promise<any>) =>
p.catch(er => {
Expand All @@ -11,7 +10,7 @@ export const ignoreENOENTSync = (fn: () => any) => {
try {
return fn()
} catch (er) {
if ((er as FsError)?.code !== 'ENOENT') {
if ((er as NodeJS.ErrnoException)?.code !== 'ENOENT') {
throw er
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/readdir-or-error.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// returns an array of entries if readdir() works,
// or the error that readdir() raised if not.
import { FsError, promises, readdirSync } from './fs.js'
import { promises, readdirSync } from './fs.js'
const { readdir } = promises
export const readdirOrError = (path: string) =>
readdir(path).catch(er => er as FsError)
readdir(path).catch(er => er as NodeJS.ErrnoException)
export const readdirOrErrorSync = (path: string) => {
try {
return readdirSync(path)
} catch (er) {
return er as FsError
return er as NodeJS.ErrnoException
}
}
5 changes: 2 additions & 3 deletions src/retry-busy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// note: max backoff is the maximum that any *single* backoff will do

import { RimrafOptions } from '.'
import { FsError } from './fs.js'

export const MAXBACKOFF = 200
export const RATE = 1.2
Expand All @@ -23,7 +22,7 @@ export const retryBusy = (fn: (path: string) => Promise<any>) => {
try {
return await fn(path)
} catch (er) {
const fer = er as FsError
const fer = er as NodeJS.ErrnoException
if (fer?.path === path && fer?.code && codes.has(fer.code)) {
backoff = Math.ceil(backoff * rate)
total = backoff + total
Expand Down Expand Up @@ -56,7 +55,7 @@ export const retryBusySync = (fn: (path: string) => any) => {
try {
return fn(path)
} catch (er) {
const fer = er as FsError
const fer = er as NodeJS.ErrnoException
if (
fer?.path === path &&
fer?.code &&
Expand Down
7 changes: 3 additions & 4 deletions src/rimraf-move-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { ignoreENOENT, ignoreENOENTSync } from './ignore-enoent.js'

import {
chmodSync,
FsError,
promises as fsPromises,
renameSync,
rmdirSync,
Expand Down Expand Up @@ -54,16 +53,16 @@ const unlinkFixEPERMSync = (path: string) => {
try {
unlinkSync(path)
} catch (er) {
if ((er as FsError)?.code === 'EPERM') {
if ((er as NodeJS.ErrnoException)?.code === 'EPERM') {
try {
return chmodSync(path, 0o666)
} catch (er2) {
if ((er2 as FsError)?.code === 'ENOENT') {
if ((er2 as NodeJS.ErrnoException)?.code === 'ENOENT') {
return
}
throw er
}
} else if ((er as FsError)?.code === 'ENOENT') {
} else if ((er as NodeJS.ErrnoException)?.code === 'ENOENT') {
return
}
throw er
Expand Down
10 changes: 5 additions & 5 deletions src/rimraf-windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
// Note: "move then remove" is 2-10 times slower, and just as unreliable.

import { parse, resolve } from 'path'
import { ignoreENOENT, ignoreENOENTSync } from './ignore-enoent.js'
import { RimrafOptions } from '.'
import { fixEPERM, fixEPERMSync } from './fix-eperm.js'
import { promises, rmdirSync, unlinkSync } from './fs.js'
import { ignoreENOENT, ignoreENOENTSync } from './ignore-enoent.js'
import { readdirOrError, readdirOrErrorSync } from './readdir-or-error.js'
import { retryBusy, retryBusySync } from './retry-busy.js'
import { rimrafMoveRemove, rimrafMoveRemoveSync } from './rimraf-move-remove.js'
import { FsError, promises, rmdirSync, unlinkSync } from './fs.js'
import { RimrafOptions } from '.'
const { unlink, rmdir } = promises

const rimrafWindowsFile = retryBusy(fixEPERM(unlink))
Expand All @@ -30,7 +30,7 @@ const rimrafWindowsDirMoveRemoveFallback = async (
try {
await rimrafWindowsDir(path, opt)
} catch (er) {
if ((er as FsError)?.code === 'ENOTEMPTY') {
if ((er as NodeJS.ErrnoException)?.code === 'ENOTEMPTY') {
return await rimrafMoveRemove(path, opt)
}
throw er
Expand All @@ -44,7 +44,7 @@ const rimrafWindowsDirMoveRemoveFallbackSync = (
try {
rimrafWindowsDirSync(path, opt)
} catch (er) {
if ((er as FsError)?.code === 'ENOTEMPTY') {
if ((er as NodeJS.ErrnoException)?.code === 'ENOTEMPTY') {
return rimrafMoveRemoveSync(path, opt)
}
throw er
Expand Down

0 comments on commit 20e3799

Please sign in to comment.