Skip to content

Commit d1d72a8

Browse files
committed
build(deps): use @flex-development/when
- https://github.com/flex-development/when/tree/1.0.0 Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent fc3e70a commit d1d72a8

16 files changed

Lines changed: 266 additions & 595 deletions

eslint.config.mjs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,8 @@ const config = [
3333
'unicorn/text-encoding-identifier-case': 0
3434
}
3535
},
36-
{
37-
files: ['src/internal/__tests__/is-promise.spec.mts'],
38-
rules: {
39-
'unicorn/no-thenable': 0
40-
}
41-
},
4236
{
4337
files: [
44-
'src/internal/chain-or-call.mts',
4538
'src/lib/get-source.mts',
4639
'src/lib/is-directory.mts',
4740
'src/lib/is-file.mts',

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
"@flex-development/is-builtin": "3.2.0",
166166
"@flex-development/pathe": "4.0.2",
167167
"@flex-development/pkg-types": "4.1.0",
168+
"@flex-development/when": "1.0.0",
168169
"devlop": "1.1.0"
169170
},
170171
"devDependencies": {

src/internal/__tests__/is-promise.spec.mts

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/internal/chain-or-call.mts

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/internal/is-promise.mts

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/lib/__snapshots__/node/resolve-module.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`unit:lib/resolveModule > fs (default) > should return resolved URL ("#internal/chain-or-call") 1`] = `file://\${process.cwd()}/src/internal/chain-or-call.mjs`;
3+
exports[`unit:lib/resolveModule > fs (default) > should return resolved URL ("#internal/identity") 1`] = `file://\${process.cwd()}/src/internal/identity.mjs`;
44

55
exports[`unit:lib/resolveModule > fs (default) > should return resolved URL ("../../lib") 1`] = `file://\${process.cwd()}/src/lib/index.mjs`;
66

@@ -30,7 +30,7 @@ exports[`unit:lib/resolveModule > fs (default) > should return resolved URL ("su
3030

3131
exports[`unit:lib/resolveModule > fs (default) > should return resolved URL ("unist") 1`] = `file://\${process.cwd()}/node_modules/@types/unist/index.d.ts`;
3232

33-
exports[`unit:lib/resolveModule > fs (only async) > should return resolved URL ("#internal/chain-or-call") 1`] = `file://\${process.cwd()}/src/internal/chain-or-call.mjs`;
33+
exports[`unit:lib/resolveModule > fs (only async) > should return resolved URL ("#internal/identity") 1`] = `file://\${process.cwd()}/src/internal/identity.mjs`;
3434

3535
exports[`unit:lib/resolveModule > fs (only async) > should return resolved URL ("../../lib") 1`] = `file://\${process.cwd()}/src/lib/index.mjs`;
3636

src/lib/__tests__/get-source.spec.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
import fsa from '#fixtures/fsa'
7-
import isPromise from '#internal/is-promise'
87
import testSubject from '#lib/get-source'
98
import fsCaseType, { type FileSystemCaseType } from '#tests/utils/fs-case-type'
109
import {
@@ -14,6 +13,7 @@ import {
1413
} from '@flex-development/errnode'
1514
import type { FileSystem } from '@flex-development/mlly'
1615
import pathe from '@flex-development/pathe'
16+
import { isThenable } from '@flex-development/when'
1717

1818
describe('unit:lib/getSource', () => {
1919
describe.each<[fst: FileSystemCaseType, fs?: FileSystem | null | undefined]>([
@@ -76,7 +76,7 @@ describe('unit:lib/getSource', () => {
7676
const result = testSubject(id, { ...options, fs })
7777

7878
// Expect
79-
expect(result).to.not.satisfy(isPromise)
79+
expect(result).to.not.satisfy(isThenable)
8080
expect(result).to.not.be.a('string')
8181
expect(String(result)).toMatchSnapshot()
8282
})
@@ -116,7 +116,7 @@ describe('unit:lib/getSource', () => {
116116
const result = testSubject(id, { ...options, fs })
117117

118118
// Expect
119-
expect(result).to.satisfy(isPromise)
119+
expect(result).to.satisfy(isThenable)
120120
expect(await result).toMatchSnapshot()
121121
})
122122
})

src/lib/__tests__/resolve-module.spec.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('unit:lib/resolveModule', () => {
3838
})
3939

4040
it.each<Case>([
41-
['#internal/chain-or-call', import.meta.url, { conditions, ext: 'mjs' }],
41+
['#internal/identity', import.meta.url, { conditions, ext: 'mjs' }],
4242
['../../lib', import.meta.url, { ext: () => 'mjs' }],
4343
['@types/unist', import.meta.url],
4444
['data:text/javascript,', import.meta.url],

src/lib/get-source.mts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
* @module mlly/lib/getSource
44
*/
55

6-
import chainOrCall from '#internal/chain-or-call'
76
import fs from '#internal/fs'
8-
import isPromise from '#internal/is-promise'
7+
import identity from '#internal/identity'
98
import process from '#internal/process'
109
import isFile from '#lib/is-file'
1110
import isModuleId from '#lib/is-module-id'
@@ -24,6 +23,7 @@ import type {
2423
ModuleId,
2524
Protocol
2625
} from '@flex-development/mlly'
26+
import when from '@flex-development/when'
2727
import { ok } from 'devlop'
2828

2929
export default getSource
@@ -157,21 +157,7 @@ function getSource(
157157
)
158158
}
159159

160-
/**
161-
* The source code.
162-
*
163-
* @var {ReturnType<GetSourceHandler>} code
164-
*/
165-
let code: ReturnType<GetSourceHandler> = handle.call(context, url)
166-
167-
// resolve source code.
168-
if (isPromise(code)) {
169-
void code.then(resolved => (code = resolved))
170-
}
171-
172-
return chainOrCall(code, () => {
173-
return ok(!isPromise(code), 'expected `code` to be resolved'), code
174-
})
160+
return when(handle.call(context, url), identity)
175161
}
176162

177163
/**
@@ -208,15 +194,8 @@ function data(this: GetSourceContext, url: URL): Buffer {
208194
function file(this: GetSourceContext, url: URL): Awaitable<FileContent | null> {
209195
ok(url.protocol === 'file:', 'expected `file:` URL')
210196

211-
/**
212-
* Whether the file exists.
213-
*
214-
* @const {Awaitable<boolean>} exists
215-
*/
216-
const exists: Awaitable<boolean> = isFile(url, this.fs)
217-
218-
return chainOrCall(exists, isFile => {
219-
return isFile ?? exists ? this.fs.readFile(url, this.encoding) : null
197+
return when(isFile(url, this.fs), exists => {
198+
return exists ? this.fs.readFile(url, this.encoding) : null
220199
})
221200
}
222201

src/lib/is-directory.mts

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,11 @@
33
* @module mlly/lib/isDirectory
44
*/
55

6-
import chainOrCall from '#internal/chain-or-call'
76
import constant from '#internal/constant'
87
import dfs from '#internal/fs'
9-
import identity from '#internal/identity'
10-
import isPromise from '#internal/is-promise'
118
import isModuleId from '#lib/is-module-id'
12-
import type {
13-
Awaitable,
14-
FileSystem,
15-
ModuleId,
16-
Stats
17-
} from '@flex-development/mlly'
9+
import type { Awaitable, FileSystem } from '@flex-development/mlly'
10+
import when from '@flex-development/when'
1811

1912
export default isDirectory
2013

@@ -66,28 +59,14 @@ function isDirectory(
6659
id: unknown,
6760
fs?: FileSystem | null | undefined
6861
): Awaitable<boolean> {
69-
if (isModuleId(id)) {
70-
try {
71-
if (typeof id === 'string' && id.startsWith('file:')) id = new URL(id)
62+
if (typeof id === 'string' && id.startsWith('file:')) id = new URL(id)
63+
if (!isModuleId(id)) return false
7264

73-
/**
74-
* The stats object.
75-
*
76-
* @var {Awaitable<Stats | null>} stats
77-
*/
78-
let stats: Awaitable<Stats | null> = (fs ?? dfs).stat(id as ModuleId)
65+
fs ??= dfs
7966

80-
// resolve the stats object.
81-
if (isPromise<Stats>(stats)) stats = stats.then(identity, constant(null))
82-
83-
return chainOrCall(stats, (): Awaitable<boolean> => {
84-
if (!isPromise(stats)) return stats.isDirectory()
85-
return stats.then(stats => stats?.isDirectory() ?? false)
86-
})
87-
} catch {
88-
// swallow error.
89-
}
67+
try {
68+
return when(fs.stat(id), stats => stats.isDirectory(), constant(false))
69+
} catch {
70+
return false // swallow error.
9071
}
91-
92-
return false
9372
}

0 commit comments

Comments
 (0)