Skip to content

Commit aaa9a73

Browse files
authored
feat: support sync api
feat: support sync api
1 parent 616d15d commit aaa9a73

File tree

8 files changed

+289
-147
lines changed

8 files changed

+289
-147
lines changed

build.config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineBuildConfig } from 'unbuild'
2+
import Quansync from 'unplugin-quansync/rollup'
23

34
export default defineBuildConfig({
45
entries: [
@@ -7,4 +8,9 @@ export default defineBuildConfig({
78
],
89
declaration: true,
910
clean: true,
11+
hooks: {
12+
'rollup:options': function (ctx, options) {
13+
options.plugins.push(Quansync())
14+
},
15+
},
1016
})

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@
4242
},
4343
"dependencies": {
4444
"@antfu/utils": "^8.1.0",
45+
"@quansync/fs": "^0.1.1",
4546
"defu": "^6.1.4",
46-
"jiti": "^2.4.2"
47+
"jiti": "^2.4.2",
48+
"quansync": "^0.2.6"
4749
},
4850
"devDependencies": {
4951
"@antfu/eslint-config": "^4.1.1",
@@ -55,6 +57,7 @@
5557
"lodash-es": "^4.17.21",
5658
"typescript": "^5.7.3",
5759
"unbuild": "^3.3.1",
60+
"unplugin-quansync": "^0.3.3",
5861
"vitest": "^3.0.5"
5962
}
6063
}

pnpm-lock.yaml

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fs.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import fs, { constants, promises as fsp } from 'node:fs'
21
import { dirname, parse, resolve } from 'node:path'
32
import process from 'node:process'
3+
import { lstat, stat } from '@quansync/fs'
4+
import { quansync } from 'quansync/macro'
45

56
export interface FindUpOptions {
67
/**
@@ -21,44 +22,43 @@ export interface FindUpOptions {
2122
allowSymlinks?: boolean
2223
}
2324

24-
function existsSync(fp: string) {
25+
const isFile = quansync(async (path: string, allowSymlinks: boolean) => {
2526
try {
26-
fs.accessSync(fp, constants.R_OK)
27-
return true
27+
return (await (allowSymlinks ? lstat : stat)(path)).isFile()
2828
}
2929
catch {
3030
return false
3131
}
32-
}
33-
34-
export async function findUp(paths: string[], options: FindUpOptions = {}): Promise<string[]> {
35-
const {
36-
cwd = process.cwd(),
37-
stopAt = parse(cwd).root,
38-
multiple = false,
39-
allowSymlinks = true,
40-
} = options
32+
})
4133

42-
let current = cwd
34+
export const findUp = quansync(
35+
async (paths: string[], options: FindUpOptions = {}): Promise<string[]> => {
36+
const {
37+
cwd = process.cwd(),
38+
stopAt = parse(cwd).root,
39+
multiple = false,
40+
allowSymlinks = true,
41+
} = options
4342

44-
const files: string[] = []
43+
let current = cwd
4544

46-
const stat = allowSymlinks ? fsp.stat : fsp.lstat
45+
const files: string[] = []
4746

48-
while (current && current !== stopAt) {
49-
for (const path of paths) {
50-
const filepath = resolve(current, path)
51-
if (existsSync(filepath) && (await stat(filepath)).isFile()) {
52-
files.push(filepath)
53-
if (!multiple)
54-
return files
47+
while (current && current !== stopAt) {
48+
for (const path of paths) {
49+
const filepath = resolve(current, path)
50+
if (await isFile(filepath, allowSymlinks)) {
51+
files.push(filepath)
52+
if (!multiple)
53+
return files
54+
}
5555
}
56+
const parent = dirname(current)
57+
if (parent === current)
58+
break
59+
current = parent
5660
}
57-
const parent = dirname(current)
58-
if (parent === current)
59-
break
60-
current = parent
61-
}
6261

63-
return files
64-
}
62+
return files
63+
},
64+
)

0 commit comments

Comments
 (0)