diff --git a/src/files.ts b/src/files.ts index 0e804d4f2..c52f02f04 100644 --- a/src/files.ts +++ b/src/files.ts @@ -7,13 +7,8 @@ import {cwd} from "node:process"; import {fileURLToPath} from "node:url"; import {isEnoent} from "./error.js"; -export function toOsPath(path: string): string { - return path.split(sep).join(op.sep); -} - -export function fromOsPath(path: string): string { - return path.split(op.sep).join(sep); -} +export const toOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(sep).join(op.sep); +export const fromOsPath = sep === op.sep ? (path: string) => path : (path: string) => path.split(op.sep).join(sep); /** * Returns the relative path from the current working directory to the given diff --git a/src/path.ts b/src/path.ts index 3e92766d4..5dc315336 100644 --- a/src/path.ts +++ b/src/path.ts @@ -1,4 +1,5 @@ -import {dirname, isAbsolute, join, normalize, relative, resolve} from "node:path/posix"; +import op from "node:path"; +import {dirname, join} from "node:path/posix"; /** * Returns the normalized relative path from "/file/path/to/a" to @@ -86,6 +87,7 @@ export function parseRelativeUrl(url: string): {pathname: string; search: string } export function within(root: string, path: string): boolean { + const {relative, normalize, resolve, isAbsolute} = op; path = relative(normalize(resolve(root)), normalize(resolve(path))); return !path.startsWith("..") && !isAbsolute(path); } diff --git a/test/path-test.ts b/test/path-test.ts index 9ac9a8c02..3e373c7fb 100644 --- a/test/path-test.ts +++ b/test/path-test.ts @@ -1,5 +1,5 @@ import assert from "node:assert"; -import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath} from "../src/path.js"; +import {isPathImport, parseRelativeUrl, relativePath, resolveLocalPath, resolvePath, within} from "../src/path.js"; describe("resolvePath(source, target)", () => { it("returns the path to the specified target within the source root", () => { @@ -151,3 +151,21 @@ describe("parseRelativeUrl(url)", () => { assert.deepStrictEqual(parseRelativeUrl("foo?bar#baz"), {pathname: "foo", search: "?bar", hash: "#baz"}); }); }); + +describe("within(root, path)", () => { + it("returns true for paths within the current working directory", () => { + assert.strictEqual(within(process.cwd(), "dist"), true, "dist"); + assert.strictEqual(within(process.cwd(), "./dist"), true, "./dist"); + assert.strictEqual(within(process.cwd(), "dist/"), true, "dist/"); + assert.strictEqual(within(process.cwd(), "./dist/"), true, "./dist/"); + assert.strictEqual(within(process.cwd(), "foo/../dist"), true, "foo/../dist"); + assert.strictEqual(within(process.cwd(), "foo/../dist/"), true, "foo/../dist/"); + assert.strictEqual(within(process.cwd(), "./foo/../dist/"), true, "./foo/../dist/"); + assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar"); + assert.strictEqual(within(process.cwd(), "foo/bar"), true, "foo/bar"); + assert.strictEqual(within(process.cwd(), "../framework/dist"), true, "../framework/dist"); + assert.strictEqual(within(process.cwd(), "../framework2/dist"), false, "../framework2/dist"); + assert.strictEqual(within(process.cwd(), "../dist"), false, "../dist"); + assert.strictEqual(within(process.cwd(), "/dist"), false, "/dist"); + }); +});