Skip to content

Commit

Permalink
dont need didIgnore fns
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Oct 7, 2024
1 parent b0833aa commit 9986a81
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 66 deletions.
28 changes: 12 additions & 16 deletions src/fix-eperm.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import { errorCode } from './error.js'
import { chmodSync, promises } from './fs.js'
import {
ignoreENOENT,
didIgnoreENOENT,
ignoreENOENTSync,
didIgnoreENOENTSync,
} from './ignore-enoent.js'
import { ignoreENOENT, ignoreENOENTSync } from './ignore-enoent.js'
const { chmod } = promises

export const fixEPERM =
(fn: (path: string) => Promise<unknown>) =>
async (path: string): Promise<void> => {
try {
await ignoreENOENT(fn(path))
return
return void (await ignoreENOENT(fn(path)))
} catch (er) {
if (errorCode(er) === 'EPERM') {
if (await didIgnoreENOENT(chmod(path, 0o666), er)) {
if (
!(await ignoreENOENT(
chmod(path, 0o666).then(() => true),
er,
))
) {
return
}
await fn(path)
return
return void (await fn(path))
}
throw er
}
Expand All @@ -30,15 +28,13 @@ export const fixEPERMSync =
(fn: (path: string) => unknown) =>
(path: string): void => {
try {
ignoreENOENTSync(() => fn(path))
return
return void ignoreENOENTSync(() => fn(path))
} catch (er) {
if (errorCode(er) === 'EPERM') {
if (didIgnoreENOENTSync(() => chmodSync(path, 0o666), er)) {
if (!ignoreENOENTSync(() => (chmodSync(path, 0o666), true), er)) {
return
}
fn(path)
return
return void fn(path)
}
throw er
}
Expand Down
28 changes: 3 additions & 25 deletions src/ignore-enoent.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,20 @@
import { errorCode } from './error.js'

export const ignoreENOENT = async <T>(p: Promise<T>) =>
export const ignoreENOENT = async <T>(p: Promise<T>, rethrow?: unknown) =>
p.catch(er => {
if (errorCode(er) === 'ENOENT') {
return
}
throw er
throw rethrow ?? er
})

export const didIgnoreENOENT = async (p: Promise<unknown>, rethrow?: unknown) =>
p
.then(() => false)
.catch(er => {
if (errorCode(er) === 'ENOENT') {
return true
}
throw rethrow ?? er
})

export const ignoreENOENTSync = <T>(fn: () => T) => {
export const ignoreENOENTSync = <T>(fn: () => T, rethrow?: unknown) => {
try {
return fn()
} catch (er) {
if (errorCode(er) === 'ENOENT') {
return
}
throw er
}
}

export const didIgnoreENOENTSync = (fn: () => unknown, rethrow?: unknown) => {
try {
fn()
return false
} catch (er) {
if (errorCode(er) === 'ENOENT') {
return true
}
throw rethrow ?? er
}
}
28 changes: 3 additions & 25 deletions test/ignore-enoent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import t from 'tap'
import {
ignoreENOENT,
didIgnoreENOENT,
ignoreENOENTSync,
didIgnoreENOENTSync,
} from '../src/ignore-enoent.js'
import { ignoreENOENT, ignoreENOENTSync } from '../src/ignore-enoent.js'

const enoent = () => Object.assign(new Error('no ent'), { code: 'ENOENT' })
const eperm = () => Object.assign(new Error('eperm'), { code: 'EPERM' })
Expand All @@ -16,16 +11,9 @@ t.test('async', async t => {
{ code: 'EPERM' },
'eperm is not',
)
t.equal(await didIgnoreENOENT(Promise.reject(enoent())), true)
t.equal(await didIgnoreENOENT(Promise.resolve()), false)
const rethrow = new Error('rethrow')
t.rejects(
didIgnoreENOENT(Promise.reject(eperm())),
{ code: 'EPERM' },
'throws error',
)
t.rejects(
didIgnoreENOENT(Promise.reject(eperm()), rethrow),
ignoreENOENT(Promise.reject(eperm()), rethrow),
rethrow,
'or rethrows passed in error',
)
Expand All @@ -44,19 +32,9 @@ t.test('sync', t => {
{ code: 'EPERM' },
'eperm is not fine sync',
)
t.equal(didIgnoreENOENTSync(throwEnoent), true)
t.equal(
didIgnoreENOENTSync(() => {}),
false,
)
const rethrow = new Error('rethrow')
t.throws(
() => didIgnoreENOENTSync(throwEperm),
{ code: 'EPERM' },
'throws error',
)
t.throws(
() => didIgnoreENOENTSync(throwEperm, rethrow),
() => ignoreENOENTSync(throwEperm, rethrow),
rethrow,
'or rethrows passed in error',
)
Expand Down

0 comments on commit 9986a81

Please sign in to comment.