Skip to content

Commit 27e9fdd

Browse files
roli-lpciclaude
andauthored
chore: replace fs-extra with native Node.js fs (#510)
* chore: replace fs-extra with native Node.js fs Replace fs-extra with native Node.js fs APIs (node:fs/promises, node:fs). All replacements are available on the supported Node >= 18: - fs.pathExists() → fs.access() with boolean resolution - fs.outputFile() → fs.mkdir({ recursive: true }) + fs.writeFile() - fs.copy() → fs.copyFile() (glob ensures file-only results) - fs.emptyDir() → fs.rm({ recursive, force }) + fs.mkdir({ recursive }) - fs.remove() → fs.rm({ recursive, force }) - fs.createReadStream() → named import from node:fs This removes 1 runtime dependency (fs-extra + 3 transitive deps). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: convert env helper to async/await per review feedback --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f2c2e8a commit 27e9fdd

File tree

12 files changed

+58
-31
lines changed

12 files changed

+58
-31
lines changed

index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
import fs from 'fs-extra'
3+
import fs from 'node:fs/promises'
44
import path from 'path'
55

66
import prettyHrtime from 'pretty-hrtime'
@@ -286,10 +286,14 @@ function css(css, file) {
286286
})
287287

288288
async function outputFile(file, string) {
289-
const fileExists = await fs.pathExists(file)
289+
const fileExists = await fs.access(file).then(
290+
() => true,
291+
() => false,
292+
)
290293
const currentValue = fileExists ? await fs.readFile(file, 'utf8') : null
291294
if (currentValue === string) return
292-
return fs.outputFile(file, string)
295+
await fs.mkdir(path.dirname(file), { recursive: true })
296+
return fs.writeFile(file, string)
293297
}
294298
}
295299

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"dependencies": {
2121
"chokidar": "^3.3.0",
2222
"dependency-graph": "^1.0.0",
23-
"fs-extra": "^11.0.0",
2423
"picocolors": "^1.0.0",
2524
"postcss-load-config": "^5.0.0",
2625
"postcss-reporter": "^7.0.0",

test/ext.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import fs from 'fs-extra'
3+
import fs from 'node:fs/promises'
44
import path from 'path'
55

66
import cli from './helpers/cli.js'
@@ -20,7 +20,12 @@ test('--ext works', async (t) => {
2020
])
2121
t.falsy(error, stderr)
2222

23-
t.truthy(await fs.pathExists(path.join(dir, 'a.css')))
23+
t.truthy(
24+
await fs.access(path.join(dir, 'a.css')).then(
25+
() => true,
26+
() => false,
27+
),
28+
)
2429
})
2530

2631
test('--ext works with no leading dot', async (t) => {
@@ -37,5 +42,10 @@ test('--ext works with no leading dot', async (t) => {
3742
])
3843
t.falsy(error, stderr)
3944

40-
t.truthy(await fs.pathExists(path.join(dir, 'a.css')))
45+
t.truthy(
46+
await fs.access(path.join(dir, 'a.css')).then(
47+
() => true,
48+
() => false,
49+
),
50+
)
4151
})

test/helpers/clean.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import fs from 'fs-extra'
1+
import fs from 'node:fs/promises'
22

33
Promise.all([
4-
fs.emptyDir('./test/fixtures/.tmp/'),
5-
fs.remove('./coverage'),
4+
fs
5+
.rm('./test/fixtures/.tmp/', { recursive: true, force: true })
6+
.then(() => fs.mkdir('./test/fixtures/.tmp/', { recursive: true })),
7+
fs.rm('./coverage', { recursive: true, force: true }),
68
]).catch((err) => {
79
console.error(err)
810
process.exit(1)

test/helpers/env.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import fs from 'fs-extra'
1+
import fs from 'node:fs/promises'
22
import path from 'path'
33
import { glob } from 'tinyglobby'
44

55
import tmp from './tmp.js'
66

7-
export default function (config, fixtures = '**/*', extension = 'cjs') {
7+
export default async function (config, fixtures = '**/*', extension = 'cjs') {
88
const dir = tmp()
99

10-
return Promise.all([
11-
glob(fixtures, { cwd: 'test/fixtures' }).then((list) => {
12-
return list.map((item) => {
13-
return fs.copy(path.join('test/fixtures', item), path.join(dir, item))
14-
})
10+
await fs.mkdir(dir, { recursive: true })
11+
12+
const list = await glob(fixtures, { cwd: 'test/fixtures' })
13+
14+
await Promise.all([
15+
...list.map(async (item) => {
16+
const dest = path.join(dir, item)
17+
await fs.mkdir(path.dirname(dest), { recursive: true })
18+
await fs.copyFile(path.join('test/fixtures', item), dest)
1519
}),
16-
fs.outputFile(path.join(dir, `postcss.config.${extension}`), config),
17-
]).then(() => dir)
20+
fs.writeFile(path.join(dir, `postcss.config.${extension}`), config),
21+
])
22+
23+
return dir
1824
}

test/helpers/read.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from 'fs-extra'
1+
import fs from 'node:fs/promises'
22

33
export default function (path) {
44
return fs.readFile(path, 'utf8').then(

test/map.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import test from 'ava'
2-
import fs from 'fs-extra'
2+
import fs from 'node:fs/promises'
33

44
import cli from './helpers/cli.js'
55
import tmp from './helpers/tmp.js'
@@ -35,7 +35,12 @@ test('--map generates external sourcemaps', async (t) => {
3535

3636
t.falsy(error, stderr)
3737

38-
t.truthy(await fs.pathExists(output.replace('.css', '.css.map')))
38+
t.truthy(
39+
await fs.access(output.replace('.css', '.css.map')).then(
40+
() => true,
41+
() => false,
42+
),
43+
)
3944
})
4045

4146
test('--no-map disables internal sourcemaps', async (t) => {

test/replace.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import fs from 'fs-extra'
3+
import fs from 'node:fs/promises'
44
import path from 'path'
55

66
import cli from './helpers/cli.js'
@@ -12,9 +12,10 @@ test('--replace works', async (t) => {
1212

1313
const output = path.join(dir, 'output.css')
1414

15+
await fs.mkdir(dir, { recursive: true })
1516
await Promise.all([
16-
fs.copy('test/fixtures/import.css', output),
17-
fs.copy('test/fixtures/a.css', path.join(dir, 'a.css')),
17+
fs.copyFile('test/fixtures/import.css', output),
18+
fs.copyFile('test/fixtures/a.css', path.join(dir, 'a.css')),
1819
])
1920

2021
const { error, stderr } = await cli([

test/stdin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import fs from 'fs-extra'
3+
import { createReadStream } from 'node:fs'
44
import path from 'path'
55
import { exec } from 'child_process'
66

@@ -24,5 +24,5 @@ test.cb('reads from stdin', (t) => {
2424
},
2525
)
2626

27-
fs.createReadStream('test/fixtures/a.css').pipe(cp.stdin)
27+
createReadStream('test/fixtures/a.css').pipe(cp.stdin)
2828
})

test/stdout.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import fs from 'fs-extra'
3+
import { createReadStream } from 'node:fs'
44
import path from 'path'
55
import { exec } from 'child_process'
66

@@ -23,5 +23,5 @@ test.cb('writes to stdout', (t) => {
2323
},
2424
)
2525

26-
fs.createReadStream('./test/fixtures/a.sss').pipe(cp.stdin)
26+
createReadStream('./test/fixtures/a.sss').pipe(cp.stdin)
2727
})

0 commit comments

Comments
 (0)