mirror of
https://github.com/denoland/deno.git
synced 2024-11-26 16:09:27 -05:00
fix(std/path): Support browsers (#6003)
This commit is contained in:
parent
464f5bf769
commit
10573183af
10 changed files with 47 additions and 18 deletions
|
@ -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 };
|
||||
|
|
|
@ -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 ? `\\` : `/`;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
/** This module is browser compatible. */
|
||||
|
||||
/**
|
||||
* A parsed path object generated by path.parse() or consumed by path.format().
|
||||
*/
|
|
@ -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,
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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 ? /[\\/]+/ : /\/+/;
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue