Skip to content

Commit

Permalink
Rename path import
Browse files Browse the repository at this point in the history
  • Loading branch information
paulmillr committed Jan 25, 2025
1 parent 22a35b8 commit bc4b3fa
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
42 changes: 21 additions & 21 deletions src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { watchFile, unwatchFile, watch as fs_watch } from 'node:fs';
import type { WatchListener, WatchEventType, Stats, FSWatcher as NativeFsWatcher } from 'node:fs';
import { open, stat, lstat, realpath as fsrealpath } from 'node:fs/promises';
import * as sysPath from 'node:path';
import type { FSWatcher as NativeFsWatcher, Stats, WatchEventType, WatchListener } from 'node:fs';
import { watch as fs_watch, unwatchFile, watchFile } from 'node:fs';
import { realpath as fsrealpath, lstat, open, stat } from 'node:fs/promises';
import { type as osType } from 'node:os';
import type { FSWatcher, WatchHelper, FSWInstanceOptions, Throttler } from './index.js';
import * as sp from 'node:path';
import type { EntryInfo } from 'readdirp';
import type { FSWatcher, FSWInstanceOptions, Throttler, WatchHelper } from './index.js';

export type Path = string;

Expand Down Expand Up @@ -80,7 +80,7 @@ const binaryExtensions = new Set([
'z', 'zip', 'zipx',
]);
const isBinaryPath = (filePath: string) =>
binaryExtensions.has(sysPath.extname(filePath).slice(1).toLowerCase());
binaryExtensions.has(sp.extname(filePath).slice(1).toLowerCase());

