-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
copy path util functions from legacy to harmony
- Loading branch information
Showing
12 changed files
with
82 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { normalizePath } from './normalize-path'; | ||
export { pathJoinLinux, pathNormalizeToLinux, pathRelativeLinux, pathResolveToLinux } from './to-linux'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*! | ||
* normalize-path <https://github.com/jonschlinkert/normalize-path> | ||
* | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
|
||
export function normalizePath(path: string, stripTrailing?: boolean) { | ||
if (path === '\\' || path === '/') return '/'; | ||
|
||
const len = path.length; | ||
if (len <= 1) return path; | ||
|
||
// ensure that win32 namespaces has two leading slashes, so that the path is | ||
// handled properly by the win32 version of path.parse() after being normalized | ||
// https://msdn.microsoft.com/library/windows/desktop/aa365247(v=vs.85).aspx#namespaces | ||
let prefix = ''; | ||
if (len > 4 && path[3] === '\\') { | ||
const ch = path[2]; | ||
if ((ch === '?' || ch === '.') && path.slice(0, 2) === '\\\\') { | ||
path = path.slice(2); | ||
prefix = '//'; | ||
} | ||
} | ||
|
||
const segs = path.split(/[/\\]+/); | ||
if (stripTrailing !== false && segs[segs.length - 1] === '') { | ||
segs.pop(); | ||
} | ||
return prefix + segs.join('/'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as path from 'path'; | ||
import { normalizePath } from './normalize-path'; | ||
|
||
export type PathLinux = string; // Linux format path (even when running on Windows) | ||
export type PathLinuxRelative = string; | ||
export type PathLinuxAbsolute = string; | ||
|
||
// OS based format. On Windows it is Windows format, on Linux it is Linux format. | ||
export type PathOsBased = string | PathOsBasedRelative | PathOsBasedAbsolute; | ||
export type PathOsBasedRelative = string; | ||
export type PathOsBasedAbsolute = string; | ||
|
||
export function pathJoinLinux(...paths): PathLinux { | ||
return normalizePath(path.join(...paths)); | ||
} | ||
export function pathNormalizeToLinux(pathToNormalize?: PathOsBased): PathLinux | undefined { | ||
return pathToNormalize ? normalizePath(pathToNormalize) : pathToNormalize; | ||
} | ||
export function pathRelativeLinux(from: PathOsBased, to: PathOsBased): PathLinux { | ||
return normalizePath(path.relative(from, to)); | ||
} | ||
export function pathResolveToLinux(arr: PathOsBased[]): PathLinux { | ||
return normalizePath(path.resolve(arr.join(','))); | ||
} | ||
|
||
/** | ||
* path.resolve uses current working dir. | ||
* sometimes the cwd is not important. a user may run a Bit command from an inner dir. | ||
*/ | ||
export function getPathRelativeRegardlessCWD(from: PathOsBasedRelative, to: PathOsBasedRelative): PathLinuxRelative { | ||
const fromLinux = pathNormalizeToLinux(from); | ||
const toLinux = pathNormalizeToLinux(to); | ||
// change them to absolute so path.relative won't consider the cwd | ||
return pathRelativeLinux(`/${fromLinux}`, `/${toLinux}`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters