mirror of
https://github.com/denoland/deno.git
synced 2025-09-28 05:04:48 +00: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.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
/** This module is browser compatible. */
|
||||||
const { build } = Deno;
|
|
||||||
|
|
||||||
// Alphabet chars.
|
// Alphabet chars.
|
||||||
export const CHAR_UPPERCASE_A = 65; /* A */
|
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_0 = 48; /* 0 */
|
||||||
export const CHAR_9 = 57; /* 9 */
|
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 ? "\\" : "/";
|
let isWindows = false;
|
||||||
export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
|
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
|
// This file is ported from globrex@0.1.2
|
||||||
// MIT License
|
// MIT License
|
||||||
// Copyright (c) 2018 Terkel Gjervig Nielsen
|
// 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 = isWin ? `(?:\\\\|\\/)` : `\\/`;
|
||||||
const SEP_ESC = isWin ? `\\\\` : `/`;
|
const SEP_ESC = isWin ? `\\\\` : `/`;
|
||||||
const SEP_RAW = 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().
|
* A parsed path object generated by path.parse() or consumed by path.format().
|
||||||
*/
|
*/
|
|
@ -1,7 +1,8 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// Ported from https://github.com/browserify/path-browserify/
|
||||||
|
/** This module is browser compatible. */
|
||||||
|
|
||||||
import { FormatInputPathObject } from "./interface.ts";
|
import { FormatInputPathObject } from "./_interface.ts";
|
||||||
import {
|
import {
|
||||||
CHAR_UPPERCASE_A,
|
CHAR_UPPERCASE_A,
|
||||||
CHAR_LOWERCASE_A,
|
CHAR_LOWERCASE_A,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
||||||
|
/** This module is browser compatible. */
|
||||||
|
|
||||||
import { SEP } from "./separator.ts";
|
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 { SEP, SEP_PATTERN } from "./separator.ts";
|
||||||
import { globrex } from "./_globrex.ts";
|
import { globrex } from "./_globrex.ts";
|
||||||
import { join, normalize } from "./mod.ts";
|
import { join, normalize } from "./mod.ts";
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported mostly from https://github.com/browserify/path-browserify/
|
// 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 _win32 from "./win32.ts";
|
||||||
import * as _posix from "./posix.ts";
|
import * as _posix from "./posix.ts";
|
||||||
|
|
||||||
const isWindows = Deno.build.os == "windows";
|
|
||||||
|
|
||||||
const path = isWindows ? _win32 : _posix;
|
const path = isWindows ? _win32 : _posix;
|
||||||
|
|
||||||
export const win32 = _win32;
|
export const win32 = _win32;
|
||||||
|
@ -29,5 +29,5 @@ export const {
|
||||||
|
|
||||||
export * from "./common.ts";
|
export * from "./common.ts";
|
||||||
export { SEP, SEP_PATTERN } from "./separator.ts";
|
export { SEP, SEP_PATTERN } from "./separator.ts";
|
||||||
export * from "./interface.ts";
|
export * from "./_interface.ts";
|
||||||
export * from "./glob.ts";
|
export * from "./glob.ts";
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// 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 { CHAR_DOT, CHAR_FORWARD_SLASH } from "./_constants.ts";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -24,7 +24,12 @@ export function resolve(...pathSegments: string[]): string {
|
||||||
let path: string;
|
let path: string;
|
||||||
|
|
||||||
if (i >= 0) path = pathSegments[i];
|
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);
|
assertPath(path);
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
// 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 = isWindows ? "\\" : "/";
|
||||||
export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
|
export const SEP_PATTERN = isWindows ? /[\\/]+/ : /\/+/;
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
// Copyright the Browserify authors. MIT License.
|
// Copyright the Browserify authors. MIT License.
|
||||||
// Ported from https://github.com/browserify/path-browserify/
|
// 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 {
|
import {
|
||||||
CHAR_DOT,
|
CHAR_DOT,
|
||||||
CHAR_BACKWARD_SLASH,
|
CHAR_BACKWARD_SLASH,
|
||||||
|
@ -32,14 +32,20 @@ export function resolve(...pathSegments: string[]): string {
|
||||||
if (i >= 0) {
|
if (i >= 0) {
|
||||||
path = pathSegments[i];
|
path = pathSegments[i];
|
||||||
} else if (!resolvedDevice) {
|
} 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 {
|
} else {
|
||||||
|
if (globalThis.Deno == null) {
|
||||||
|
throw new TypeError("Resolved a relative path without a CWD.");
|
||||||
|
}
|
||||||
// Windows has the concept of drive-specific current working
|
// Windows has the concept of drive-specific current working
|
||||||
// directories. If we've resolved a drive letter but not yet an
|
// directories. If we've resolved a drive letter but not yet an
|
||||||
// absolute path, get cwd for that drive, or the process cwd if
|
// 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
|
// 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.
|
// 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
|
// Verify that a cwd was found and that it actually points
|
||||||
// to our drive. If not, default to the drive's root.
|
// to our drive. If not, default to the drive's root.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue