summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-05-31 18:48:32 +0100
committerGitHub <noreply@github.com>2020-05-31 13:48:32 -0400
commit10573183af14b4f303909ee9394e0e72cf10b45d (patch)
tree45a2136c24d8cbb7d36218f9d1c10432bef301f8
parent464f5bf769f23b85ecc2c99b54b639c5a71d700f (diff)
fix(std/path): Support browsers (#6003)
-rw-r--r--std/path/_constants.ts16
-rw-r--r--std/path/_globrex.ts4
-rw-r--r--std/path/_interface.ts (renamed from std/path/interface.ts)2
-rw-r--r--std/path/_util.ts3
-rw-r--r--std/path/common.ts1
-rw-r--r--std/path/glob.ts3
-rw-r--r--std/path/mod.ts6
-rw-r--r--std/path/posix.ts11
-rw-r--r--std/path/separator.ts5
-rw-r--r--std/path/win32.ts14
10 files changed, 47 insertions, 18 deletions
diff --git a/std/path/_constants.ts b/std/path/_constants.ts
index ae0aac184..186c32ab5 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;
+
+let isWindows = false;
+if (globalThis.Deno != null) {
+ isWindows = Deno.build.os == "windows";
+} else if (navigator?.appVersion != null) {
+ isWindows = navigator.appVersion.includes("Win");
+}
-export const SEP = isWindows ? "\\" : "/";
-export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
+export { isWindows };
diff --git a/std/path/_globrex.ts b/std/path/_globrex.ts
index 2b3af14ad..6ad297d86 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
index b31c89ea7..6c82c9c35 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 2776303cb..8ae40373b 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 e0e51ef23..01470105c 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 80672579d..34fc213f6 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 9cb7f1edb..0b4156e69 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 e88eb3f97..365232e33 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 fb990b808..4b54ad438 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 0557b3768..f556c5b73 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.