Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
ad4af23a
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
ad4af23a
编写于
8月 13, 2020
作者:
D
Divy Srivastava
提交者:
GitHub
8月 12, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
unify path normalization utility (#6865)
上级
c4d9c6aa
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
43 addition
and
73 deletion
+43
-73
cli/fs.rs
cli/fs.rs
+3
-38
core/lib.rs
core/lib.rs
+2
-0
core/module_specifier.rs
core/module_specifier.rs
+2
-35
core/normalize_path.rs
core/normalize_path.rs
+36
-0
未找到文件。
cli/fs.rs
浏览文件 @
ad4af23a
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
pub
use
deno_core
::
normalize_path
;
use
deno_core
::
ErrBox
;
use
std
::
env
::
current_dir
;
use
std
::
fs
::
OpenOptions
;
use
std
::
io
::
Write
;
use
std
::
path
::{
Component
,
Path
,
PathBuf
};
use
deno_core
::
ErrBox
;
use
std
::
path
::{
Path
,
PathBuf
};
use
walkdir
::
WalkDir
;
pub
fn
write_file
<
T
:
AsRef
<
[
u8
]
>>
(
...
...
@@ -46,41 +46,6 @@ pub fn write_file_2<T: AsRef<[u8]>>(
file
.write_all
(
data
.as_ref
())
}
/// IMPORTANT: This method is duplicated in core/module_specifier.rs
///
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
///
/// Taken from Cargo
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
pub
fn
normalize_path
(
path
:
&
Path
)
->
PathBuf
{
let
mut
components
=
path
.components
()
.peekable
();
let
mut
ret
=
if
let
Some
(
c
@
Component
::
Prefix
(
..
))
=
components
.peek
()
.cloned
()
{
components
.next
();
PathBuf
::
from
(
c
.as_os_str
())
}
else
{
PathBuf
::
new
()
};
for
component
in
components
{
match
component
{
Component
::
Prefix
(
..
)
=>
unreachable!
(),
Component
::
RootDir
=>
{
ret
.push
(
component
.as_os_str
());
}
Component
::
CurDir
=>
{}
Component
::
ParentDir
=>
{
ret
.pop
();
}
Component
::
Normal
(
c
)
=>
{
ret
.push
(
c
);
}
}
}
ret
}
pub
fn
resolve_from_cwd
(
path
:
&
Path
)
->
Result
<
PathBuf
,
ErrBox
>
{
let
resolved_path
=
if
path
.is_absolute
()
{
path
.to_owned
()
...
...
core/lib.rs
浏览文件 @
ad4af23a
...
...
@@ -15,6 +15,7 @@ mod es_isolate;
mod
flags
;
mod
module_specifier
;
mod
modules
;
mod
normalize_path
;
mod
ops
;
pub
mod
plugin_api
;
mod
resources
;
...
...
@@ -43,6 +44,7 @@ pub use crate::modules::ModuleLoader;
pub
use
crate
::
modules
::
ModuleSource
;
pub
use
crate
::
modules
::
ModuleSourceFuture
;
pub
use
crate
::
modules
::
RecursiveModuleLoad
;
pub
use
crate
::
normalize_path
::
normalize_path
;
pub
use
crate
::
ops
::
Buf
;
pub
use
crate
::
ops
::
Op
;
pub
use
crate
::
ops
::
OpAsyncFuture
;
...
...
core/module_specifier.rs
浏览文件 @
ad4af23a
use
crate
::
normalize_path
;
use
std
::
env
::
current_dir
;
use
std
::
error
::
Error
;
use
std
::
fmt
;
use
std
::
path
::
Component
;
use
std
::
path
::
Path
;
use
std
::
path
::
PathBuf
;
use
url
::
ParseError
;
use
url
::
Url
;
...
...
@@ -206,42 +205,10 @@ impl PartialEq<String> for ModuleSpecifier {
}
}
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
///
/// Taken from Cargo
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
pub
fn
normalize_path
(
path
:
&
Path
)
->
PathBuf
{
let
mut
components
=
path
.components
()
.peekable
();
let
mut
ret
=
if
let
Some
(
c
@
Component
::
Prefix
(
..
))
=
components
.peek
()
.cloned
()
{
components
.next
();
PathBuf
::
from
(
c
.as_os_str
())
}
else
{
PathBuf
::
new
()
};
for
component
in
components
{
match
component
{
Component
::
Prefix
(
..
)
=>
unreachable!
(),
Component
::
RootDir
=>
{
ret
.push
(
component
.as_os_str
());
}
Component
::
CurDir
=>
{}
Component
::
ParentDir
=>
{
ret
.pop
();
}
Component
::
Normal
(
c
)
=>
{
ret
.push
(
c
);
}
}
}
ret
}
#[cfg(test)]
mod
tests
{
use
super
::
*
;
use
std
::
path
::
Path
;
#[test]
fn
test_resolve_import
()
{
...
...
core/normalize_path.rs
0 → 100644
浏览文件 @
ad4af23a
use
std
::
path
::
Component
;
use
std
::
path
::
Path
;
use
std
::
path
::
PathBuf
;
/// Normalize all itermediate components of the path (ie. remove "./" and "../" components).
/// Similar to `fs::canonicalize()` but doesn't resolve symlinks.
///
/// Taken from Cargo
/// https://github.com/rust-lang/cargo/blob/af307a38c20a753ec60f0ad18be5abed3db3c9ac/src/cargo/util/paths.rs#L60-L85
pub
fn
normalize_path
(
path
:
&
Path
)
->
PathBuf
{
let
mut
components
=
path
.components
()
.peekable
();
let
mut
ret
=
if
let
Some
(
c
@
Component
::
Prefix
(
..
))
=
components
.peek
()
.cloned
()
{
components
.next
();
PathBuf
::
from
(
c
.as_os_str
())
}
else
{
PathBuf
::
new
()
};
for
component
in
components
{
match
component
{
Component
::
Prefix
(
..
)
=>
unreachable!
(),
Component
::
RootDir
=>
{
ret
.push
(
component
.as_os_str
());
}
Component
::
CurDir
=>
{}
Component
::
ParentDir
=>
{
ret
.pop
();
}
Component
::
Normal
(
c
)
=>
{
ret
.push
(
c
);
}
}
}
ret
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录