2021-01-11 12:13:41 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-08-12 16:34:17 -04:00
|
|
|
pub use deno_core::normalize_path;
|
2020-12-18 13:30:49 -05:00
|
|
|
use deno_runtime::deno_crypto::rand;
|
2020-03-20 09:46:26 -04:00
|
|
|
use std::env::current_dir;
|
|
|
|
use std::fs::OpenOptions;
|
2020-10-29 22:19:03 -04:00
|
|
|
use std::io::{Error, Write};
|
2020-08-12 16:34:17 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-02-17 13:11:45 -05:00
|
|
|
use walkdir::WalkDir;
|
2018-07-26 17:54:22 -04:00
|
|
|
|
2020-12-18 13:30:49 -05:00
|
|
|
pub fn atomic_write_file<T: AsRef<[u8]>>(
|
|
|
|
filename: &Path,
|
|
|
|
data: T,
|
|
|
|
mode: u32,
|
|
|
|
) -> std::io::Result<()> {
|
|
|
|
let rand: String = (0..4)
|
|
|
|
.map(|_| format!("{:02x}", rand::random::<u8>()))
|
|
|
|
.collect();
|
|
|
|
let extension = format!("{}.tmp", rand);
|
|
|
|
let tmp_file = filename.with_extension(extension);
|
|
|
|
write_file(&tmp_file, data, mode)?;
|
|
|
|
std::fs::rename(tmp_file, filename)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-11 08:36:34 -05:00
|
|
|
pub fn write_file<T: AsRef<[u8]>>(
|
2018-09-11 12:00:57 -04:00
|
|
|
filename: &Path,
|
2018-12-11 08:36:34 -05:00
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
mode: u32,
|
2018-09-11 12:00:57 -04:00
|
|
|
) -> std::io::Result<()> {
|
2020-03-07 22:29:12 -05:00
|
|
|
write_file_2(filename, data, true, mode, true, false)
|
2019-02-02 14:26:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_file_2<T: AsRef<[u8]>>(
|
|
|
|
filename: &Path,
|
|
|
|
data: T,
|
2020-03-07 22:29:12 -05:00
|
|
|
update_mode: bool,
|
|
|
|
mode: u32,
|
2019-02-02 14:26:18 -05:00
|
|
|
is_create: bool,
|
|
|
|
is_append: bool,
|
|
|
|
) -> std::io::Result<()> {
|
2018-09-11 12:00:57 -04:00
|
|
|
let mut file = OpenOptions::new()
|
|
|
|
.read(false)
|
|
|
|
.write(true)
|
|
|
|
.append(is_append)
|
|
|
|
.truncate(!is_append)
|
2019-02-02 14:26:18 -05:00
|
|
|
.create(is_create)
|
2018-09-11 12:00:57 -04:00
|
|
|
.open(filename)?;
|
|
|
|
|
2020-03-07 22:29:12 -05:00
|
|
|
if update_mode {
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
{
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
let mode = mode & 0o777;
|
|
|
|
let permissions = PermissionsExt::from_mode(mode);
|
|
|
|
file.set_permissions(permissions)?;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2020-03-20 09:46:26 -04:00
|
|
|
#[cfg(not(unix))]
|
|
|
|
let _ = mode;
|
2018-08-23 18:36:45 -04:00
|
|
|
}
|
2018-09-14 15:30:43 -04:00
|
|
|
|
2020-03-20 09:46:26 -04:00
|
|
|
file.write_all(data.as_ref())
|
2019-05-07 21:58:58 -04:00
|
|
|
}
|
2019-07-17 18:15:30 -04:00
|
|
|
|
2020-10-29 22:19:03 -04:00
|
|
|
/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
|
|
|
|
pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
|
|
|
|
let mut canonicalized_path = path.canonicalize()?;
|
|
|
|
if cfg!(windows) {
|
|
|
|
canonicalized_path = PathBuf::from(
|
|
|
|
canonicalized_path
|
|
|
|
.display()
|
|
|
|
.to_string()
|
|
|
|
.trim_start_matches("\\\\?\\"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(canonicalized_path)
|
|
|
|
}
|
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
pub fn resolve_from_cwd(path: &Path) -> Result<PathBuf, AnyError> {
|
2020-01-20 09:45:44 -05:00
|
|
|
let resolved_path = if path.is_absolute() {
|
|
|
|
path.to_owned()
|
2019-07-17 18:15:30 -04:00
|
|
|
} else {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2019-07-17 18:15:30 -04:00
|
|
|
cwd.join(path)
|
|
|
|
};
|
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
Ok(normalize_path(&resolved_path))
|
2019-07-17 18:15:30 -04:00
|
|
|
}
|
2019-09-07 14:13:09 -04:00
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
/// Checks if the path has extension Deno supports.
|
|
|
|
pub fn is_supported_ext(path: &Path) -> bool {
|
2021-01-19 12:39:35 -05:00
|
|
|
if let Some(ext) = get_extension(path) {
|
|
|
|
matches!(ext.as_str(), "ts" | "tsx" | "js" | "jsx" | "mjs")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:31:32 -05:00
|
|
|
/// This function is similar to is_supported_ext but adds additional extensions
|
|
|
|
/// supported by `deno fmt`.
|
|
|
|
pub fn is_supported_ext_fmt(path: &Path) -> bool {
|
2021-01-19 12:39:35 -05:00
|
|
|
if let Some(ext) = get_extension(path) {
|
2021-02-18 11:31:32 -05:00
|
|
|
matches!(
|
|
|
|
ext.as_str(),
|
|
|
|
"ts" | "tsx" | "js" | "jsx" | "mjs" | "md" | "json" | "jsonc"
|
|
|
|
)
|
2020-11-14 07:05:26 -05:00
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:39:35 -05:00
|
|
|
/// Get the extension of a file in lowercase.
|
|
|
|
pub fn get_extension(file_path: &Path) -> Option<String> {
|
|
|
|
return file_path
|
|
|
|
.extension()
|
|
|
|
.and_then(|e| e.to_str())
|
|
|
|
.map(|e| e.to_lowercase());
|
|
|
|
}
|
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
/// Collects file paths that satisfy the given predicate, by recursively walking `files`.
|
|
|
|
/// If the walker visits a path that is listed in `ignore`, it skips descending into the directory.
|
|
|
|
pub fn collect_files<P>(
|
2020-11-22 15:45:44 -05:00
|
|
|
files: &[PathBuf],
|
|
|
|
ignore: &[PathBuf],
|
2020-11-14 07:05:26 -05:00
|
|
|
predicate: P,
|
|
|
|
) -> Result<Vec<PathBuf>, AnyError>
|
|
|
|
where
|
|
|
|
P: Fn(&Path) -> bool,
|
|
|
|
{
|
|
|
|
let mut target_files = Vec::new();
|
|
|
|
|
|
|
|
// retain only the paths which exist and ignore the rest
|
|
|
|
let canonicalized_ignore: Vec<PathBuf> = ignore
|
2020-11-22 15:45:44 -05:00
|
|
|
.iter()
|
2020-11-14 07:05:26 -05:00
|
|
|
.filter_map(|i| i.canonicalize().ok())
|
|
|
|
.collect();
|
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let cur_dir = [std::env::current_dir()?];
|
|
|
|
let files = if files.is_empty() { &cur_dir } else { files };
|
2020-11-14 07:05:26 -05:00
|
|
|
|
|
|
|
for file in files {
|
|
|
|
for entry in WalkDir::new(file)
|
|
|
|
.into_iter()
|
|
|
|
.filter_entry(|e| {
|
|
|
|
e.path().canonicalize().map_or(false, |c| {
|
|
|
|
!canonicalized_ignore.iter().any(|i| c.starts_with(i))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.filter_map(|e| match e {
|
|
|
|
Ok(e) if !e.file_type().is_dir() && predicate(e.path()) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
{
|
|
|
|
target_files.push(entry.into_path().canonicalize()?)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(target_files)
|
|
|
|
}
|
|
|
|
|
2019-09-07 14:13:09 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-11-14 07:05:26 -05:00
|
|
|
use tempfile::TempDir;
|
2019-09-07 14:13:09 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_child() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a")).unwrap(), cwd.join("a"));
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_dot() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new(".")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_parent() {
|
2020-03-20 09:46:26 -04:00
|
|
|
let cwd = current_dir().unwrap();
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(Path::new("a/..")).unwrap(), cwd);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:11:52 -05:00
|
|
|
#[test]
|
|
|
|
fn test_normalize_path() {
|
|
|
|
assert_eq!(normalize_path(Path::new("a/../b")), PathBuf::from("b"));
|
|
|
|
assert_eq!(normalize_path(Path::new("a/./b/")), PathBuf::from("a/b/"));
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("a/./b/../c")),
|
|
|
|
PathBuf::from("a/c")
|
|
|
|
);
|
|
|
|
|
|
|
|
if cfg!(windows) {
|
|
|
|
assert_eq!(
|
|
|
|
normalize_path(Path::new("C:\\a\\.\\b\\..\\c")),
|
|
|
|
PathBuf::from("C:\\a\\c")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-10 23:16:30 -04:00
|
|
|
// TODO: Get a good expected value here for Windows.
|
|
|
|
#[cfg(not(windows))]
|
2019-09-07 14:13:09 -04:00
|
|
|
#[test]
|
|
|
|
fn resolve_from_cwd_absolute() {
|
2019-09-10 23:16:30 -04:00
|
|
|
let expected = Path::new("/a");
|
2020-01-20 09:45:44 -05:00
|
|
|
assert_eq!(resolve_from_cwd(expected).unwrap(), expected);
|
2019-09-07 14:13:09 -04:00
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_is_supported_ext() {
|
|
|
|
assert!(!is_supported_ext(Path::new("tests/subdir/redirects")));
|
|
|
|
assert!(!is_supported_ext(Path::new("README.md")));
|
|
|
|
assert!(is_supported_ext(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(is_supported_ext(Path::new("cli/tests/001_hello.js")));
|
|
|
|
assert!(is_supported_ext(Path::new("cli/tests/002_hello.ts")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.jsx")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.tsx")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.TS")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.TSX")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.JS")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.JSX")));
|
|
|
|
assert!(is_supported_ext(Path::new("foo.mjs")));
|
|
|
|
assert!(!is_supported_ext(Path::new("foo.mjsx")));
|
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
|
2021-01-19 12:39:35 -05:00
|
|
|
#[test]
|
2021-02-18 11:31:32 -05:00
|
|
|
fn test_is_supported_ext_fmt() {
|
|
|
|
assert!(!is_supported_ext_fmt(Path::new("tests/subdir/redirects")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("README.md")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("readme.MD")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("cli/tests/001_hello.js")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("cli/tests/002_hello.ts")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.jsx")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.tsx")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.TS")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.TSX")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.JS")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.JSX")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.mjs")));
|
|
|
|
assert!(!is_supported_ext_fmt(Path::new("foo.mjsx")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.jsonc")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.JSONC")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.json")));
|
|
|
|
assert!(is_supported_ext_fmt(Path::new("foo.JsON")));
|
2021-01-19 12:39:35 -05:00
|
|
|
}
|
|
|
|
|
2020-11-14 07:05:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_collect_files() {
|
|
|
|
fn create_files(dir_path: &PathBuf, files: &[&str]) {
|
|
|
|
std::fs::create_dir(dir_path).expect("Failed to create directory");
|
|
|
|
for f in files {
|
|
|
|
let path = dir_path.join(f);
|
|
|
|
std::fs::write(path, "").expect("Failed to create file");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// dir.ts
|
|
|
|
// ├── a.ts
|
|
|
|
// ├── b.js
|
|
|
|
// ├── child
|
|
|
|
// │ ├── e.mjs
|
|
|
|
// │ ├── f.mjsx
|
|
|
|
// │ ├── .foo.TS
|
|
|
|
// │ └── README.md
|
|
|
|
// ├── c.tsx
|
|
|
|
// ├── d.jsx
|
|
|
|
// └── ignore
|
|
|
|
// ├── g.d.ts
|
|
|
|
// └── .gitignore
|
|
|
|
|
|
|
|
let t = TempDir::new().expect("tempdir fail");
|
|
|
|
|
|
|
|
let root_dir_path = t.path().join("dir.ts");
|
|
|
|
let root_dir_files = ["a.ts", "b.js", "c.tsx", "d.jsx"];
|
|
|
|
create_files(&root_dir_path, &root_dir_files);
|
|
|
|
|
|
|
|
let child_dir_path = root_dir_path.join("child");
|
|
|
|
let child_dir_files = ["e.mjs", "f.mjsx", ".foo.TS", "README.md"];
|
|
|
|
create_files(&child_dir_path, &child_dir_files);
|
|
|
|
|
|
|
|
let ignore_dir_path = root_dir_path.join("ignore");
|
|
|
|
let ignore_dir_files = ["g.d.ts", ".gitignore"];
|
|
|
|
create_files(&ignore_dir_path, &ignore_dir_files);
|
|
|
|
|
2020-11-22 15:45:44 -05:00
|
|
|
let result = collect_files(&[root_dir_path], &[ignore_dir_path], |path| {
|
|
|
|
// exclude dotfiles
|
|
|
|
path
|
|
|
|
.file_name()
|
|
|
|
.and_then(|f| f.to_str())
|
|
|
|
.map_or(false, |f| !f.starts_with('.'))
|
|
|
|
})
|
|
|
|
.unwrap();
|
2020-11-14 07:05:26 -05:00
|
|
|
let expected = [
|
|
|
|
"a.ts",
|
|
|
|
"b.js",
|
|
|
|
"e.mjs",
|
|
|
|
"f.mjsx",
|
|
|
|
"README.md",
|
|
|
|
"c.tsx",
|
|
|
|
"d.jsx",
|
|
|
|
];
|
|
|
|
for e in expected.iter() {
|
|
|
|
assert!(result.iter().any(|r| r.ends_with(e)));
|
|
|
|
}
|
|
|
|
assert_eq!(result.len(), expected.len());
|
|
|
|
}
|
2020-02-17 13:11:45 -05:00
|
|
|
}
|