Skip to content

Commit ba34a8e

Browse files
committed
refactor(internal): [matchFiles] use file system tree
- `fs.readdirSync` now returns `Dirent` objects - https://github.com/flex-development/fst Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 694e84a commit ba34a8e

File tree

17 files changed

+269
-164
lines changed

17 files changed

+269
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,5 @@ codecov
7575
# ------------------------------------------------------------------------------
7676
**/*config.*.timestamp*
7777
**/.temp/
78+
**/*.scratch.*
7879
**/scratch.*

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ convenience of TypeScript users who do not hoist packages, but may need to `impo
105105

106106
### Interfaces
107107

108+
- [`Dirent`](./src/interfaces/dirent.mts)
108109
- [`FileSystem`](./src/interfaces/file-system.mts)
109110
- [`LoadTsconfigOptions`](./src/interfaces/options-load-tsconfig.mts)
110111
- [`ModuleResolutionHostOptions`](./src/interfaces/options-module-resolution-host.mts)

__tests__/reporters/verbose.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class VerboseReporter extends DefaultReporter implements Reporter {
134134
* @return {undefined}
135135
*/
136136
public override onTaskUpdate(packs: TaskResultPack[]): undefined {
137-
return void (this.isTTY && void super.onTaskUpdate(packs))
137+
return void packs
138138
}
139139

140140
/**

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,7 @@
5858
"default": "./dist/interfaces/*.d.mts"
5959
},
6060
"#internal/fs": {
61-
"types": {
62-
"tsconfig-utils": "./src/internal/fs.d.mts",
63-
"default": "./dist/internal/fs.d.mts"
64-
},
61+
"types": "./src/internal/fs.d.mts",
6562
"browser": {
6663
"tsconfig-utils": "./src/internal/fs.browser.mts",
6764
"default": "./dist/internal/fs.browser.mjs"
@@ -86,7 +83,7 @@
8683
"module": "./dist/index.mjs",
8784
"types": "./dist/index.d.mts",
8885
"scripts": {
89-
"build": "yarn clean:build; tsc -p tsconfig.build.json --noEmit false",
86+
"build": "yarn clean:build; tsc -p tsconfig.build.json --noEmit false; trash ./dist/internal/*.d.mts && trash ./dist/{interfaces,types}/*.mjs",
9087
"check:ci": "yarn dedupe --check && yarn check:format && yarn check:lint && yarn check:spelling && yarn typecheck && yarn test:cov && yarn pack && yarn check:types:build && attw package.tgz && yarn clean:pack",
9188
"check:format": "dprint check --incremental=false",
9289
"check:lint": "eslint --exit-on-fatal-error --max-warnings 0 .",
@@ -125,6 +122,8 @@
125122
},
126123
"dependencies": {
127124
"@flex-development/errnode": "3.1.1",
125+
"@flex-development/fst": "flex-development/fst#commit=9fae8b6708a1e392bb874a68aa28730fe6cefdf5",
126+
"@flex-development/fst-util-from-fs": "flex-development/fst-util-from-fs#commit=e1e2279f829efeaf7357b76852b29d29da40c91e",
128127
"@flex-development/mlly": "1.0.0-alpha.20",
129128
"@flex-development/pathe": "4.0.1",
130129
"@flex-development/pkg-types": "4.1.0",
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @file Type Tests - Dirent
3+
* @module tsconfig-utils/interfaces/tests/unit-d/Dirent
4+
*/
5+
6+
import type TestSubject from '#interfaces/dirent'
7+
import type * as fst from '@flex-development/fst-util-from-fs'
8+
9+
describe('unit-d:interfaces/Dirent', () => {
10+
it('should extend fst.Dirent', () => {
11+
expectTypeOf<TestSubject>().toMatchTypeOf<fst.Dirent>()
12+
})
13+
14+
it('should match [parentPath: string]', () => {
15+
expectTypeOf<TestSubject>()
16+
.toHaveProperty('parentPath')
17+
.toEqualTypeOf<string>()
18+
})
19+
})

src/interfaces/__tests__/file-system.spec-d.mts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import type TestSubject from '#interfaces/file-system'
77
import type * as mlly from '@flex-development/mlly'
8+
import type { Dirent } from '@flex-development/tsconfig-utils'
89

910
describe('unit-d:interfaces/FileSystem', () => {
1011
it('should extend mlly.FileSystem', () => {
@@ -19,14 +20,16 @@ describe('unit-d:interfaces/FileSystem', () => {
1920
})
2021

2122
describe('parameters', () => {
22-
it('should be callable with [mlly.ModuleId]', () => {
23-
expectTypeOf<Subject>().parameters.toEqualTypeOf<[mlly.ModuleId]>()
23+
it('should be callable with [mlly.ModuleId, { withFileTypes: true }]', () => {
24+
expectTypeOf<Subject>()
25+
.parameters
26+
.toEqualTypeOf<[mlly.ModuleId, { withFileTypes: true }]>()
2427
})
2528
})
2629

2730
describe('returns', () => {
28-
it('should return string[]', () => {
29-
expectTypeOf<Subject>().returns.toEqualTypeOf<string[]>()
31+
it('should return readonly Dirent[]', () => {
32+
expectTypeOf<Subject>().returns.toEqualTypeOf<readonly Dirent[]>()
3033
})
3134
})
3235
})

src/interfaces/dirent.mts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @file Interfaces - Dirent
3+
* @module tsconfig-utils/interfaces/Dirent
4+
*/
5+
6+
import type * as fst from '@flex-development/fst-util-from-fs'
7+
8+
/**
9+
* Directory content entry.
10+
*
11+
* @see {@linkcode fst.Dirent}
12+
*
13+
* @extends {fst.Dirent}
14+
*/
15+
interface Dirent extends fst.Dirent {
16+
/**
17+
* Path to parent directory.
18+
*/
19+
parentPath: string
20+
}
21+
22+
export type { Dirent as default }

src/interfaces/file-system.mts

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

66
import type * as mlly from '@flex-development/mlly'
7+
import type { Dirent } from '@flex-development/tsconfig-utils'
78

89
/**
910
* File system API.
@@ -14,18 +15,27 @@ import type * as mlly from '@flex-development/mlly'
1415
*/
1516
interface FileSystem extends mlly.FileSystem {
1617
/**
17-
* Read the contents of a directory.
18+
* Read the contents of the directory at `id`.
1819
*
20+
* @see {@linkcode Dirent}
1921
* @see {@linkcode mlly.ModuleId}
2022
*
2123
* @this {void}
2224
*
2325
* @param {mlly.ModuleId} id
24-
* The path or `file:` URL to handle
25-
* @return {string[]}
26-
* List of filenames
26+
* Module id of directory to read
27+
* @param {{ withFileTypes: true }} options
28+
* Read options
29+
* @param {true} options.withFileTypes
30+
* Return a list of dirent objects instead of strings or `Buffer`s
31+
* @return {ReadonlyArray<Dirent>}
32+
* Directory content list
2733
*/
28-
readdirSync(this: void, id: mlly.ModuleId): string[]
34+
readdirSync(
35+
this: void,
36+
id: mlly.ModuleId,
37+
options: { withFileTypes: true }
38+
): readonly Dirent[]
2939
}
3040

3141
export type { FileSystem as default }

src/interfaces/index.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* @module tsconfig-utils/interfaces
44
*/
55

6+
export type { default as Dirent } from '#interfaces/dirent'
67
export type { default as FileSystem } from '#interfaces/file-system'
78
export type {
89
default as ModuleResolutionHost

src/internal/__tests__/match-files.spec.mts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,24 @@
66
import testSubject from '#internal/match-files'
77
import toPath from '#internal/to-path'
88
import createModuleResolutionHost from '#lib/create-module-resolution-host'
9+
import type { Extensions } from '@flex-development/fst-util-from-fs'
910
import * as mlly from '@flex-development/mlly'
1011
import type { FileSystem } from '@flex-development/tsconfig-utils'
1112
import { alphabetize, identity } from '@flex-development/tutils'
1213
import ts from 'typescript'
14+
import tsconfigBuild from '../../../tsconfig.build.json' with { type: 'json' }
1315
import tsconfig from '../../../tsconfig.json' with { type: 'json' }
1416

1517
describe('unit:internal/matchFiles', () => {
1618
it.each<[
1719
id: mlly.ModuleId,
18-
extensions: Set<string> | readonly string[] | undefined,
19-
exclude: Set<string> | readonly string[] | undefined,
20-
include: Set<string> | readonly string[] | undefined,
20+
extensions: Extensions | null | undefined,
21+
exclude: Set<string> | readonly string[] | null | undefined,
22+
include: Set<string> | readonly string[] | null | undefined,
2123
useCaseSensitiveFileNames?: boolean | null | undefined,
2224
depth?: number | null | undefined,
23-
fs?: FileSystem | null | undefined
25+
fs?: Partial<FileSystem> | null | undefined
2426
]>([
25-
[
26-
new URL('src', mlly.cwd()),
27-
['.cjs', '.cts', '.js', '.json', '.mjs', '.mts', '.ts'],
28-
tsconfig.exclude,
29-
tsconfig.include,
30-
null,
31-
0
32-
],
3327
[
3428
mlly.cwd(),
3529
['.cjs', '.cts', '.js', '.json', '.mjs', '.mts', '.ts'],
@@ -43,8 +37,22 @@ describe('unit:internal/matchFiles', () => {
4337
tsconfig.include,
4438
undefined,
4539
1
40+
],
41+
[
42+
mlly.cwd(),
43+
['.mts'],
44+
tsconfigBuild.exclude,
45+
tsconfigBuild.include
46+
],
47+
[
48+
new URL('src', mlly.cwd()),
49+
['.cjs', '.cts', '.js', '.json', '.mjs', '.mts', '.ts'],
50+
tsconfig.exclude,
51+
tsconfig.include,
52+
null,
53+
0
4654
]
47-
])('should return list of files under directory at `id` (%#)', (
55+
])('should return list of matched files (%#)', (
4856
id,
4957
extensions,
5058
exclude,
@@ -75,6 +83,6 @@ describe('unit:internal/matchFiles', () => {
7583
)
7684

7785
// Expect
78-
expect(result).to.be.an('array').and.eql(expected).and.not.be.frozen
86+
expect(result).to.be.an('array').and.eql(expected).and.be.frozen
7987
})
8088
})

0 commit comments

Comments
 (0)