Skip to content

Commit 09f9a95

Browse files
committed
refactor: rename ls by readdir
1 parent d7658f3 commit 09f9a95

File tree

12 files changed

+301
-116
lines changed

12 files changed

+301
-116
lines changed

Cargo.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22
[package]
33
authors = ["LongYinan <lynweklm@gmail.com>"]
44
edition = "2021"
5-
name = "hyper_fs"
5+
name = "hyper_fs"
66
version = "0.1.0"
77

88
[lib]
99
crate-type = ["cdylib"]
1010

1111
[dependencies]
12-
napi = "3.0.0"
12+
jwalk = "0.8.1"
13+
napi = "3.0.0"
1314
napi-derive = "3.4"
14-
walkdir = "2.5.0"
15+
serde = "1.0.228"
16+
walkdir = "2.5.0"
1517

1618
[build-dependencies]
1719
napi-build = "2"
1820

1921
[profile.release]
20-
lto = true
22+
lto = true
2123
strip = "symbols"

README.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
# `@napi-rs/package-template`
1+
# Hyper-FS
22

3-
![https://github.com/napi-rs/package-template/actions](https://github.com/napi-rs/package-template/workflows/CI/badge.svg)
3+
# Development
44

5-
> Template project for writing node packages with napi-rs.
6-
7-
# Usage
8-
9-
1. Click **Use this template**.
10-
2. **Clone** your project.
11-
3. Run `yarn install` to install dependencies.
12-
4. Run `yarn napi rename -n [@your-scope/package-name] -b [binary-name]` command under the project folder to rename your package.
5+
1. Run `pnpm install` to install dependencies.
6+
2. Edit files in `/src`
7+
3. Run `pnpm build` to build the lib could be invoked by Node.js
8+
4. Run `pnpm test` to check if everything is ok
139

1410
## Install this test package
1511

__test__/ls.spec.ts

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

__test__/readdir.spec.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import test from 'ava'
2+
import { readdirSync, readdir } from '../index.js'
3+
4+
test('sync: should list files in current directory', (t) => {
5+
const files = readdirSync('.')
6+
7+
t.true(Array.isArray(files))
8+
t.true(files.length > 0)
9+
10+
// Verify Dirent structure
11+
const packageJson = files.find((f) => f.name === 'package.json')
12+
t.truthy(packageJson, 'Result should contain package.json')
13+
t.is(packageJson?.isDir, false)
14+
t.true(packageJson?.path.includes('package.json'))
15+
16+
const srcDir = files.find((f) => f.name === 'src')
17+
if (srcDir) {
18+
t.is(srcDir.isDir, true, 'src should be identified as a directory')
19+
}
20+
})
21+
22+
test('async: should list files in current directory', async (t) => {
23+
const files = await readdir('.')
24+
t.true(files.length > 0)
25+
t.truthy(files.find((f) => f.name === 'package.json'))
26+
})
27+
28+
test('concurrency: run with specific thread count', (t) => {
29+
const files = readdirSync('.', {
30+
concurrency: 4,
31+
})
32+
t.true(files.length > 0)
33+
})
34+
35+
test('concurrency: run with high thread count (stress test)', (t) => {
36+
const files = readdirSync('.', {
37+
concurrency: 100,
38+
})
39+
t.true(files.length > 0)
40+
})
41+
42+
test('options: skip_hidden should filter out dotfiles', (t) => {
43+
// First, ensure we can see hidden files (default behavior usually depends on impl,
44+
// but based on your rust code, default is false)
45+
const allFiles = readdirSync('.', { skipHidden: false })
46+
// Assuming this repo has a .git folder or similar
47+
const hasHidden = allFiles.some((f) => f.name.startsWith('.'))
48+
49+
if (hasHidden) {
50+
const visibleFiles = readdirSync('.', { skipHidden: true })
51+
const hiddenRemains = visibleFiles.some((f) => f.name.startsWith('.'))
52+
t.false(hiddenRemains, 'Should not contain hidden files when skip_hidden is true')
53+
} else {
54+
t.pass('No hidden files found in root to test skipping')
55+
}
56+
})
57+
58+
test('error: should throw on non-existent directory', async (t) => {
59+
t.throws(() => readdirSync('./path/to/nowhere'))
60+
await t.throwsAsync(async () => await readdir('./path/to/nowhere'))
61+
})

benchmark/bench.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import { Bench } from 'tinybench'
1+
import * as fs from 'node:fs'
2+
import * as path from 'node:path'
3+
import { fileURLToPath } from 'node:url'
24

3-
import { plus100 } from '../index.js'
5+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
46

5-
function add(a: number) {
6-
return a + 100
7-
}
8-
9-
const b = new Bench()
7+
async function runBenchmarks() {
8+
const files = fs.readdirSync(__dirname).filter((file) => {
9+
return file.endsWith('.ts') && file !== 'bench.ts' && !file.endsWith('.d.ts')
10+
})
1011

11-
b.add('Native a + 100', () => {
12-
plus100(10)
13-
})
12+
console.log(`Found ${files.length} benchmark files to run...`)
1413

15-
b.add('JavaScript a + 100', () => {
16-
add(10)
17-
})
14+
for (const file of files) {
15+
console.log(`\n========================================`)
16+
console.log(`Running benchmark: ${file}`)
17+
console.log(`========================================`)
1818

19-
await b.run()
19+
try {
20+
await import(path.join(__dirname, file))
21+
} catch (e) {
22+
console.error(`Error running ${file}:`, e)
23+
}
24+
}
25+
}
2026

21-
console.table(b.table())
27+
runBenchmarks()

benchmark/readdir.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Bench } from 'tinybench'
2+
import * as fs from 'node:fs'
3+
import { readdirSync } from '../index.js'
4+
import * as path from 'node:path'
5+
6+
const bench = new Bench({ time: 1000 })
7+
const targetDir = path.resolve(process.cwd(), 'node_modules')
8+
const dir = fs.existsSync(targetDir) ? targetDir : process.cwd()
9+
10+
console.log(`Benchmarking readdir on: ${dir}`)
11+
12+
bench
13+
.add('Node.js fs.readdirSync', () => {
14+
fs.readdirSync(dir, { withFileTypes: true })
15+
})
16+
.add('Node.js fs.readdirSync (recursive)', () => {
17+
fs.readdirSync(dir, { recursive: true, withFileTypes: true })
18+
})
19+
.add('hyper-fs readdirSync (default)', () => {
20+
readdirSync(dir)
21+
})
22+
.add('hyper-fs readdirSync (4 threads)', () => {
23+
readdirSync(dir, { concurrency: 4 })
24+
})
25+
26+
await bench.run()
27+
28+
console.table(bench.table())

index.d.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
/* auto-generated by NAPI-RS */
22
/* eslint-disable */
3-
export declare function ls(path: string): Array<string>
3+
export interface Dirent {
4+
name: string
5+
path: string
6+
isDir: boolean
7+
}
8+
9+
export declare function readdir(path: string, options?: ReaddirOptions | undefined | null): Promise<unknown>
10+
11+
export interface ReaddirOptions {
12+
skipHidden?: boolean
13+
concurrency?: number
14+
}
15+
16+
export declare function readdirSync(path: string, options?: ReaddirOptions | undefined | null): Array<Dirent>

0 commit comments

Comments
 (0)