Skip to content

Commit 70b31be

Browse files
committed
feat: support sync usage
1 parent 616d15d commit 70b31be

File tree

8 files changed

+306
-152
lines changed

8 files changed

+306
-152
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"dependencies": {
4444
"@antfu/utils": "^8.1.0",
4545
"defu": "^6.1.4",
46-
"jiti": "^2.4.2"
46+
"jiti": "^2.4.2",
47+
"quansync": "^0.2.2"
4748
},
4849
"devDependencies": {
4950
"@antfu/eslint-config": "^4.1.1",
@@ -55,6 +56,7 @@
5556
"lodash-es": "^4.17.21",
5657
"typescript": "^5.7.3",
5758
"unbuild": "^3.3.1",
59+
"unplugin-quansync": "^0.3.2",
5860
"vitest": "^3.0.5"
5961
}
6062
}

pnpm-lock.yaml

Lines changed: 65 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: 65 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import fs, { constants, promises as fsp } from 'node:fs'
1+
import fs from 'node:fs'
22
import { dirname, parse, resolve } from 'node:path'
33
import process from 'node:process'
4+
import { quansync } from 'quansync/macro'
45

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

24-
function existsSync(fp: string) {
25-
try {
26-
fs.accessSync(fp, constants.R_OK)
27-
return true
28-
}
29-
catch {
30-
return false
31-
}
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
25+
const isFile = quansync({
26+
sync: (path: string, allowSymlinks: boolean) => {
27+
try {
28+
return fs[allowSymlinks ? 'lstatSync' : 'statSync'](path).isFile()
29+
}
30+
catch {
31+
return false
32+
}
33+
},
34+
async: async (path: string, allowSymlinks: boolean) => {
35+
try {
36+
return (await fs.promises[allowSymlinks ? 'lstat' : 'stat'](path)).isFile()
37+
}
38+
catch {
39+
return false
40+
}
41+
},
42+
})
4143

42-
let current = cwd
44+
export const findUp = quansync(
45+
async (paths: string[], options: FindUpOptions = {}): Promise<string[]> => {
46+
const {
47+
cwd = process.cwd(),
48+
stopAt = parse(cwd).root,
49+
multiple = false,
50+
allowSymlinks = true,
51+
} = options
4352

44-
const files: string[] = []
53+
let current = cwd
4554

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

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
57+
while (current && current !== stopAt) {
58+
for (const path of paths) {
59+
const filepath = resolve(current, path)
60+
if (await isFile(filepath, allowSymlinks)) {
61+
files.push(filepath)
62+
if (!multiple)
63+
return files
64+
}
5565
}
66+
const parent = dirname(current)
67+
if (parent === current)
68+
break
69+
current = parent
5670
}
57-
const parent = dirname(current)
58-
if (parent === current)
59-
break
60-
current = parent
61-
}
6271

63-
return files
64-
}
72+
return files
73+
},
74+
)
75+
76+
export const readFile = quansync({
77+
sync: (path: string) => fs.readFileSync(path, 'utf8'),
78+
async: path => fs.promises.readFile(path, 'utf8'),
79+
})
80+
81+
export const writeFile = quansync({
82+
sync: (path: string, data: string) => fs.writeFileSync(path, data),
83+
async: (path, data) => fs.promises.writeFile(path, data),
84+
})
85+
86+
export const unlink = quansync({
87+
sync: (path: string) => {
88+
try {
89+
fs.unlinkSync(path)
90+
}
91+
catch {}
92+
},
93+
async: path => fs.promises.unlink(path).catch(() => {}),
94+
})

0 commit comments

Comments
 (0)