// TODO: emit errors properly. Example: EMFILE on Macos.
const foreach = <V extends unknown>(val: V, fn: (arg: V) => unknown) => {
Expand Down Expand Up @@ -157,7 +157,7 @@ function createFsWatchInstance(
// emit based on events occurring for files from a directory's watcher in
// case the file's watcher misses it (and rely on throttling to de-dupe)
if (evPath && path !== evPath) {
fsWatchBroadcast(sysPath.resolve(path, evPath), KEY_LISTENERS, sysPath.join(path, evPath));
fsWatchBroadcast(sp.resolve(path, evPath), KEY_LISTENERS, sp.join(path, evPath));
}
};
try {
Expand Down Expand Up @@ -380,11 +380,11 @@ export class NodeFsHandler {
listener: (path: string, newStats?: any) => void | Promise<void>
): (() => void) | undefined {
const opts = this.fsw.options;
const directory = sysPath.dirname(path);
const basename = sysPath.basename(path);
const directory = sp.dirname(path);
const basename = sp.basename(path);
const parent = this.fsw._getWatchedDir(directory);
parent.add(basename);
const absolutePath = sysPath.resolve(path);
const absolutePath = sp.resolve(path);
const options: Partial<FSWInstanceOptions> = {
persistent: opts.persistent,
};
Expand Down Expand Up @@ -416,8 +416,8 @@ export class NodeFsHandler {
if (this.fsw.closed) {
return;
}
const dirname = sysPath.dirname(file);
const basename = sysPath.basename(file);
const dirname = sp.dirname(file);
const basename = sp.basename(file);
const parent = this.fsw._getWatchedDir(dirname);
// stats is always present
let prevStats = stats;
Expand Down Expand Up @@ -537,7 +537,7 @@ export class NodeFsHandler {
throttler: Throttler
): Promise<unknown> | undefined {
// Normalize the directory name on Windows
directory = sysPath.join(directory, '');
directory = sp.join(directory, '');

throttler = this.fsw._throttle('readdir', directory, 1000) as Throttler;
if (!throttler) return;
Expand All @@ -557,7 +557,7 @@ export class NodeFsHandler {
return;
}
const item = entry.path;
let path = sysPath.join(directory, item);
let path = sp.join(directory, item);
current.add(item);

if (
Expand All @@ -578,7 +578,7 @@ export class NodeFsHandler {
this.fsw._incrReadyCount();

// ensure relativeness of path is preserved in case of watcher reuse
path = sysPath.join(dir, sysPath.relative(dir, path));
path = sp.join(dir, sp.relative(dir, path));

this._addToNodeFs(path, initialAdd, wh, depth + 1);
}
Expand Down Expand Up @@ -636,14 +636,14 @@ export class NodeFsHandler {
wh: WatchHelper,
realpath: string
): Promise<(() => void) | undefined> {
const parentDir = this.fsw._getWatchedDir(sysPath.dirname(dir));
const tracked = parentDir.has(sysPath.basename(dir));
const parentDir = this.fsw._getWatchedDir(sp.dirname(dir));
const tracked = parentDir.has(sp.basename(dir));
if (!(initialAdd && this.fsw.options.ignoreInitial) && !target && !tracked) {
this.fsw._emit(EV.ADD_DIR, dir, stats);
}

// ensure dir is tracked (harmless if redundant)
parentDir.add(sysPath.basename(dir));
parentDir.add(sp.basename(dir));
this.fsw._getWatchedDir(dir);
let throttler!: Throttler;
let closer;
Expand Down Expand Up @@ -705,7 +705,7 @@ export class NodeFsHandler {
const follow = this.fsw.options.followSymlinks;
let closer;
if (stats.isDirectory()) {
const absPath = sysPath.resolve(path);
const absPath = sp.resolve(path);
const targetPath = follow ? await fsrealpath(path) : path;
if (this.fsw.closed) return;
closer = await this._handleDir(
Expand All @@ -725,15 +725,15 @@ export class NodeFsHandler {
} else if (stats.isSymbolicLink()) {
const targetPath = follow ? await fsrealpath(path) : path;
if (this.fsw.closed) return;
const parent = sysPath.dirname(wh.watchPath);
const parent = sp.dirname(wh.watchPath);
this.fsw._getWatchedDir(parent).add(wh.watchPath);
this.fsw._emit(EV.ADD, wh.watchPath, stats);
closer = await this._handleDir(parent, stats, initialAdd, depth, path, wh, targetPath);
if (this.fsw.closed) return;

// preserve this symlink's target path
if (targetPath !== undefined) {
this.fsw._symlinkPaths.set(sysPath.resolve(path), targetPath);
this.fsw._symlinkPaths.set(sp.resolve(path), targetPath);
}
} else {
closer = this._handleFile(wh.watchPath, stats, initialAdd);
Expand Down
54 changes: 27 additions & 27 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { EventEmitter } from 'node:events';
import { stat as statcb, Stats } from 'node:fs';
import { readdir, stat } from 'node:fs/promises';
import * as sysPath from 'node:path';
import * as sp from 'node:path';
import { type EntryInfo, readdirp, type ReaddirpOptions, ReaddirpStream } from 'readdirp';
import {
EMPTY_FN,
Expand Down Expand Up @@ -95,11 +95,11 @@ function createPattern(matcher: Matcher): MatchFunction {
return (string) => {
if (matcher.path === string) return true;
if (matcher.recursive) {
const relative = sysPath.relative(matcher.path, string);
const relative = sp.relative(matcher.path, string);
if (!relative) {
return false;
}
return !relative.startsWith('..') && !sysPath.isAbsolute(relative);
return !relative.startsWith('..') && !sp.isAbsolute(relative);
}
return false;
};
Expand All @@ -109,7 +109,7 @@ function createPattern(matcher: Matcher): MatchFunction {

function normalizePath(path: Path): Path {
if (typeof path !== 'string') throw new Error('string expected');
path = sysPath.normalize(path);
path = sp.normalize(path);
path = path.replace(/\\/g, '/');
let prepend = false;
if (path.startsWith('//')) prepend = true;
Expand Down Expand Up @@ -179,24 +179,24 @@ const toUnix = (string: string) => {

// Our version of upath.normalize
// TODO: this is not equal to path-normalize module - investigate why
const normalizePathToUnix = (path: Path) => toUnix(sysPath.normalize(toUnix(path)));
const normalizePathToUnix = (path: Path) => toUnix(sp.normalize(toUnix(path)));

// TODO: refactor
const normalizeIgnored =
(cwd = '') =>
(path: unknown): string => {
if (typeof path === 'string') {
return normalizePathToUnix(sysPath.isAbsolute(path) ? path : sysPath.join(cwd, path));
return normalizePathToUnix(sp.isAbsolute(path) ? path : sp.join(cwd, path));
} else {
return path as string;
}
};

const getAbsolutePath = (path: Path, cwd: Path) => {
if (sysPath.isAbsolute(path)) {
if (sp.isAbsolute(path)) {
return path;
}
return sysPath.join(cwd, path);
return sp.join(cwd, path);
};

const EMPTY_SET = Object.freeze(new Set<string>());
Expand Down Expand Up @@ -231,7 +231,7 @@ class DirEntry {
await readdir(dir);
} catch (err) {
if (this._removeWatcher) {
this._removeWatcher(sysPath.dirname(dir), sysPath.basename(dir));
this._removeWatcher(sp.dirname(dir), sp.basename(dir));
}
}
}
Expand Down Expand Up @@ -273,7 +273,7 @@ export class WatchHelper {
const watchPath = path;
this.path = path = path.replace(REPLACER_RE, '');
this.watchPath = watchPath;
this.fullWatchPath = sysPath.resolve(watchPath);
this.fullWatchPath = sp.resolve(watchPath);
this.dirParts = [];
this.dirParts.forEach((parts) => {
if (parts.length > 1) parts.pop();
Expand All @@ -283,7 +283,7 @@ export class WatchHelper {
}

entryPath(entry: EntryInfo): Path {
return sysPath.join(this.watchPath, sysPath.relative(this.watchPath, entry.fullPath));
return sp.join(this.watchPath, sp.relative(this.watchPath, entry.fullPath));
}

filterPath(entry: EntryInfo): boolean {
Expand Down Expand Up @@ -491,7 +491,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
).then((results) => {
if (this.closed) return;
results.forEach((item) => {
if (item) this.add(sysPath.dirname(item), sysPath.basename(_origAdd || item));
if (item) this.add(sp.dirname(item), sp.basename(_origAdd || item));
});
});

Expand All @@ -508,9 +508,9 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {

paths.forEach((path) => {
// convert to absolute path unless relative path already matches
if (!sysPath.isAbsolute(path) && !this._closers.has(path)) {
if (cwd) path = sysPath.join(cwd, path);
path = sysPath.resolve(path);
if (!sp.isAbsolute(path) && !this._closers.has(path)) {
if (cwd) path = sp.join(cwd, path);
path = sp.resolve(path);
}

this._closePath(path);
Expand Down Expand Up @@ -574,7 +574,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
getWatched(): Record<string, string[]> {
const watchList: Record<string, string[]> = {};
this._watched.forEach((entry, dir) => {
const key = this.options.cwd ? sysPath.relative(this.options.cwd, dir) : dir;
const key = this.options.cwd ? sp.relative(this.options.cwd, dir) : dir;
const index = key || ONE_DOT;
watchList[index] = entry.getChildren().sort();
});
Expand All @@ -601,8 +601,8 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
if (this.closed) return;

const opts = this.options;
if (isWindows) path = sysPath.normalize(path);
if (opts.cwd) path = sysPath.relative(opts.cwd, path);
if (isWindows) path = sp.normalize(path);
if (opts.cwd) path = sp.relative(opts.cwd, path);
const args: EmitArgs | EmitErrorArgs = [path];
if (stats != null) args.push(stats);

Expand Down Expand Up @@ -665,7 +665,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
stats === undefined &&
(event === EV.ADD || event === EV.ADD_DIR || event === EV.CHANGE)
) {
const fullPath = opts.cwd ? sysPath.join(opts.cwd, path) : path;
const fullPath = opts.cwd ? sp.join(opts.cwd, path) : path;
let stats;
try {
stats = await stat(fullPath);
Expand Down Expand Up @@ -759,8 +759,8 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
let timeoutHandler: NodeJS.Timeout;

let fullPath = path;
if (this.options.cwd && !sysPath.isAbsolute(path)) {
fullPath = sysPath.join(this.options.cwd, path);
if (this.options.cwd && !sp.isAbsolute(path)) {
fullPath = sp.join(this.options.cwd, path);
}

const now = new Date();
Expand Down Expand Up @@ -841,7 +841,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
* @param directory path of the directory
*/
_getWatchedDir(directory: string): DirEntry {
const dir = sysPath.resolve(directory);
const dir = sp.resolve(directory);
if (!this._watched.has(dir)) this._watched.set(dir, new DirEntry(dir, this._boundRemove));
return this._watched.get(dir)!;
}
Expand All @@ -868,8 +868,8 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
// if what is being deleted is a directory, get that directory's paths
// for recursive deleting and cleaning of watched object
// if it is not a directory, nestedDirectoryChildren will be empty array
const path = sysPath.join(directory, item);
const fullPath = sysPath.resolve(path);
const path = sp.join(directory, item);
const fullPath = sp.resolve(path);
isDirectory =
isDirectory != null ? isDirectory : this._watched.has(path) || this._watched.has(fullPath);

Expand Down Expand Up @@ -906,7 +906,7 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {

// If we wait for this file to be fully written, cancel the wait.
let relPath = path;
if (this.options.cwd) relPath = sysPath.relative(this.options.cwd, path);
if (this.options.cwd) relPath = sp.relative(this.options.cwd, path);
if (this.options.awaitWriteFinish && this._pendingWrites.has(relPath)) {
const event = this._pendingWrites.get(relPath).cancelWait();
if (event === EV.ADD) return;
Expand All @@ -928,8 +928,8 @@ export class FSWatcher extends EventEmitter<FSWatcherEventMap> {
*/
_closePath(path: Path): void {
this._closeFile(path);
const dir = sysPath.dirname(path);
this._getWatchedDir(dir).remove(sysPath.basename(path));
const dir = sp.dirname(path);
this._getWatchedDir(dir).remove(sp.basename(path));
}

/**
Expand Down

0 comments on commit bc4b3fa

Please sign in to comment.