Skip to content

Commit

Permalink
fix: mixed posix/win32 namespaces (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jan 9, 2025
1 parent 5c4f321 commit a48a600
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
7 changes: 0 additions & 7 deletions src/path.ts → src/_path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ const _PATH_ROOT_RE = /^[/\\]|^[a-zA-Z]:[/\\]/;
*/
export const sep = "/";

/**
* The platform-specific file delimiter.
*
* Equals to `";"` in windows and `":"` in all other platforms.
*/
export const delimiter = globalThis.process?.platform === "win32" ? ";" : ":";

export const normalize: typeof path.normalize = function (path: string) {
if (path.length === 0) {
return ".";
Expand Down
37 changes: 30 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import type NodePath from "node:path";
import type { posix as Posix, win32 as Win32, PlatformPath } from "node:path";

import * as path from "./path";
import * as _path from "./_path";

export * from "./path";
export * from "./_path";

export type Pathe = Omit<typeof NodePath, "posix" | "win32">;
/**
* The platform-specific file delimiter.
*
* Equals to `";"` in windows and `":"` in all other platforms.
*/
export const delimiter: ";" | ":" = /* @__PURE__ */ (() =>
globalThis.process?.platform === "win32" ? ";" : ":")();

export const posix = path satisfies Pathe;
// Mix namespaces without side-effects of object to allow tree-shaking

export const win32 = path satisfies Pathe;
const _platforms = { posix: undefined, win32: undefined } as {
posix: typeof Posix;
win32: typeof Win32;
};

export default path satisfies Pathe;
const mix = (del: ";" | ":" = delimiter) => {
return new Proxy(_path, {
get(_, prop) {
if (prop === "delimiter") return del;
if (prop === "posix") return posix;
if (prop === "win32") return win32;
return _platforms[prop] || _path[prop];
},
}) as unknown as PlatformPath;
};

export const posix = /* @__PURE__ */ mix(":") as typeof Posix;
export const win32 = /* @__PURE__ */ mix(";") as typeof Win32;

export default posix;
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { join } from "./path";
import { join } from "./_path";
import { normalizeWindowsPath } from "./_internal";

const pathSeparators = new Set(["/", "\\", undefined]);
Expand Down
11 changes: 11 additions & 0 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
toNamespacedPath,
normalizeString,
matchesGlob,
posix,
win32,
} from "../src";

import { normalizeWindowsPath } from "../src/_internal";
Expand Down Expand Up @@ -397,6 +399,8 @@ runTest("toNamespacedPath", toNamespacedPath, {

describe("constants", () => {
it("delimiter", () => {
expect(posix.delimiter).to.equal(":");
expect(win32.delimiter).to.equal(";");
expect(delimiter).to.equal(
globalThis.process?.platform === "win32" ? ";" : ":",
);
Expand All @@ -407,6 +411,13 @@ describe("constants", () => {
});
});

describe("mixed namespaces", () => {
it("nested namespaces work", () => {
expect(posix.win32.posix.delimiter).to.equal(":");
expect(win32.posix.win32.delimiter).to.equal(";");
});
});

runTest("matchesGlob", matchesGlob, [
["/foo/bar", "/foo/**", true],
[String.raw`\foo\bar`, "/foo/**", true],
Expand Down

0 comments on commit a48a600

Please sign in to comment.