2020-01-29 21:16:48 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
//! This module provides file formating utilities using
|
|
|
|
//! [`dprint`](https://github.com/dsherret/dprint).
|
|
|
|
//!
|
|
|
|
//! At the moment it is only consumed using CLI but in
|
|
|
|
//! the future it can be easily extended to provide
|
|
|
|
//! the same functions as ops available in JS runtime.
|
|
|
|
|
2020-02-17 13:11:45 -05:00
|
|
|
use crate::fs::files_in_subtree;
|
2020-02-27 15:39:41 -05:00
|
|
|
use crate::op_error::OpError;
|
2020-02-09 05:19:05 -05:00
|
|
|
use deno_core::ErrBox;
|
2020-01-30 03:33:32 -05:00
|
|
|
use dprint_plugin_typescript as dprint;
|
2020-01-29 21:16:48 -05:00
|
|
|
use std::fs;
|
2020-02-09 05:19:05 -05:00
|
|
|
use std::io::stdin;
|
|
|
|
use std::io::stdout;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::io::Write;
|
2020-01-29 21:16:48 -05:00
|
|
|
use std::path::Path;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn is_supported(path: &Path) -> bool {
|
2020-02-13 16:02:18 -05:00
|
|
|
if let Some(ext) = path.extension() {
|
|
|
|
if ext == "tsx" || ext == "js" || ext == "jsx" {
|
|
|
|
true
|
|
|
|
} else if ext == "ts" {
|
|
|
|
// Currently dprint does not support d.ts files.
|
|
|
|
// https://github.com/dsherret/dprint/issues/100
|
|
|
|
!path.as_os_str().to_string_lossy().ends_with(".d.ts")
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
|
2020-02-03 15:52:32 -05:00
|
|
|
fn get_config() -> dprint::configuration::Configuration {
|
2020-03-11 05:51:57 -04:00
|
|
|
use dprint::configuration::*;
|
2020-03-25 17:24:26 -04:00
|
|
|
ConfigurationBuilder::new().prettier().build()
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:52:32 -05:00
|
|
|
fn check_source_files(
|
|
|
|
config: dprint::configuration::Configuration,
|
|
|
|
paths: Vec<PathBuf>,
|
2020-02-13 16:02:18 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-01-29 21:16:48 -05:00
|
|
|
let mut not_formatted_files = vec![];
|
|
|
|
|
|
|
|
for file_path in paths {
|
|
|
|
let file_path_str = file_path.to_string_lossy();
|
2020-04-08 10:31:48 -04:00
|
|
|
let file_contents = fs::read_to_string(&file_path)?;
|
|
|
|
let r = dprint::format_text(&file_path_str, &file_contents, &config);
|
|
|
|
match r {
|
2020-01-29 21:16:48 -05:00
|
|
|
Ok(None) => {
|
|
|
|
// nothing to format, pass
|
|
|
|
}
|
|
|
|
Ok(Some(formatted_text)) => {
|
|
|
|
if formatted_text != file_contents {
|
|
|
|
not_formatted_files.push(file_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error checking: {}", &file_path_str);
|
|
|
|
eprintln!(" {}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 16:02:18 -05:00
|
|
|
if not_formatted_files.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
2020-02-23 14:51:29 -05:00
|
|
|
Err(
|
2020-02-27 15:39:41 -05:00
|
|
|
OpError::other(format!(
|
2020-02-26 05:50:53 -05:00
|
|
|
"Found {} not formatted {}",
|
2020-02-23 14:51:29 -05:00
|
|
|
not_formatted_files.len(),
|
2020-04-08 10:31:48 -04:00
|
|
|
files_str(not_formatted_files.len()),
|
2020-02-23 14:51:29 -05:00
|
|
|
))
|
|
|
|
.into(),
|
|
|
|
)
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-08 10:31:48 -04:00
|
|
|
fn files_str(len: usize) -> &'static str {
|
|
|
|
if len == 1 {
|
|
|
|
"file"
|
|
|
|
} else {
|
|
|
|
"files"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 15:52:32 -05:00
|
|
|
fn format_source_files(
|
|
|
|
config: dprint::configuration::Configuration,
|
|
|
|
paths: Vec<PathBuf>,
|
2020-02-26 05:50:53 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-01-29 21:16:48 -05:00
|
|
|
let mut not_formatted_files = vec![];
|
|
|
|
|
|
|
|
for file_path in paths {
|
|
|
|
let file_path_str = file_path.to_string_lossy();
|
2020-02-26 05:50:53 -05:00
|
|
|
let file_contents = fs::read_to_string(&file_path)?;
|
2020-04-08 10:31:48 -04:00
|
|
|
let r = dprint::format_text(&file_path_str, &file_contents, &config);
|
|
|
|
match r {
|
2020-03-25 17:24:26 -04:00
|
|
|
Ok(None) => {
|
|
|
|
// nothing to format, pass
|
|
|
|
}
|
|
|
|
Ok(Some(formatted_text)) => {
|
|
|
|
if formatted_text != file_contents {
|
|
|
|
println!("{}", file_path_str);
|
|
|
|
fs::write(&file_path, formatted_text)?;
|
|
|
|
not_formatted_files.push(file_path);
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
}
|
2020-03-25 17:24:26 -04:00
|
|
|
Err(e) => {
|
|
|
|
eprintln!("Error formatting: {}", &file_path_str);
|
|
|
|
eprintln!(" {}", e);
|
|
|
|
}
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 10:31:48 -04:00
|
|
|
debug!(
|
|
|
|
"Formatted {} {}",
|
|
|
|
not_formatted_files.len(),
|
|
|
|
files_str(not_formatted_files.len()),
|
|
|
|
);
|
2020-02-26 05:50:53 -05:00
|
|
|
Ok(())
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Format JavaScript/TypeScript files.
|
|
|
|
///
|
|
|
|
/// First argument supports globs, and if it is `None`
|
|
|
|
/// then the current directory is recursively walked.
|
2020-02-26 05:50:53 -05:00
|
|
|
pub fn format(args: Vec<String>, check: bool) -> Result<(), ErrBox> {
|
2020-02-13 16:02:18 -05:00
|
|
|
if args.len() == 1 && args[0] == "-" {
|
2020-02-27 15:39:41 -05:00
|
|
|
return format_stdin(check);
|
2020-02-13 16:02:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut target_files: Vec<PathBuf> = vec![];
|
|
|
|
|
|
|
|
if args.is_empty() {
|
2020-02-17 13:11:45 -05:00
|
|
|
target_files.extend(files_in_subtree(
|
|
|
|
std::env::current_dir().unwrap(),
|
|
|
|
is_supported,
|
|
|
|
));
|
2020-02-13 16:02:18 -05:00
|
|
|
} else {
|
|
|
|
for arg in args {
|
|
|
|
let p = PathBuf::from(arg);
|
|
|
|
if p.is_dir() {
|
2020-02-17 13:11:45 -05:00
|
|
|
target_files.extend(files_in_subtree(p, is_supported));
|
2020-02-09 05:19:05 -05:00
|
|
|
} else {
|
2020-02-13 16:02:18 -05:00
|
|
|
target_files.push(p);
|
|
|
|
};
|
2020-02-09 05:19:05 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-29 21:16:48 -05:00
|
|
|
let config = get_config();
|
|
|
|
if check {
|
2020-02-13 16:02:18 -05:00
|
|
|
check_source_files(config, target_files)?;
|
2020-01-29 21:16:48 -05:00
|
|
|
} else {
|
2020-02-26 05:50:53 -05:00
|
|
|
format_source_files(config, target_files)?;
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
2020-02-09 05:19:05 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Format stdin and write result to stdout.
|
|
|
|
/// Treats input as TypeScript.
|
|
|
|
/// Compatible with `--check` flag.
|
2020-02-27 15:39:41 -05:00
|
|
|
fn format_stdin(check: bool) -> Result<(), ErrBox> {
|
2020-02-09 05:19:05 -05:00
|
|
|
let mut source = String::new();
|
|
|
|
if stdin().read_to_string(&mut source).is_err() {
|
2020-02-27 15:39:41 -05:00
|
|
|
return Err(OpError::other("Failed to read from stdin".to_string()).into());
|
2020-02-09 05:19:05 -05:00
|
|
|
}
|
|
|
|
let config = get_config();
|
|
|
|
|
|
|
|
match dprint::format_text("_stdin.ts", &source, &config) {
|
2020-02-13 16:02:18 -05:00
|
|
|
Ok(None) => unreachable!(),
|
2020-02-09 05:19:05 -05:00
|
|
|
Ok(Some(formatted_text)) => {
|
|
|
|
if check {
|
|
|
|
if formatted_text != source {
|
|
|
|
println!("Not formatted stdin");
|
|
|
|
}
|
|
|
|
} else {
|
2020-02-27 15:39:41 -05:00
|
|
|
stdout().write_all(formatted_text.as_bytes())?;
|
2020-02-09 05:19:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2020-02-27 15:39:41 -05:00
|
|
|
return Err(OpError::other(e).into());
|
2020-02-09 05:19:05 -05:00
|
|
|
}
|
|
|
|
}
|
2020-02-27 15:39:41 -05:00
|
|
|
Ok(())
|
2020-01-29 21:16:48 -05:00
|
|
|
}
|
2020-02-13 16:02:18 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_is_supported() {
|
|
|
|
assert!(!is_supported(Path::new("tests/subdir/redirects")));
|
|
|
|
assert!(!is_supported(Path::new("README.md")));
|
|
|
|
assert!(!is_supported(Path::new("lib/typescript.d.ts")));
|
|
|
|
assert!(is_supported(Path::new("cli/tests/001_hello.js")));
|
|
|
|
assert!(is_supported(Path::new("cli/tests/002_hello.ts")));
|
|
|
|
assert!(is_supported(Path::new("foo.jsx")));
|
|
|
|
assert!(is_supported(Path::new("foo.tsx")));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn check_tests_dir() {
|
|
|
|
// Because of cli/tests/error_syntax.js the following should fail but not
|
|
|
|
// crash.
|
2020-02-26 05:50:53 -05:00
|
|
|
let r = format(vec!["./tests".to_string()], true);
|
2020-02-13 16:02:18 -05:00
|
|
|
assert!(r.is_err());
|
|
|
|
}
|