Skip to content

Commit

Permalink
feat: remove custom promisification in favor of fs/promises (#54)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this package no longer exports the full set of core fs functions
  • Loading branch information
nlf committed Oct 11, 2022
1 parent e666309 commit b788931
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 58 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ polyfills, and extensions, of the core `fs` module.

## Features

- all exposed functions return promises
- `fs.cp` polyfill for node < 16.7.0
- `fs.withTempDir` added

Expand Down
2 changes: 1 addition & 1 deletion lib/cp/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('../fs.js')
const fs = require('fs/promises')
const getOptions = require('../common/get-options.js')
const node = require('../common/node.js')
const polyfill = require('./polyfill.js')
Expand Down
2 changes: 1 addition & 1 deletion lib/cp/polyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const {
symlink,
unlink,
utimes,
} = require('../fs.js')
} = require('fs/promises')
const {
dirname,
isAbsolute,
Expand Down
14 changes: 0 additions & 14 deletions lib/fs.js

This file was deleted.

10 changes: 7 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
'use strict'

const cp = require('./cp/index.js')
const withTempDir = require('./with-temp-dir.js')

module.exports = {
...require('./fs.js'),
cp: require('./cp/index.js'),
withTempDir: require('./with-temp-dir.js'),
cp,
withTempDir,
}
2 changes: 1 addition & 1 deletion lib/with-temp-dir.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { join, sep } = require('path')

const getOptions = require('./common/get-options.js')
const { mkdir, mkdtemp, rm } = require('./fs.js')
const { mkdir, mkdtemp, rm } = require('fs/promises')

// create a temp directory, ensure its permissions match its parent, then call
// the supplied function passing it the path to the directory. clean up after
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"tap": "^16.0.1"
},
"dependencies": {
"@gar/promisify": "^1.1.3",
"semver": "^7.3.5"
},
"engines": {
Expand Down
10 changes: 0 additions & 10 deletions test/common/file-url-to-path.js

This file was deleted.

13 changes: 8 additions & 5 deletions test/cp/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { stat } = require('fs/promises')
const { join } = require('path')
const t = require('tap')

const fs = require('../../')
const cp = require('../../lib/cp/index.js')

t.test('can copy a file', async (t) => {
const dir = t.testdir({
Expand All @@ -10,9 +11,10 @@ t.test('can copy a file', async (t) => {
const src = join(dir, 'file')
const dest = join(dir, 'dest')

await fs.cp(src, dest)
await cp(src, dest)

t.equal(await fs.exists(dest), true, 'dest exits')
const exists = await stat(dest).then(() => true).catch(() => false)
t.equal(exists, true, 'dest exits')
})

t.test('can copy a directory', async (t) => {
Expand All @@ -22,7 +24,8 @@ t.test('can copy a directory', async (t) => {
const src = join(dir, 'directory')
const dest = join(dir, 'dest')

await fs.cp(src, dest, { recursive: true })
await cp(src, dest, { recursive: true })

t.equal(await fs.exists(dest), true, 'dest exists')
const exists = await stat(dest).then(() => true).catch(() => false)
t.equal(exists, true, 'dest exists')
})
8 changes: 0 additions & 8 deletions test/fs.js

This file was deleted.

27 changes: 14 additions & 13 deletions test/with-temp-dir.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,59 @@
const { lstat } = require('fs/promises')
const { normalize } = require('path')
const t = require('tap')

const fs = require('../')
const withTempDir = require('../lib/with-temp-dir.js')

t.test('creates a temp directory and passes it to provided function', async (t) => {
// normalize is necessary until https://github.com/tapjs/libtap/pull/40 is shipped
const root = normalize(t.testdir())
const rootStat = await fs.lstat(root)
const rootStat = await lstat(root)
let tempDir
await fs.withTempDir(root, async (dir) => {
await withTempDir(root, async (dir) => {
tempDir = dir
t.type(dir, 'string')
t.ok(dir.startsWith(root), 'dir is contained within the root')
const stat = await fs.lstat(dir)
const stat = await lstat(dir)
t.ok(stat.isDirectory(), 'dir is an actual directory')
t.equal(stat.uid, rootStat.uid, 'temp directory has same owning user')
t.equal(stat.gid, rootStat.gid, 'temp directory has same owning group')
})
await t.rejects(fs.lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
await t.rejects(lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
})

t.test('result from provided function bubbles out', async (t) => {
// normalize is necessary until https://github.com/tapjs/libtap/pull/40 is shipped
const root = normalize(t.testdir())
const rootStat = await fs.lstat(root)
const rootStat = await lstat(root)
let tempDir
const result = await fs.withTempDir(root, async (dir) => {
const result = await withTempDir(root, async (dir) => {
tempDir = dir
t.type(dir, 'string')
t.ok(dir.startsWith(root), 'dir is contained within the root')
const stat = await fs.lstat(dir)
const stat = await lstat(dir)
t.ok(stat.isDirectory(), 'dir is an actual directory')
t.equal(stat.uid, rootStat.uid, 'temp directory has same owning user')
t.equal(stat.gid, rootStat.gid, 'temp directory has same owning group')
return 'finished'
})
t.equal(result, 'finished', 'resolved value is returned')
await t.rejects(fs.lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
await t.rejects(lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
})

t.test('cleans up when provided function rejects', async (t) => {
// normalize is necessary until https://github.com/tapjs/libtap/pull/40 is shipped
const root = normalize(t.testdir())
const rootStat = await fs.lstat(root)
const rootStat = await lstat(root)
let tempDir
await t.rejects(fs.withTempDir(root, async (dir) => {
await t.rejects(withTempDir(root, async (dir) => {
tempDir = dir
t.type(dir, 'string')
t.ok(dir.startsWith(root), 'dir is contained within the root')
const stat = await fs.lstat(dir)
const stat = await lstat(dir)
t.ok(stat.isDirectory(), 'dir is an actual directory')
t.equal(stat.uid, rootStat.uid, 'temp directory has same owning user')
t.equal(stat.gid, rootStat.gid, 'temp directory has same owning group')
throw new Error('this is bad')
}), { message: 'this is bad' })
await t.rejects(fs.lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
await t.rejects(lstat(tempDir), { code: 'ENOENT' }, 'temp directory was removed')
})

0 comments on commit b788931

Please sign in to comment.