Skip to content

Commit

Permalink
feat: move to node:fs/promises
Browse files Browse the repository at this point in the history
Moves to using `node:fs/promises` rather than using `promisify`
manually.
  • Loading branch information
43081j committed Jun 25, 2024
1 parent bd3c8a0 commit b539d0b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'node:fs';
import { EventEmitter } from 'node:events';
import sysPath from 'node:path';
import { promisify } from 'node:util';
import readdirp from 'readdirp';
import {stat, readdir} from 'node:fs/promises';

import NodeFsHandler from './nodefs-handler.js';
import { anymatch, MatchFunction, isMatcherObject, Matcher } from './anymatch.js';
Expand All @@ -28,9 +28,6 @@ import {
import * as EV from './events.js';
import { EventName } from './events.js';

const stat = promisify(fs.stat);
const readdir = promisify(fs.readdir);

type ThrottleType = 'readdir' | 'watch' | 'add' | 'remove' | 'change';
type EmitArgs = [EventName, Path, any?, any?, any?];

Expand Down
15 changes: 7 additions & 8 deletions src/nodefs-handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'fs';
import sysPath from 'path';
import { promisify } from 'util';
import isBinaryPath from 'is-binary-path';
import {
Path,
Expand All @@ -20,15 +19,15 @@ import {
} from './constants.js';
import * as EV from './events.js';
import type { FSWatcher, WatchHelper, FSWInstanceOptions } from './index.js';
import {
open,
stat,
lstat,
realpath as fsrealpath
} from 'node:fs/promises';

const THROTTLE_MODE_WATCH = 'watch';

const open = promisify(fs.open);
const stat = promisify(fs.stat);
const lstat = promisify(fs.lstat);
const close = promisify(fs.close);
const fsrealpath = promisify(fs.realpath);

const statMethods = { lstat, stat };

// TODO: emit errors properly. Example: EMFILE on Macos.
Expand Down Expand Up @@ -191,7 +190,7 @@ const setFsWatchListener = (
if (isWindows && error.code === 'EPERM') {
try {
const fd = await open(path, 'r');
await close(fd);
await fd.close();
broadcastErr(error);
} catch (err) {
// do nothing
Expand Down

0 comments on commit b539d0b

Please sign in to comment.