diff --git a/std/path/_constants.ts b/std/path/_constants.ts index ae0aac1846..186c32ab53 100644 --- a/std/path/_constants.ts +++ b/std/path/_constants.ts @@ -1,7 +1,6 @@ // Copyright the Browserify authors. MIT License. // Ported from https://github.com/browserify/path-browserify/ - -const { build } = Deno; +/** This module is browser compatible. */ // Alphabet chars. export const CHAR_UPPERCASE_A = 65; /* A */ @@ -48,7 +47,14 @@ export const CHAR_EQUAL = 61; /* = */ export const CHAR_0 = 48; /* 0 */ export const CHAR_9 = 57; /* 9 */ -const isWindows = build.os == "windows"; +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const navigator = (globalThis as any).navigator; -export const SEP = isWindows ? "\\" : "/"; -export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; +let isWindows = false; +if (globalThis.Deno != null) { + isWindows = Deno.build.os == "windows"; +} else if (navigator?.appVersion != null) { + isWindows = navigator.appVersion.includes("Win"); +} + +export { isWindows }; diff --git a/std/path/_globrex.ts b/std/path/_globrex.ts index 2b3af14ad0..6ad297d864 100644 --- a/std/path/_globrex.ts +++ b/std/path/_globrex.ts @@ -1,8 +1,10 @@ // This file is ported from globrex@0.1.2 // MIT License // Copyright (c) 2018 Terkel Gjervig Nielsen +/** This module is browser compatible. */ + +import { isWindows as isWin } from "./_constants.ts"; -const isWin = Deno.build.os === "windows"; const SEP = isWin ? `(?:\\\\|\\/)` : `\\/`; const SEP_ESC = isWin ? `\\\\` : `/`; const SEP_RAW = isWin ? `\\` : `/`; diff --git a/std/path/interface.ts b/std/path/_interface.ts similarity index 93% rename from std/path/interface.ts rename to std/path/_interface.ts index b31c89ea73..6c82c9c35c 100644 --- a/std/path/interface.ts +++ b/std/path/_interface.ts @@ -1,3 +1,5 @@ +/** This module is browser compatible. */ + /** * A parsed path object generated by path.parse() or consumed by path.format(). */ diff --git a/std/path/_util.ts b/std/path/_util.ts index 2776303cb6..8ae40373b2 100644 --- a/std/path/_util.ts +++ b/std/path/_util.ts @@ -1,7 +1,8 @@ // Copyright the Browserify authors. MIT License. // Ported from https://github.com/browserify/path-browserify/ +/** This module is browser compatible. */ -import { FormatInputPathObject } from "./interface.ts"; +import { FormatInputPathObject } from "./_interface.ts"; import { CHAR_UPPERCASE_A, CHAR_LOWERCASE_A, diff --git a/std/path/common.ts b/std/path/common.ts index e0e51ef239..01470105ca 100644 --- a/std/path/common.ts +++ b/std/path/common.ts @@ -1,4 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/** This module is browser compatible. */ import { SEP } from "./separator.ts"; diff --git a/std/path/glob.ts b/std/path/glob.ts index 80672579d2..34fc213f62 100644 --- a/std/path/glob.ts +++ b/std/path/glob.ts @@ -1,3 +1,6 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +/** This module is browser compatible. */ + import { SEP, SEP_PATTERN } from "./separator.ts"; import { globrex } from "./_globrex.ts"; import { join, normalize } from "./mod.ts"; diff --git a/std/path/mod.ts b/std/path/mod.ts index 9cb7f1edb2..0b4156e699 100644 --- a/std/path/mod.ts +++ b/std/path/mod.ts @@ -1,11 +1,11 @@ // Copyright the Browserify authors. MIT License. // Ported mostly from https://github.com/browserify/path-browserify/ +/** This module is browser compatible. */ +import { isWindows } from "./_constants.ts"; import * as _win32 from "./win32.ts"; import * as _posix from "./posix.ts"; -const isWindows = Deno.build.os == "windows"; - const path = isWindows ? _win32 : _posix; export const win32 = _win32; @@ -29,5 +29,5 @@ export const { export * from "./common.ts"; export { SEP, SEP_PATTERN } from "./separator.ts"; -export * from "./interface.ts"; +export * from "./_interface.ts"; export * from "./glob.ts"; diff --git a/std/path/posix.ts b/std/path/posix.ts index e88eb3f973..365232e336 100644 --- a/std/path/posix.ts +++ b/std/path/posix.ts @@ -1,8 +1,8 @@ // Copyright the Browserify authors. MIT License. // Ported from https://github.com/browserify/path-browserify/ +/** This module is browser compatible. */ -const { cwd } = Deno; -import { FormatInputPathObject, ParsedPath } from "./interface.ts"; +import { FormatInputPathObject, ParsedPath } from "./_interface.ts"; import { CHAR_DOT, CHAR_FORWARD_SLASH } from "./_constants.ts"; import { @@ -24,7 +24,12 @@ export function resolve(...pathSegments: string[]): string { let path: string; if (i >= 0) path = pathSegments[i]; - else path = cwd(); + else { + if (globalThis.Deno == null) { + throw new TypeError("Resolved a relative path without a CWD."); + } + path = Deno.cwd(); + } assertPath(path); diff --git a/std/path/separator.ts b/std/path/separator.ts index fb990b8086..4b54ad4384 100644 --- a/std/path/separator.ts +++ b/std/path/separator.ts @@ -1,4 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -const isWindows = Deno.build.os == "windows"; +/** This module is browser compatible. */ + +import { isWindows } from "./_constants.ts"; + export const SEP = isWindows ? "\\" : "/"; export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/; diff --git a/std/path/win32.ts b/std/path/win32.ts index 0557b3768e..f556c5b736 100644 --- a/std/path/win32.ts +++ b/std/path/win32.ts @@ -1,8 +1,8 @@ // Copyright the Browserify authors. MIT License. // Ported from https://github.com/browserify/path-browserify/ +/** This module is browser compatible. */ -const { cwd, env } = Deno; -import { FormatInputPathObject, ParsedPath } from "./interface.ts"; +import { FormatInputPathObject, ParsedPath } from "./_interface.ts"; import { CHAR_DOT, CHAR_BACKWARD_SLASH, @@ -32,14 +32,20 @@ export function resolve(...pathSegments: string[]): string { if (i >= 0) { path = pathSegments[i]; } else if (!resolvedDevice) { - path = cwd(); + if (globalThis.Deno == null) { + throw new TypeError("Resolved a drive-letter-less path without a CWD."); + } + path = Deno.cwd(); } else { + if (globalThis.Deno == null) { + throw new TypeError("Resolved a relative path without a CWD."); + } // Windows has the concept of drive-specific current working // directories. If we've resolved a drive letter but not yet an // absolute path, get cwd for that drive, or the process cwd if // the drive cwd is not available. We're sure the device is not // a UNC path at this points, because UNC paths are always absolute. - path = env.get(`=${resolvedDevice}`) || cwd(); + path = Deno.env.get(`=${resolvedDevice}`) || Deno.cwd(); // Verify that a cwd was found and that it actually points // to our drive. If not, default to the drive's root.