summaryrefslogtreecommitdiff
path: root/cli/fmt.rs
blob: f007d8da8da63c8c883b74579666a5242a76b24f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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.

use dprint_plugin_typescript as dprint;
use glob;
use regex::Regex;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::time::Instant;

lazy_static! {
  static ref TYPESCRIPT_LIB: Regex = Regex::new(".d.ts$").unwrap();
  static ref TYPESCRIPT: Regex = Regex::new(".tsx?$").unwrap();
  static ref JAVASCRIPT: Regex = Regex::new(".jsx?$").unwrap();
}

fn is_supported(path: &Path) -> bool {
  let path_str = path.to_string_lossy();
  !TYPESCRIPT_LIB.is_match(&path_str)
    && (TYPESCRIPT.is_match(&path_str) || JAVASCRIPT.is_match(&path_str))
}

fn get_config() -> dprint::configuration::Configuration {
  dprint::configuration::ConfigurationBuilder::new()
    .line_width(80)
    .indent_width(2)
    .next_control_flow_position(
      dprint::configuration::NextControlFlowPosition::SameLine,
    )
    .binary_expression_operator_position(
      dprint::configuration::OperatorPosition::SameLine,
    )
    .build()
}

fn get_supported_files(paths: Vec<PathBuf>) -> Vec<PathBuf> {
  let mut files_to_check = vec![];

  for path in paths {
    if is_supported(&path) {
      files_to_check.push(path.to_owned());
    }
  }

  files_to_check
}

fn check_source_files(
  config: dprint::configuration::Configuration,
  paths: Vec<PathBuf>,
) {
  let start = Instant::now();
  let mut not_formatted_files = vec![];

  for file_path in paths {
    let file_path_str = file_path.to_string_lossy();
    let file_contents = fs::read_to_string(&file_path).unwrap();
    match dprint::format_text(&file_path_str, &file_contents, &config) {
      Ok(None) => {
        // nothing to format, pass
      }
      Ok(Some(formatted_text)) => {
        if formatted_text != file_contents {
          println!("Not formatted {}", file_path_str);
          not_formatted_files.push(file_path);
        }
      }
      Err(e) => {
        eprintln!("Error checking: {}", &file_path_str);
        eprintln!("   {}", e);
      }
    }
  }

  let duration = Instant::now() - start;

  if !not_formatted_files.is_empty() {
    let f = if not_formatted_files.len() == 1 {
      "file"
    } else {
      "files"
    };

    eprintln!(
      "Found {} not formatted {} in {:?}",
      not_formatted_files.len(),
      f,
      duration
    );
    std::process::exit(1);
  }
}

fn format_source_files(
  config: dprint::configuration::Configuration,
  paths: Vec<PathBuf>,
) {
  let start = Instant::now();
  let mut not_formatted_files = vec![];

  for file_path in paths {
    let file_path_str = file_path.to_string_lossy();
    let file_contents = fs::read_to_string(&file_path).unwrap();
    match dprint::format_text(&file_path_str, &file_contents, &config) {
      Ok(None) => {
        // nothing to format, pass
      }
      Ok(Some(formatted_text)) => {
        if formatted_text != file_contents {
          println!("Formatting {}", file_path_str);
          fs::write(&file_path, formatted_text).unwrap();
          not_formatted_files.push(file_path);
        }
      }
      Err(e) => {
        eprintln!("Error formatting: {}", &file_path_str);
        eprintln!("   {}", e);
      }
    }
  }

  let duration = Instant::now() - start;
  let f = if not_formatted_files.len() == 1 {
    "file"
  } else {
    "files"
  };
  eprintln!(
    "Formatted {} {} in {:?}",
    not_formatted_files.len(),
    f,
    duration
  );
}

fn get_matching_files(glob_paths: Vec<String>) -> Vec<PathBuf> {
  let mut target_files = Vec::with_capacity(128);

  for path in glob_paths {
    let files = glob::glob(&path)
      .expect("Failed to execute glob.")
      .filter_map(Result::ok);
    target_files.extend(files);
  }

  target_files
}

/// Format JavaScript/TypeScript files.
///
/// First argument supports globs, and if it is `None`
/// then the current directory is recursively walked.
pub fn format_files(maybe_files: Option<Vec<String>>, check: bool) {
  // TODO: improve glob to look for tsx?/jsx? files only
  let glob_paths = maybe_files.unwrap_or_else(|| vec!["**/*".to_string()]);
  let matching_files = get_matching_files(glob_paths);
  let matching_files = get_supported_files(matching_files);
  let config = get_config();

  if check {
    check_source_files(config, matching_files);
  } else {
    format_source_files(config, matching_files);
  }
}